观察者模式 The Observer Pattern
2013-03-29 16:42
162 查看
观察者模式:
在对象之间定义一对多的依赖。这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新。
设计原则:
4、为交互对象之间的松耦合设计而努力。
示例类图:
以《Head First Design Patterns》中气象站为例。
1、所有依赖主题的观察者都应该实现Observer接口,这样主题状态信息发生改变,需要通知观察者时,就有了一个共同的接口。
2、可以使用推(push)或拉(pull)的方式传送数据。
通过update接口传入主题的引用;
在Observer对象构造时,通过构造函数传入主题引用;
把主题的信息以参数的形式直接通过update接口传入;
(转载请注明出处 ^.^)
在对象之间定义一对多的依赖。这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新。
设计原则:
4、为交互对象之间的松耦合设计而努力。
示例类图:
以《Head First Design Patterns》中气象站为例。
1、所有依赖主题的观察者都应该实现Observer接口,这样主题状态信息发生改变,需要通知观察者时,就有了一个共同的接口。
2、可以使用推(push)或拉(pull)的方式传送数据。
通过update接口传入主题的引用;
在Observer对象构造时,通过构造函数传入主题引用;
把主题的信息以参数的形式直接通过update接口传入;
(转载请注明出处 ^.^)
相关文章推荐
- 设计模式之 观察者模式 the observer design pattern ---------《head first design pattern》
- 观察者模式 The Observer Pattern
- 设计模式之 观察者模式 the observer design pattern
- 设计模式(二)The Observer Pattern 观察者模式
- 设计模式(二)The Observer Pattern 观察者模式
- 观察者模式(Observer Pattern)
- C#设计模式——观察者模式(Observer Pattern)1
- 【设计模式】设计模式C++编程实现之观察者模式(ObserverPattern)
- C#设计模式(17)——观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 我所理解的设计模式(C++实现)——观察者模式(Observer Pattern)
- 观察者模式(Observer Pattern)
- observer pattern--观察者模式入门
- .NET设计模式(19):观察者模式(Observer Pattern)
- Head First 设计模式——观察者模式(Observer Pattern)
- Js-JavaScript 观察者设计模式 Observer Pattern
- c#设计模式系列:观察者模式(Observer Pattern)
- 观察者模式(Observer Pattern)
- 第 16 章 观察者模式【Observer Pattern】
- C#设计模式——观察者模式(Observer Pattern)