Observer Pattern(又叫发布订阅模式)
定义了对象之间的一对多依赖,让多个观察者对象同时监听一个主体对象,当主体对象发生变化时,它的所有依赖者(观察者)都会收到通知并更新,属于行为型模式。
以订阅报纸为例,当人们订阅了报纸后,只要有新的报纸一发布,订阅了该报纸的人就能立即受到通知。
报纸 Newspaper 类:
public class Newspaper extends Observable {
private String title;
public String getTitle() {
return title;
}
public void publishNew(String title){
this.title = title;
System.out.println("发布了标题为【"+ title +"】的新闻");
this.setChanged();
this.notifyObservers("新华社");
}
}
订阅报纸的 People 类
public class People implements Observer {
private String name ;
public People(String name) {
this.name = name;
}
@Override
public void update(Observable o, Object arg) {
Newspaper n = (Newspaper) o;
System.out.println(this.name + "收到了新闻:"+n.getTitle()+",该新闻被【"+arg+"】发布");
}
}
测试代码:
public class NewsPublishTest {
public static void main(String[] args) {
Newspaper news = new Newspaper();
news.addObserver(new People("张三"));
news.publishNew("今天是个好日子");
}
}
输出结果:
发布了标题为【今天是个好日子】的新闻
张三收到了新闻:今天是个好日子,该新闻被【新华社】发布
上面的JDK版本是将整个被观察的对象都继承了Observable
类,同时还要让被观察者手动设置改变和发送通知
this.setChanged();
this.notifyObservers("新华社");
而且观察者那里也要先实现Observer
接口,并实现update
方法,这样显得代码很繁琐,换成Guava就很清爽了
报纸 Newspaper 类:完全没有代码侵入
public class Newspaper {
private String title;
public String getTitle() {
return title;
}
public void publishNew(String title){
this.title = title;
System.out.println("发布了 guava 版本的新闻,标题为【"+title+"】");
}
}
订阅报纸的 People 类,也很简单,只需要在处理订阅结果的方法上添加 Subscribe注解即可
public class People {
private String name;
public People(String name) {
this.name = name;
}
@Subscribe
public void receiveNews(Object o){
Newspaper n = (Newspaper) o;
System.out.println(this.name + "收到了新闻【"+n.getTitle()+"】");
}
}
测试类:
public class TestGuavaNewspaper {
public static void main(String[] args) {
EventBus eventBus = new EventBus(); //消息总线
Newspaper newspaper = new Newspaper();
newspaper.publishNew("Guava不错");
eventBus.register(new People("老李"));//先注册观察者
eventBus.post(newspaper);//后发送被观察者
}
}
实现的思路其实很简单,就是将观察者的实例和需要调用的方法放入到一个集合中(addListener方法干的事),然后在被观察者的相应动作(需要被观察的刚发)发生时,去集合中拿到观察者对象和相应方法,通过反射机制去调用观察者的观察方法,这样就能实现实时监听,代码如下:
首先,还是要先定义一个 Observable类,主要功能是可以添加观察者和手动触发观察者调用相关方法
public class Observable {
private Map<String,Map<String,Object>> events = new HashMap<String, Map<String,Object>>();
public void addListener(String eventType,Object object,String methodName) {//添加注册方法
try {
Map<String,Object> callbackInfo = new HashMap<String, Object>();
callbackInfo.put("observer",object);
callbackInfo.put("method",object.getClass().getMethod(methodName,Observable.class));
events.put(eventType,callbackInfo);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public void trigger(String eventType){ //触发观察者的观察方法
try {
if(events.containsKey(eventType)){
Map<String,Object> callbackInfo = events.get(eventType);
Method m = (Method) callbackInfo.get("method");
m.invoke(callbackInfo.get("observer"),this); //将本身作为参数传递给观察者
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我这里实现的很简单粗暴,还可以进一步优化代码,比如添加一个事件类,将相关的事件源、事件名称、回调方法进行封装,这里就继续扩展了。
Newspaper类
public class Newspaper extends Observable {
private String title;
public String getTitle() {
return title;
}
public void publishNew(String title){
this.title = title;
System.out.println("发布了标题为【"+ title +"】的新闻");
this.trigger("publish");
}
}
People类,没有代码侵入的痕迹
public class People {
private String name ;
public People(String name) {
this.name = name;
}
public void receiveNew(Observable o) {
Newspaper n = (Newspaper) o;
System.out.println(this.name + "收到了新闻》》》"+n.getTitle());
}
}
测试类:
public class NewsPublishTest {
public static void main(String[] args) {
Newspaper newspaper = new Newspaper();
People laoLi = new People("老李");
newspaper.addListener("publish",laoLi,"receiveNew");
newspaper.publishNew("手写观察者模式上线了");
}
}
输出结果:
发布了标题为【手写观察者模式上线了】的新闻
老李收到了新闻》》》手写观察者模式上线了
这里的观察(监听)都是同步监听,在很多系统中,考虑到业务量太大,对时效性要求不是特别严格的情况下,用异步监听能提高系统的性能吞吐量,比如MQ,异步队列等
Listener
结尾的基本上都是观察者模式