# 《Java极简设计模式》第19章:观察者模式(Observer)
作者:冰河
星球:http://m6z.cn/6aeFbs (opens new window)
博客:https://binghe.gitcode.host (opens new window)
文章汇总:https://binghe.gitcode.host/md/all/all.html (opens new window)
源码地址:https://github.com/binghe001/java-simple-design-patterns/tree/master/java-simple-design-observer (opens new window)
沉淀,成长,突破,帮助他人,成就自我。
- 本章难度:★★☆☆☆
- 本章重点:用最简短的篇幅介绍观察者模式最核心的知识,理解观察者模式的设计精髓,并能够灵活运用到实际项目中,编写可维护的代码。
大家好,我是CurleyG~~
今天给大家介绍《Java极简设计模式》的第19章:观察者模式(Observer),多一句没有,少一句不行,用最简短的篇幅讲述设计模式最核心的知识,好了,开始今天的内容。
# 一、概述
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
# 二、适用性
1.当一个抽象模型有两个方面,其中一个方面依赖于另一方面。 将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
2.当对一个对象的改变需要同时改变其它对象,而不知道具体有多少对象有待改变。
3.当一个对象必须通知其它对象,而又不能确定其它对象是谁。
# 三、参与者
1.Subject(目标) 目标知道它的观察者。可以有任意多个观察者观察同一个目标。 提供注册和删除观察者对象的接口。
2.Observer(观察者) 为那些在目标发生改变时需获得通知的对象定义一个更新接口。
3.ConcreteSubject(具体目标) 将有关状态存入各ConcreteObserver对象。 当它的状态发生改变时,向各个观察者发出通知。
4.ConcreteObserver(具体观察者) 维护一个指向ConcreteSubject对象的引用。 存储有关状态,这些状态应与目标的状态保持一致。 实现Observer的更新接口以使自身状态与目标的状态保持一致
# 四、类图
# 五、示例
Subject
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description Subject
* @github https://github.com/binghe001
* @copyright 公众号: 冰河技术
*/
public abstract class Citizen {
List<Policeman> pols;
String help = "normal";
public void setHelp(String help) {
this.help = help;
}
public String getHelp() {
return this.help;
}
public abstract void sendMessage(String help);
public void setPolicemen() {
this.pols = new ArrayList<>();
}
public void register(Policeman pol) {
this.pols.add(pol);
}
public void unRegister(Policeman pol) {
this.pols.remove(pol);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Observer
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description Observer
* @github https://github.com/binghe001
* @copyright 公众号: 冰河技术
*/
public interface Policeman {
void action(Citizen ci);
}
2
3
4
5
6
7
8
9
10
ConcreteSubject
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description ConcreteSubject
* @github https://github.com/binghe001
* @copyright 公众号: 冰河技术
*/
public class Binghe001Citizen extends Citizen{
public Binghe001Citizen(Policeman pol) {
setPolicemen();
register(pol);
}
@Override
public void sendMessage(String help) {
setHelp(help);
for(int i = 0; i < pols.size(); i++) {
Policeman pol = pols.get(i);
//通知警察行动
pol.action(this);
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description ConcreteSubject
* @github https://github.com/binghe001
* @copyright 公众号: 冰河技术
*/
public class Binghe002Citizen extends Citizen{
public Binghe002Citizen(Policeman pol) {
setPolicemen();
register(pol);
}
@Override
public void sendMessage(String help) {
setHelp(help);
for(int i = 0; i < pols.size(); i++) {
Policeman pol = pols.get(i);
//通知警察行动
pol.action(this);
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ConcreteObserver
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description ConcreteObserver
* @github https://github.com/binghe001
* @copyright 公众号: 冰河技术
*/
public class Binghe001Policeman implements Policeman{
@Override
public void action(Citizen ci) {
String help = ci.getHelp();
if (help.equals("normal")) {
System.out.println("一切正常, 不用出动");
}
if (help.equals("unnormal")) {
System.out.println("有犯罪行为, Binghe001警察出动!");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description ConcreteObserver
* @github https://github.com/binghe001
* @copyright 公众号: 冰河技术
*/
public class Binghe002Policeman implements Policeman{
@Override
public void action(Citizen ci) {
String help = ci.getHelp();
if (help.equals("normal")) {
System.out.println("一切正常, 不用出动");
}
if (help.equals("unnormal")) {
System.out.println("有犯罪行为, Binghe002警察出动!");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Test
/**
* @author binghe(微信 : hacker_binghe)
* @version 1.0.0
* @description 测试类
* @github https://github.com/binghe001
* @copyright 公众号: 冰河技术
*/
public class Test {
public static void main(String[] args) {
Policeman binghe001Policeman = new Binghe001Policeman();
Policeman binghe002Policeman = new Binghe002Policeman();
Citizen citizen = new Binghe001Citizen(binghe001Policeman);
citizen.sendMessage("unnormal");
citizen.sendMessage("normal");
System.out.println("===========");
citizen = new Binghe002Citizen(binghe002Policeman);
citizen.sendMessage("normal");
citizen.sendMessage("unnormal");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Result
有犯罪行为, Binghe001警察出动!
一切正常, 不用出动
===========
一切正常, 不用出动
有犯罪行为, Binghe002警察出动!
2
3
4
5
好了,今天就到这儿吧,相信大家对观察者模式有了更清晰的了解,我是冰河,我们下期见~~
← 第18章:备忘录模式 第20章:状态模式 →
