观察者模式:探索消息推送与注册中心
一、引入话题-发散思考
二、简介
- 消息推送(广播)
- 注册中心/消息订阅
二、自定义接口实现
//主题接口
public interface Subject {
void registerObserver(Observer observer);
void removeObject(Observer observer);
void notifyObservers();
}
//主题具体实现类
public class ConcreteSubject implements Subject{
List</span><Observer> observers=<span style="color: #0000ff;">new</span> ArrayList<Observer><span style="color: #000000;">();
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> state;
</span><span style="color: #008000;">//</span><span style="color: #008000;">注册观察者 </span>
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">移除观察者</span>
@Override
public void removeObject(Observer observer) {
int index=observers.indexOf(observer);
if(index>=0){
observers.remove(index);
return;
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">通知观察者:类似消息推送</span>
@Override
public void notifyObservers() {
for(Observer observer:observers){
observer.update(this);
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getState() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> state;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setState(<span style="color: #0000ff;">int</span><span style="color: #000000;"> state) {
</span><span style="color: #0000ff;">this</span>.state =<span style="color: #000000;"> state;
notifyObservers();
}
}
//观察者接口
public interface Observer {
void update(Subject subject);
}
//观察者实现类
public class ConcreteObserver implements Observer{
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> state;
</span><span style="color: #008000;">//</span><span style="color: #008000;">同步主题的状态</span>
@Override
public void update(Subject subject) {
this.state=((ConcreteSubject)subject).getState();
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getState() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> state;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setState(<span style="color: #0000ff;">int</span><span style="color: #000000;"> state) {
</span><span style="color: #0000ff;">this</span>.state =<span style="color: #000000;"> state;
}
}
//客户端测试类
public class Client {
public static void main(String[] args) {
Subject subject=new ConcreteSubject();
ConcreteObserver observerA</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> ConcreteObserver();
ConcreteObserver observerB</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> ConcreteObserver();
subject.registerObserver(observerA);
subject.registerObserver(observerB);
((ConcreteSubject)subject).setState(</span>10<span style="color: #000000;">);
System.out.println(</span>"观察者A(订阅者A):"+<span style="color: #000000;">observerA.getState());
System.out.println(</span>"观察者B(订阅者B):"+<span style="color: #000000;">observerB.getState());
}
}
运行结果:
观察者A(订阅者A):10
观察者B(订阅者B):10
三、Java提供API
//具体主题类(继承自主题类Observable)
public class ConcreteSubject extends Observable{
private int state;
public void setState(int state) {
this.state = state;
setChanged();
notifyObservers();
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getState() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> state;
}
}
//具体观察者(实现javaAPI Observer)
public class ConcreteObserver implements Observer{
private int state;
@Override
public void update(Observable o, Object arg) {
this.state=((ConcreteSubject)o).getState();
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getState() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> state;
}
}
//客户端
public class Client {
public static void main(String[] args) {
ConcreteSubject subject=new ConcreteSubject();
ConcreteObserver observerA</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> ConcreteObserver();
ConcreteObserver observerB</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> ConcreteObserver();
subject.addObserver(observerA);
subject.addObserver(observerB);
subject.setState(</span>123<span style="color: #000000;">);
System.out.println(</span>"观察者A(订阅者A):"+<span style="color: #000000;">observerA.getState());
System.out.println(</span>"观察者B(订阅者B):"+<span style="color: #000000;">observerB.getState());
}
}
运行结构:
观察者A(订阅者A):123
观察者B(订阅者B):123
四、版权声明
作者:邱勇Aaron
出处:http://www.cnblogs.com/qiuyong/
您的支持是对博主深入思考总结的最大鼓励。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,尊重作者的劳动成果。
参考:Head First 设计模式