一模式定义
观察者模式,又称为发布/订阅模式。观察者模式定义了对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
二模式举例
1 模式分析
我们借用气象局高温预警来说明这一模式。
2观察者模式静态类图
3代码示例
3.1观察者接口一IObserver
- package com.demo.observer;
-
- import com.demo.subject.ISubject;
- /**
- * 观察者接口
- * @author
- *
- */
- public interface IObserver
- {
- //更新方法
- public void update(ISubject subject);
- }
3.2主题接口一ISubject
- package com.demo.subject;
-
- import com.demo.observer.IObserver;
-
- /**
- * 主题接口(被观察者)
- *
- * @author
- *
- */
- public interface ISubject
- {
- /**
- * 增加观察者
- *
- * @param observer
- * @return
- */
- public boolean add(IObserver observer);
-
- /**
- * 删除观察者
- *
- * @param observer
- * @return
- */
- public boolean remove(IObserver observer);
-
- /**
- * 通知所有观察者更新数据
- */
- public void notifyAllObserver();
-
- /**
- * 设置温度值
- *
- * @param temperature
- */
- public void setTemperature(float temperature);
-
- /**
- * 获得温度预警
- *
- * @return
- */
- public String temperatureReport();
- }
3.3具体主题实现一Subject
3.4个人观察者一PersonObserver
- package com.demo.observer;
-
- import com.demo.subject.ISubject;
- /**
- * 个人用户观察者
- * @author
- *
- */
- public class PersonObserver implements IObserver
- {
-
- public void update(ISubject subject)
- {
- System.out.println("个人收到高温预警:" + subject.temperatureReport());
- }
-
- }
3.5政府观察者一GovernmentObserver
- package com.demo.observer;
-
- import com.demo.subject.ISubject;
- /**
- * 政府用户观察者
- * @author
- *
- */
- public class GovernmentObserver implements IObserver
- {
-
- public void update(ISubject subject)
- {
- System.out.println("政府部门收到高温预警:" + subject.temperatureReport());
- }
-
- }
3.6企事业单位观察者一CompanyObserver
- package com.demo.observer;
-
- import com.demo.subject.ISubject;
- /**
- * 企事业单位用户观察者
- * @author
- *
- */
- public class CompanyObserver implements IObserver
- {
-
- public void update(ISubject subject)
- {
- System.out.println("企事业单位收到高温预警:" + subject.temperatureReport());
-
- }
-
- }
3.7让系统开始运行一Client
- package com.demo;
-
- import java.util.Random;
-
- import com.demo.observer.CompanyObserver;
- import com.demo.observer.GovernmentObserver;
- import com.demo.observer.PersonObserver;
- import com.demo.subject.ISubject;
- import com.demo.subject.Subject;
-
- /**
- * 客户端应用
- *
- * @author
- *
- */
- public class Client {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 创建主题对象
- ISubject subject = new Subject();
-
- // 增加企事业单位观察者
- subject.add(new CompanyObserver());
- // 增加政府用户观察者
- subject.add(new GovernmentObserver());
- // 增加个人用户观察者
- subject.add(new PersonObserver());
-
- Random random = new Random();
- int i = 0;
- while (++i < 10) {
- // 设置随机温度
- subject.setTemperature(random.nextInt(45));
- }
- }
- }
4运行结果
======气象部门发布高温黄色警报!======
企事业单位收到高温预警: 温度:35.0
政府部门收到高温预警: 温度:35.0
个人收到高温预警: 温度:35.0
======气象部门发布高温红色警报!======
企事业单位收到高温预警: 温度:43.0
政府部门收到高温预警: 温度:43.0
个人收到高温预警: 温度:43.0
======气象部门发布高温橙色警报!======
企事业单位收到高温预警: 温度:37.0
政府部门收到高温预警: 温度:37.0
个人收到高温预警: 温度:37.0
三该模式设计原则
1"开——闭"原则
2单一职责原则
3依赖倒置原则
四使用场合
1当一个抽象模型有两个方面,其中一个方面依赖于另一个方面,需要将这两个方面分别封装到独立对象中,彼此独立地改变和复用的时候。
2当一个系统中一个对象的改变需要同时改变其他对象内容,但又不知道待改变对象到底有多少个的时候。
3当一个对象的改变必须通知其他对象做出相应的变化,但是不能确定通知对象是谁的时候。
五"推数据"静态类图
所谓"推数据",就是当被观察对象发生改变时,将相关数据通过参数形式传递给观察者,这就形成了被观察者"推数据"给观察者,静态类图如下:
六"拉数据"静态类图
所谓"拉数据",观察者对象含有一个对被观察者对象实例的引用,当被观察者对象发生变化时,不会传递任何数据给观察者,而由观察者根据被观察者对象实例的引用主动获取相关的数据,这就形成了观察者主动从被观察对象中"拉数据“,静态类图如下:
|