$Java设计模式之——观察者模式(Observer)

  (一)观察者模式简介

  1、定义:定义对象间一种一对多的依赖关系,一个对象状态发生改变时,所有依赖它的对象都会接到通知并作出相应的响应。

  2、应用场景:

  (1)GUI系统

  (2)订阅-发布系统

  (3)事件多级触发场景

  (4)当一个对象改变时需要通知其他对象,但不知道有其他对象具体有哪些时

  3、UML类图

  

  (二)观察者模式实例

  1、假设有个珠宝公司要运送一批钻石,强盗也盯上这批钻石了,准备在运输途中抢劫,而珠宝公司雇佣的保镖要全程对钻石进行保护,警察也派出警车护航,关系如下图:

  2、代码如下:

  (1)抽象观察者接口:

1 /**
2  * 抽象观察者
3  *
4  */
5 
6 public interface Watcher {
7     // 对被观察者状态变化做出响应的抽象方法
8     public void update(String msg);
9 }

  (2)抽象被观察者接口:

 1 /**
 2  * 抽象被观察者
 3  *
 4  */
 5 
 6 public interface Watched {
 7     // 添加观察者
 8     public void addWatcher(Watcher watcher);
 9 
10     // 移除观察者
11     public void removeWatcher(Watcher watcher);
12 
13     // 通知观察者
14     public void notifyWatchers(String msg);
15 }

  (3)保镖类:

 1 /**
 2  * 保镖类,实现Watcher接口
 3  *
 4  */
 5 
 6 public class Security implements Watcher {
 7 
 8     @Override
 9     public void update(String msg) {
10         System.out.println("保镖收到消息:" + msg + "。保镖开始保护!");
11     }
12 
13 }

  (4)警察类:

 1 /**
 2  * 警察类,实现Watcher接口
 3  *
 4  */
 5 
 6 public class Police implements Watcher {
 7 
 8     @Override
 9     public void update(String msg) {
10         System.out.println("警察收到消息:" + msg + "。警察开始派警车护航!");
11     }
12 
13 }

  (5)强盗类:

/**
 * 强盗类,实现Watcher接口
 *
 */

public class Thief implements Watcher {

    @Override
    public void update(String msg) {
        System.out.println("收到消息:" + msg + "。强盗准备动手!");
    }

}

  (6)珠宝运输类:

 1 /**
 2  * 具体的被观察者
 3  *
 4  */
 5 
 6 public class Transporter implements Watched {
 7 
 8     List<Watcher> wathcerList = new ArrayList<Watcher>();
 9 
10     @Override
11     public void addWatcher(Watcher watcher) {
12         wathcerList.add(watcher);
13     }
14 
15     @Override
16     public void removeWatcher(Watcher watcher) {
17         wathcerList.remove(watcher);
18     }
19 
20     @Override
21     public void notifyWatchers(String msg) {
22         for (Watcher w : wathcerList) {
23             w.update(msg);
24         }
25     }
26 
27 }

  (6)测试类:

 1 public class Test {
 2     public static void main(String[] args) {
 3         Security s = new Security();
 4         Thief t = new Thief();
 5         Police p = new Police();
 6 
 7         Transporter transPorter = new Transporter();
 8         transPorter.addWatcher(s);
 9         transPorter.addWatcher(t);
10         transPorter.addWatcher(p);
11 
12         transPorter.notifyWatchers("运输车队开始出发了");
13 
14         transPorter.removeWatcher(t);
15         transPorter.notifyWatchers("运输车队摆脱了强盗");
16     }
17 }

  (7)输出结果:

保镖收到消息:运输车队开始出发了。保镖开始保护!
收到消息:运输车队开始出发了。强盗准备动手!
警察收到消息:运输车队开始出发了。警察开始派警车护航!
保镖收到消息:运输车队摆脱了强盗。保镖开始保护!
警察收到消息:运输车队摆脱了强盗。警察开始派警车护航!

 

Refer:http://blog.csdn.net/jason0539/article/details/45055233

posted @ 2016-05-01 01:26  AzureSky  阅读(274)  评论(0编辑  收藏  举报