在阎宏博士的《JAVA与模式》一书中开头是这样描述观察者(Observer)模式的:

  观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式、模型-视图(Model/View)模式、源-监听器(Source/Listener)模式或从属者(Dependents)模式。

  观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。


观察者模式的结构

  一个软件系统里面包含了各种对象,就像一片欣欣向荣的森林充满了各种生物一样。在一片森林中,各种生物彼此依赖和约束,形成一个个生物链。一种生物的状态变化会造成其他一些生物的相应行动,每一个生物都处于别的生物的互动之中。

  同样,一个软件系统常常要求在某一个对象的状态发生变化的时候,某些其他的对象做出相应的改变。做到这一点的设计方案有很多,但是为了使系统能够易于复用,应该选择低耦合度的设计方案。减少对象之间的耦合有利于系统的复用,但是同时设计师需要使这些低耦合度的对象之间能够维持行动的协调一致,保证高度的协作。观察者模式是满足这一要求的各种设计方案中最重要的一种。

  下面以一个简单的示意性实现为例,讨论观察者模式的结构。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnQAAAD5CAIAAAAhuj+3AAAgAElEQVR4nO2dL4/kSBLFG412pQPzJSw1bDjAYI+NdMhs4Uom9wEMljWy9gtYWrjAYLHRQMMjlg6OjJZ54AKDhnngqeJiMm2Xq8v/wvV+oFXtsl1ZlWE/R2ZkxJMjhBBCyKI87d0AQggh5GxQXAkhhJCFobgSQgghC0NxJYQQQhaG4koIIYQsDMWVEEIIWRiKKyGEELIwC4jry8vLEyFL8/Lycr9xEkLILiwgrk9PdH/J8tCuCCF2obiSg0K7IoTYheJKDgrtihBiF4orOSi0K0KIXSiu5KDQrgghdqG4Lk/f93s34QzQrgghdqG4zqJpmuRCmqZju3VdF8dxnucTO0RRVFXVOs08FY9gV4SQs0JxnUUcx+KPxnE8sWee5xPi6pxLkqRt23e0oW3b9x1olEewK0LIWaG4XqdpGi2odV1PDPxeFdd3k2VZ0zRrnPmYnN6uCCEnh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FFdCCCFkYSiuhBBCyMJQXAkhhJCF+R9Sr2UFsYelvgAAAABJRU5ErkJggg==" alt="" />

  观察者模式所涉及的角色有:

  ●  抽象主题(Subject)角色:抽象主题角色把所有对观察者对象的引用保存在一个聚集(比如ArrayList对象)里,每个主题都可以有任何数量的观察者。抽象主题提供一个接口,可以增加和删除观察者对象,抽象主题角色又叫做抽象被观察者(Observable)角色。

  ●  具体主题(ConcreteSubject)角色:将有关状态存入具体观察者对象;在具体主题的内部状态改变时,给所有登记过的观察者发出通知。具体主题角色又叫做具体被观察者(Concrete Observable)角色。

  ●  抽象观察者(Observer)角色:为所有的具体观察者定义一个接口,在得到主题的通知时更新自己,这个接口叫做更新接口。

  ●  具体观察者(ConcreteObserver)角色:存储与主题的状态自恰的状态。具体观察者角色实现抽象观察者角色所要求的更新接口,以便使本身的状态与主题的状态 像协调。如果需要,具体观察者角色可以保持一个指向具体主题对象的引用。

  源代码

    抽象主题角色类

public abstract class Subject {
/**
* 用来保存注册的观察者对象
*/
private List<Observer> list = new ArrayList<Observer>();
/**
* 注册观察者对象
* @param observer 观察者对象
*/
public void attach(Observer observer){ list.add(observer);
System.out.println("Attached an observer");
}
/**
* 删除观察者对象
* @param observer 观察者对象
*/
public void detach(Observer observer){ list.remove(observer);
}
/**
* 通知所有注册的观察者对象
*/
public void nodifyObservers(String newState){ for(Observer observer : list){
observer.update(newState);
}
}
}

  具体主题角色类

public class ConcreteSubject extends Subject{

    private String state;

    public String getState() {
return state;
} public void change(String newState){
state = newState;
System.out.println("主题状态为:" + state);
//状态发生改变,通知各个观察者
this.nodifyObservers(state);
}
}

  抽象观察者角色类

public interface Observer {
/**
* 更新接口
* @param state 更新的状态
*/
public void update(String state);
}

  具体观察者角色类

public class ConcreteObserver implements Observer {
//观察者的状态
private String observerState; @Override
public void update(String state) {
/**
* 更新观察者的状态,使其与目标的状态保持一致
*/
observerState = state;
System.out.println("状态为:"+observerState);
} }

  客户端类

public class Client {

    public static void main(String[] args) {
//创建主题对象
ConcreteSubject subject = new ConcreteSubject();
//创建观察者对象
Observer observer = new ConcreteObserver();
//将观察者对象登记到主题对象上
subject.attach(observer);
//改变主题对象的状态
subject.change("new state");
} }

  运行结果如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA14AAACsCAIAAADkCOoqAAAgAElEQVR4nO3df3ATV4IncFXtXV1d3VzdH7s1Wze1VamtmdrZhKvbze6KmtXMxXe1M7nN7C0TMk4gv7BCGMXkBz9scJwAsQOMbBNiBCTgQEAmIVhYOBgMwkCITDA/kxgTMMPEGYz5EcBGpvFPWb/6/nit1uvu97pbsmTL4vuprkRudb9+/fp166vXErIISh0dHbW1tbW1tcL9Z5Lu+CStdpaQO7x4P6mN6+jomOgjMPFy5gzKmR0BgPHBeyGw0H+Ql8lxrFUWmaQ7PkmrnSVIh5/onDaRkBFlOXMq5cyOgHn19fVz587NT97cuXPr6+vTWAhMRtqXgEQ0RC6cdCZptbME6fAxiMVisRgCopBDJ1TO7Ej2WLly5dDQ0ETXgs3j8cyePbu6uvrD5FVXVz/99NMejycthZD6ZHNbAY/q+i9FQ1UurK2x17pm1tbYa2sKa2tLPbWVHk+Np9HjaWz2NLc0trQ3tlwa74pnjOFltHpzo/foBe/RC9WbG8elRqbQ1fb73eanCaxz9kAuZEI6zJlQlTM7kkYuV6XHo24Wj6fW5ao0XFc/7pzcv5A5jbXG5hQWFrpcrh07duxL3oEDB6qrqwsLC9NSiJm2gqxFX/850dA1s10IiqJ4KxikJ1EUD7d2kMeNLVcmoO4ZoH8NXb62/tNDX3166Kvdh7769NBXy9dmy7C5KhqavIGIaEiQDh+lPFO2+8H5rfrTM2W7o7nuPh8+TCVRffJz1/xpe12zD9Y4uo6UZaBSqUA0VHG5Kl0ulyodklxI5uuvbhgNR0MR1XRh79xk02Hdh4UmJ3qt/Pz8bdu2NTc3n1bKX91uOH311VenT58m94XHXoiZtkqjWnPGoSa0Qxv/JhPFztWVlrUY0VDVfDUuO4mG3efOdlSt+HL6Yx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/sRDQEAAAAmBImG9fX1E/wNZTod4t81BAAAAJgQfdnz7xrqm5DWAQAAALivqALYOEXDuwAAAACQ9ehcmMFoeC95KVcCAAAAAHgMMxidCzMVDVP47efbAAAAAJBuKaSy9EdDAAAAALjfIBoCAAAAgESOgv8f+trXCM7g8qkAAAAASUVORK5CYII=" alt="" />

  在运行时,这个客户端首先创建了具体主题类的实例,以及一个观察者对象。然后,它调用主题对象的attach()方法,将这个观察者对象向主题对象登记,也就是将它加入到主题对象的聚集中去。

  这时,客户端调用主题的change()方法,改变了主题对象的内部状态。主题对象在状态发生变化时,调用超类的notifyObservers()方法,通知所有登记过的观察者对象。

推模型和拉模型

  在观察者模式中,又分为推模型和拉模型两种方式。

  ●  推模型

     主题对象向观察者推送主题的详细信息,不管观察者是否需要,推送的信息通常是主题对象的全部或部分数据。

  ●  拉模型

     主题对象在通知观察者的时候,只传递少量信息。如果观察者需要更具体的信息,由观察者主动到主题对象中获取,相当于是观察者从主题对象中拉数据。一般这种模型的实现中,会把主题对象自身通过update()方法传递给观察者,这样在观察者需要获取数据的时候,就可以通过这个引用来获取了。

  根据上面的描述,发现前面的例子就是典型的推模型,下面给出一个拉模型的实例。

  拉模型的抽象观察者类

  拉模型通常都是把主题对象当做参数传递。

public interface Observer {
/**
* 更新接口
* @param subject 传入主题对象,方面获取相应的主题对象的状态
*/
public void update(Subject subject);
}

  拉模型的具体观察者类

public class ConcreteObserver implements Observer {
//观察者的状态
private String observerState; @Override
public void update(Subject subject) {
/**
* 更新观察者的状态,使其与目标的状态保持一致
*/
observerState = ((ConcreteSubject)subject).getState();
System.out.println("观察者状态为:"+observerState);
} }

  拉模型的抽象主题类

  拉模型的抽象主题类主要的改变是nodifyObservers()方法。在循环通知观察者的时候,也就是循环调用观察者的update()方法的时候,传入的参数不同了。

public abstract class Subject {
/**
* 用来保存注册的观察者对象
*/
private List<Observer> list = new ArrayList<Observer>();
/**
* 注册观察者对象
* @param observer 观察者对象
*/
public void attach(Observer observer){ list.add(observer);
System.out.println("Attached an observer");
}
/**
* 删除观察者对象
* @param observer 观察者对象
*/
public void detach(Observer observer){ list.remove(observer);
}
/**
* 通知所有注册的观察者对象
*/
public void nodifyObservers(){ for(Observer observer : list){
observer.update(this);
}
}
}

  拉模型的具体主题类

  跟推模型相比,有一点变化,就是调用通知观察者的方法的时候,不需要传入参数了。

public class ConcreteSubject extends Subject{

    private String state;

    public String getState() {
return state;
} public void change(String newState){
state = newState;
System.out.println("主题状态为:" + state);
//状态发生改变,通知各个观察者
this.nodifyObservers();
}
}

  两种模式的比较

  ■  推模型是假定主题对象知道观察者需要的数据;而拉模型是主题对象不知道观察者具体需要什么数据,没有办法的情况下,干脆把自身传递给观察者,让观察者自己去按需要取值。

  ■  推模型可能会使得观察者对象难以复用,因为观察者的update()方法是按需要定义的参数,可能无法兼顾没有考虑到的使用情况。这就意味着出现新情况的时候,就可能提供新的update()方法,或者是干脆重新实现观察者;而拉模型就不会造成这样的情况,因为拉模型下,update()方法的参数是主题对象本身,这基本上是主题对象能传递的最大数据集合了,基本上可以适应各种情况的需要。

JAVA提供的对观察者模式的支持

  在JAVA语言的java.util库里面,提供了一个Observable类以及一个Observer接口,构成JAVA语言对观察者模式的支持。

  Observer接口

  这个接口只定义了一个方法,即update()方法,当被观察者对象的状态发生变化时,被观察者对象的notifyObservers()方法就会调用这一方法。

public interface Observer {

    void update(Observable o, Object arg);
}

  Observable类

  被观察者类都是java.util.Observable类的子类。java.util.Observable提供公开的方法支持观察者对象,这些方法中有两个对Observable的子类非常重要:一个是setChanged(),另一个是notifyObservers()。第一方法setChanged()被调用之后会设置一个内部标记变量,代表被观察者对象的状态发生了变化。第二个是notifyObservers(),这个方法被调用时,会调用所有登记过的观察者对象的update()方法,使这些观察者对象可以更新自己。

public class Observable {
private boolean changed = false;
private Vector obs; /** Construct an Observable with zero Observers. */ public Observable() {
obs = new Vector();
} /**
* 将一个观察者添加到观察者聚集上面
*/
public synchronized void addObserver(Observer o) {
if (o == null)
throw new NullPointerException();
if (!obs.contains(o)) {
obs.addElement(o);
}
} /**
* 将一个观察者从观察者聚集上删除
*/
public synchronized void deleteObserver(Observer o) {
obs.removeElement(o);
} public void notifyObservers() {
notifyObservers(null);
} /**
* 如果本对象有变化(那时hasChanged 方法会返回true)
* 调用本方法通知所有登记的观察者,即调用它们的update()方法
* 传入this和arg作为参数
*/
public void notifyObservers(Object arg) { Object[] arrLocal; synchronized (this) { if (!changed)
return;
arrLocal = obs.toArray();
clearChanged();
} for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
} /**
* 将观察者聚集清空
*/
public synchronized void deleteObservers() {
obs.removeAllElements();
} /**
* 将“已变化”设置为true
*/
protected synchronized void setChanged() {
changed = true;
} /**
* 将“已变化”重置为false
*/
protected synchronized void clearChanged() {
changed = false;
} /**
* 检测本对象是否已变化
*/
public synchronized boolean hasChanged() {
return changed;
} /**
* Returns the number of observers of this <tt>Observable</tt> object.
*
* @return the number of observers of this object.
*/
public synchronized int countObservers() {
return obs.size();
}
}

  这个类代表一个被观察者对象,有时称之为主题对象。一个被观察者对象可以有数个观察者对象,每个观察者对象都是实现Observer接口的对象。在被观察者发生变化时,会调用Observable的notifyObservers()方法,此方法调用所有的具体观察者的update()方法,从而使所有的观察者都被通知更新自己。

怎样使用JAVA对观察者模式的支持

  这里给出一个非常简单的例子,说明怎样使用JAVA所提供的对观察者模式的支持。在这个例子中,被观察对象叫做Watched;而观察者对象叫做Watcher。Watched对象继承自java.util.Observable类;而Watcher对象实现了java.util.Observer接口。另外有一个Test类扮演客户端角色。

  源代码

  被观察者Watched类源代码

public class Watched extends Observable{

    private String data = "";

    public String getData() {
return data;
} public void setData(String data) { if(!this.data.equals(data)){
this.data = data;
setChanged();
}
notifyObservers();
} }

  观察者类源代码

public class Watcher implements Observer{

    public Watcher(Observable o){
o.addObserver(this);
} @Override
public void update(Observable o, Object arg) { System.out.println("状态发生改变:" + ((Watched)o).getData());
} }

  测试类源代码

public class Test {

    public static void main(String[] args) {

        //创建被观察者对象
Watched watched = new Watched();
//创建观察者对象,并将被观察者对象登记
Observer watcher = new Watcher(watched);
//给被观察者状态赋值
watched.setData("start");
watched.setData("run");
watched.setData("stop"); } }

  Test对象首先创建了Watched和Watcher对象。在创建Watcher对象时,将Watched对象作为参数传入;然后Test对象调用Watched对象的setData()方法,触发Watched对象的内部状态变化;Watched对象进而通知实现登记过的Watcher对象,也就是调用它的update()方法。

原文地址:http://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html

Java常见设计模式之观察者模式的更多相关文章

  1. JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明

    1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...

  2. JAVA基础——设计模式之观察者模式

    观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.模型-视图(Model/View)模式.源-监听器(Source/Listener)模式或从属者(Dependen ...

  3. Java常见设计模式学习(非原创)

    文章大纲 一.策略模式二.观察者模式三.工厂模式四.单例模式五.其他模式六.设计模式总结七.参考文章   一.策略模式 现在假设我们有个"鸭子项目",首先我们用OOP(面向对象)的 ...

  4. java小白设计模式之观察者模式

    观察者模式: 对象之间多对一依赖的一种设计方案,被依赖对象为Subject(一),依赖对象为Observer(多),Subject通知Observer变化直接代码: package com.wz.tw ...

  5. Java常见设计模式之代理模式

    指由一个代理主题来操作真实主题,真实主题执行具体的业务操作,而代理主题负责其它相关业务的处理.比如生活中的通过代理访问网络,客户通过网络代理连接网络(具体业务),由代理服务器完成用户权限和访问限制等与 ...

  6. Java常见设计模式之工厂模式

    工厂模式在我们日常的应用中应当算是比较广泛的一种设计模式了.今天让我们一起来学习一下,工厂的设计模式. 工厂模式在<Java与模式>中分为三类:     1)简单工厂模式(Simple F ...

  7. Java常见设计模式之单例模式

         1.何为单例模式? 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的 ...

  8. java常见设计模式简要总结

    设计模式六大原则 1.开放封闭原则:对扩展开放,对修改封闭,意即程序拓展时不要动原有的代码 2.LSP原则:任何基类可以出现的地方,子类一定可以出现 3.依赖倒置原则:使用接口,依赖于抽象而不是具体 ...

  9. Java常见设计模式之适配器模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述适配器(Adapter)模式的: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能 ...

随机推荐

  1. Linux中假设系统丢失了ls命令

    假设系统丢失了ls命令,如何修复: 1.查询ls命令属于那个rpm包[root@localhost ~]# rpm -qf /bin/lscoreutils-8.4-43.el6.x86_64 2.挂 ...

  2. php生成随机密码的自定义函数

    php生成随机密码的自定义函数 生成一个随机密码的函数,生成的密码为小写字母与数字的随机字符串,长度可自定义. 复制代码代码如下: <?php /*  * php自动生成新密码自定义函数(带实例 ...

  3. [转]Homebrew 卸载时出现:Failed to locate Homebrew! 错误

    今天在 MacBook 上安装 Homebrew,结果中间断了网,想重新卸载重装,结果一直卸载失败.问题现象如下: 问题现象 卸载时错误如下: ruby -e "$(curl -fsSL h ...

  4. 用Java实现断点续传的基本思路和代码

    用Java实现断点续传的基本思路和代码   URL url = new URL(http://www.oschina.net/no-exist.zip); HttpURLConnection http ...

  5. finally return 执行关系 异常处理 c#

    Return.finally执行关系简述 除了函数出现system.exit(0)终止虚拟机,finally中的代码一定执行,return语句会等待finally的执行:如果是值传递,finally中 ...

  6. python中的id

    python对象都有三个特性分别是身份.类型.值,身份指该对象内存地址,内建函数id()可获得身份,类似于指针的地址,但不能控制这个值,类型决定对象可以保存什么类型的值,值是对象表示的数据项,pyth ...

  7. Apache 的 httpd.conf 注释

    ServerRoot “/usr/local“ ServerRoot用于指定守护进程httpd的运行目录,httpd在启动之后将自动将进程的当前目录改变为这个目录,因此如果设置文件中指定的文件或目录是 ...

  8. hbase shell概述

    hbase shell-general(常规指令):http://www.cnblogs.com/husky/p/6374867.html hbase shell-ddl(表定义指令):http:// ...

  9. 签offer和签三方协议

    一般来讲,签约分为两种:签offer和签三方协议.其中,前者对个人及企业的约束效力远不及后者.下面分别来介绍. 1.签offer offer一般是单位提供给你的一个录用意向,以合同的形式提供给你,要求 ...

  10. Luogu-3829 [SHOI2012]信用卡凸包

    这道题的转化很巧妙,可以把信用卡四个角的圆心看做平面上的点来做凸包,\(ans\)就是凸包周长加上一个圆的周长 // luogu-judger-enable-o2 #include<cmath& ...