$Java设计模式之——观察者模式(Observer)
(一)观察者模式简介
1、定义:定义对象间一种一对多的依赖关系,一个对象状态发生改变时,所有依赖它的对象都会接到通知并作出相应的响应。
2、应用场景:
(1)GUI系统
(2)订阅-发布系统
(3)事件多级触发场景
(4)当一个对象改变时需要通知其他对象,但不知道有其他对象具体有哪些时
3、UML类图
(二)观察者模式实例
1、假设有个珠宝公司要运送一批钻石,强盗也盯上这批钻石了,准备在运输途中抢劫,而珠宝公司雇佣的保镖要全程对钻石进行保护,警察也派出警车护航,关系如下图:

2、代码如下:
(1)抽象观察者接口:
/**
* 抽象观察者
*
*/ public interface Watcher {
// 对被观察者状态变化做出响应的抽象方法
public void update(String msg);
}
(2)抽象被观察者接口:
/**
* 抽象被观察者
*
*/ public interface Watched {
// 添加观察者
public void addWatcher(Watcher watcher); // 移除观察者
public void removeWatcher(Watcher watcher); // 通知观察者
public void notifyWatchers(String msg);
}
(3)保镖类:
/**
* 保镖类,实现Watcher接口
*
*/ public class Security implements Watcher { @Override
public void update(String msg) {
System.out.println("保镖收到消息:" + msg + "。保镖开始保护!");
} }
(4)警察类:
/**
* 警察类,实现Watcher接口
*
*/ public class Police implements Watcher { @Override
public void update(String msg) {
System.out.println("警察收到消息:" + msg + "。警察开始派警车护航!");
} }
(5)强盗类:
/**
* 强盗类,实现Watcher接口
*
*/ public class Thief implements Watcher { @Override
public void update(String msg) {
System.out.println("收到消息:" + msg + "。强盗准备动手!");
} }
(6)珠宝运输类:
/**
* 具体的被观察者
*
*/ public class Transporter implements Watched { List<Watcher> wathcerList = new ArrayList<Watcher>(); @Override
public void addWatcher(Watcher watcher) {
wathcerList.add(watcher);
} @Override
public void removeWatcher(Watcher watcher) {
wathcerList.remove(watcher);
} @Override
public void notifyWatchers(String msg) {
for (Watcher w : wathcerList) {
w.update(msg);
}
} }
(6)测试类:
public class Test {
public static void main(String[] args) {
Security s = new Security();
Thief t = new Thief();
Police p = new Police();
Transporter transPorter = new Transporter();
transPorter.addWatcher(s);
transPorter.addWatcher(t);
transPorter.addWatcher(p);
transPorter.notifyWatchers("运输车队开始出发了");
transPorter.removeWatcher(t);
transPorter.notifyWatchers("运输车队摆脱了强盗");
}
}
(7)输出结果:
保镖收到消息:运输车队开始出发了。保镖开始保护!
收到消息:运输车队开始出发了。强盗准备动手!
警察收到消息:运输车队开始出发了。警察开始派警车护航!
保镖收到消息:运输车队摆脱了强盗。保镖开始保护!
警察收到消息:运输车队摆脱了强盗。警察开始派警车护航!
Refer:http://blog.csdn.net/jason0539/article/details/45055233
随机推荐
- PHP安装加载yaf扩展
Yaf,全称 Yet Another Framework,是一个C语言编写的PHP框架,是一个用PHP扩展形式提供的PHP开发框架, 相比于一般的PHP框架, 它更快. 它提供了Bootstrap, ...
- Android开发教程:shape和selector的结合使用(转载)
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- 【转】ATL提供的所有转换宏
在头文件<atlconv.h>中定义了ATL提供的所有转换宏,如: A2CW (LPCSTR) -> (LPCWSTR) A2W (LPCSTR) -> (LPWSTR ...
- Unicode与UTF-8互转(c语言和lua语言)
1. 基础 1.1 ASCII码 我们知道, 在计算机内部, 全部的信息终于都表示为一个二进制的字符串. 每个二进制 位(bit)有0和1两种状态, 因此八个二进制位就能够组合出 256种状态, 这被 ...
- Codeforces Beta Round #25 (Div. 2)--A. IQ test
IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- CentOS系统bash: groupadd: command not found问题
如果我们需要在CentOS执行新建用户组命令的时候,需要进入到ROOT权限,如果你用以下命令: 1 su2 su root 进入到ROOT账户,那么会出现上述的错误信息:“bash: groupadd ...
- Gcc手册(转)
手册链接地址:http://www.shanghai.ws/gnu/gcc_1.htm GCC中文手册 GCC现在是GNU中最主要和最流行的c & c++编译器. gcc/g++在执行编译工作 ...
- 导出excel页面假死
如果是asp.net页面 public override void VerifyRenderingInServerForm(Control control) {} 如果是Sharepoint w ...
- Markov chain
w https://en.wikipedia.org/wiki/Markov_chain https://zh.wikipedia.org/wiki/马尔科夫链 In probability theo ...
- python字符串拼接相关
#字符串拼接#str.join(元组.列表.字典.字符串) 之后生成的只能是字符串.str = "-";seq = ("a", "b", & ...