观察者模式又叫发布-订阅(publish/subscribe)模式,定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使他们能够自动更新自己,我们第一时间可能会想到消息队列,但如果在同一个JVM进程中要实现这种观察者模式该怎么去做?我们可能会想到JDK内置的java.util.Observer,或者自己写代码,其实Google提供的guava工具包也有类似的实现,这就是EventBus,可以在github中通过guava的wiki中找到eventbus的wiki信息,地址为https://github.com/google/guava/wiki/EventBusExplained,示例demo如下
public class EventBusFactory {private static final EventBus eventBusInstance = new EventBus();
private EventBusFactory() {
}/**
* 获取事件总线
* @return
*/
public static EventBus getInstance(){
return eventBusInstance;
}/**
* 注册观察者对象
* @param object
*/
public static void register(Object object){
eventBusInstance.register(object);
}/**
* 取消注册观察者对象
* @param object
*/
public static void unRegister(Object object){
eventBusInstance.unregister(object);
}/**
* 投递事件
* @param object
*/
public static void post(Object object){
eventBusInstance.post(object);
}
}public class OrderEvent {
private Long orderId;
private Integer changeType;
public OrderEvent(Long orderId, Integer changeType) {
this.orderId = orderId;
this.changeType = changeType;
}public Long getOrderId() {
return orderId;
}public void setOrderId(Long orderId) {
this.orderId = orderId;
}public Integer getChangeType() {
return changeType;
}public void setChangeType(Integer changeType) {
this.changeType = changeType;
}
}public class OrderMsgObserver {@Subscribe
public void OrderSyncToMq(OrderEvent event) {
Integer changeType= event.getChangeType();
Long orderId =event.getOrderId();
System.out.println("订单开始发送MQ-》订单Id:"+orderId+"&变更类型:"+changeType);
String msgInfo="";
if(changeType ==1){
msgInfo="this order has been issued";
}else if(changeType ==2){
msgInfo ="this order has been finshed";
}else if(changeType ==0){
msgInfo="this order has been cancel ";
}
writeMq(orderId,msgInfo);
}public static void writeMq(Long orderId,String msgInfo){
System.out.println("发送MQ完成-》orderId:"+orderId+"&消息内容"+msgInfo);
}
}public class OrderSyncDbObserver {@Subscribe
public void OrderSyncToDb(OrderEvent event) {
Integer changeType= event.getChangeType();
Long orderId =event.getOrderId();
System.out.println("订单开始写库-》订单Id:"+orderId+"&变更类型:"+changeType);
String orderInfo="";
if(changeType ==1){
orderInfo="The order has been issued";
}else if(changeType ==2){
orderInfo ="The order has been finshed";
}else if(changeType ==0){
orderInfo="The order has been cancel ";
}
writeDb(orderId,orderInfo);
}public static void writeDb(Long orderId,String orderInfo){
System.out.println("写库完成-》orderId:"+orderId+"&变更内容"+orderInfo);
}
} @Test
public void testEventBus(){
OrderSyncDbObserver orderSyncDbObserver =new OrderSyncDbObserver();
OrderMsgObserver orderMsgObserver =new OrderMsgObserver();
System.out.println("注册写库观察者");
EventBusFactory.getInstance().register(orderSyncDbObserver);
System.out.println("注册发消息观察者");
EventBusFactory.getInstance().register(orderMsgObserver);
Long orderId=1298932323L;
System.out.println("开始投递事件-》订单Id:"+orderId);
EventBusFactory.getInstance().post(new OrderEvent(orderId,1));
System.out.println("投递事件完成-》订单Id:"+orderId);
}
测试结果如下:
注册写库观察者
注册发消息观察者
开始投递事件-》订单Id:1298932323
订单开始写库-》订单Id:1298932323&变更类型:1
写库完成-》orderId:1298932323&变更内容The order has been issued
订单开始发送MQ-》订单Id:1298932323&变更类型:1
发送MQ完成-》orderId:1298932323&消息内容this order has been issued
投递事件完成-》订单Id:1298932323
【观察者模式实现之EventBus(Google)】
推荐阅读
- 面试|我经历的IT公司面试及离职感受(转)
- java|设计模式——创建型——工厂方法(Factory Method)
- 设计模式|设计模式_创建型模式——工厂方法
- 设计模式|设计模式——创建型软件设计模式——工厂方法模式
- 设计模式之装饰器模式
- 设计模式之设计原则
- 设计模式六大原则(5)(迪米特法则 最少知道)
- Java高级面试|常见设计模式——装饰模式
- java设计模式——单例模式