先给大家举一个简单的例子,相信大家瞬间就明白了,在某东购商城有一个降价通知功能,只要你登录了账号点击“降价通知”,然后只要该商品出现降价就会给你进行通知。
观察者模式顾名思义就是一个或多个观察者对象对一个被观察者对象,如果该被观察者对象发生变化所有的观察者都会发生自动更新。
观察者:这里指点击“降价通知的”用户
具体被观察者对象:这里指鬼谷子全书
现在我们就开始创建一个观察者模式
1、首先创建一个具体观察者对象Customer(这里指用户,这里不想写Get Set方法就使用了lombok注解偷下懒 哈哈!)
package com.weather.spring.cloud.initializrstart.design.mode.observer;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* @program: msa-weather-master
* @description:
* @author: W.HL
* @create: 2019-03-28 10:30
**/
public @Data class Customer
{
/**
* the member name
*/
@Setter @Getter private String name;
/**
* the member mobile
*/
@Setter @Getter private String mobile;
public Customer(String name, String mobile)
{
this.name = name;
this.mobile = mobile;
}
}
2、创建一个被观察对象接口 ObservedService (注:里面有三个方法分别是 添加观察者、移除观察者、产品价格更新)
package com.weather.spring.cloud.initializrstart.design.mode.observer;
/**
* @program: msa-weather-master
* @description: This is observer by customer service
* @author: W.HL
* @create: 2019-03-27 17:58
**/
public interface ObservedService
{
/**
* add observer
* @param customer
*/
void addCustomer(Customer customer);
/**
* remove observer
* @param customer
*/
void removeCustomer(Customer customer);
/**
* update the good prices
* @param money
*/
void updatePrices(double money);
}
3、创建一个观察者接口(注:里面有一个通知方法,如果被观察者对象商品价格发生变动则调用通知全部观察者接口)
package com.weather.spring.cloud.initializrstart.design.mode.observer;
import java.util.List;
/**
* @program: msa-weather-master
* @description: This is observer service that the good observerd by customer
* @author: W.HL
* @create: 2019-03-28 10:41
**/
public interface ObserverService
{
/**
* notice the all customer that observer the good by customer
* @param list
*/
void noticeCustomer(List<Customer> list);
}
4、编写具体的观察者实现类,商品价格发生变动具体通知操作做什么
package com.weather.spring.cloud.initializrstart.design.mode.observer;
import java.util.List;
/**
* @program: msa-weather-master
* @description: This is all customer that observer the good by prices update
* @author: W.HL
* @create: 2019-03-28 10:44
**/
public class ObserverContext implements ObserverService
{
/**
* notice the all customer that observer the good by customer
*
* @param list
*/
@Override
public void noticeCustomer(List<Customer> list)
{
list.forEach(customer -> {
System.out.println(customer.getName()+"! 您好,您关注的华为P20手机终于降价了,请尽快购买");
System.out.println("--------------------------------------------------------------");
});
}
}
5、编写具体被观察者实现接口类(
注:每次有人对该商品进行关注通知则调用addCustomer方法,并将其放入观察者集合中,如果取消对该商品的关注则调用removeCustomer方法,从观察者集合中移除该观察者,商品价格发生变动则调用updatePrices方法并且只有降价的时候才进行通知观察者)
package com.weather.spring.cloud.initializrstart.design.mode.observer;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @program: msa-weather-master
* @description: This is a observed service for the customer
* @author: W.HL
* @create: 2019-03-28 10:34
**/
public class SubjectContext implements ObservedService
{
/**
* the observed sell price
*/
@Setter @Getter private double sellPrice;
/**
* the observed good name
*/
@Setter @Getter private String goodName;
private List<Customer> observerList = new ArrayList<>();
@Setter @Getter private ObserverContext observerContext;
/**
* add observer
*
* @param customer
*/
@Override
public void addCustomer(Customer customer)
{
observerList.add(customer);
}
/**
* remove observer
*
* @param customer
*/
@Override
public void removeCustomer(Customer customer)
{
if(observerList.contains(customer)){
observerList.remove(customer);
}
}
/**
* update the good prices
*
* @param money
*/
@Override
public void updatePrices(double money)
{
if(new BigDecimal(sellPrice).compareTo(new BigDecimal(money)) == 1){
/*if sell price < update money then notify all the observer customer*/
System.out.println("*****************产品降价大促销开始了*****************");
observerContext.noticeCustomer(observerList);
}
this.setSellPrice(money);
}
public SubjectContext(double sellPrice, String goodName)
{
this.sellPrice = sellPrice;
this.goodName = goodName;
}
}
6、编写测试方法
- 首先创建四个观察者
- 创建一个具体被观察者对象
- 将观察者和被观察者关联(通俗的说就是这四个会员用户都关注了华为P20该商品,如果该手机降价都会给这四个用户发送降价通知)
- 进行产品降价操作
- 将其一个会员用户取消对其关注 再次进行降价操作
package com.weather.spring.cloud.initializrstart.design.mode.observer;
import org.junit.Test;
public class ObserverModeContextTest
{
@Test
public void observerTest(){
Customer customer_1 = new Customer("张无忌","13693545297");
Customer customer_2 = new Customer("赵敏","13693545296");
Customer customer_3 = new Customer("周芷若","13693545295");
Customer customer_4 = new Customer("杨不悔","13693545294");
ObservedService observedContext = new SubjectContext(1999.00,"华为P20");
/*添加观察者*/
observedContext.addCustomer(customer_1);
observedContext.addCustomer(customer_2);
observedContext.addCustomer(customer_3);
observedContext.addCustomer(customer_4);
((SubjectContext) observedContext).setObserverContext(new ObserverContext());
/*被观察者产品价格产生变动*/
observedContext.updatePrices(1899.00);
/*取消关注*/
observedContext.removeCustomer(customer_4);
observedContext.updatePrices(1799.00);
}
}
7、预想结果
- 第一次执行价格变动会对四个用户都发送降价通知
- 一个会员取消关注后只会给前三个发送降价通知
8、查看结果,猜想正确