Observer pattern: defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
oo principles: strive for loosely coupled designs between objects that interact.
How observer pattern works
let's look at how newspaper or magazine subscriptions work:
1. A newspaper publisher goes into business and begins publishing newspapers.
2. You subscribe to a particular publisher, and every time there's a new edition it gets delivered to you. As long as you remain a subscriber, you get new newspapers
3. You unsubscribe when you don't want papers anymore, and they stop being delivered
4. While the publisher remains in business, people, hotels, airlines and other businesses constantly subscribe and unsubscribe to the newspaper.
Publishers+subscribers = observer pattern
if you understand newspaper subscriptions, you pretty much understand the Observer Pattern, only we call the publisher the
SUBJECT and the subscribers the
OBSERVERS
Illustration
the duck object isn't an observer, it wont get notified when the subject's data changes.
How does the duck object become an observer
1. register/subscribe
2.The duck object is now an official observer
duck is psyched. he's on the list and is waiting with great anticipation for the next notification.
3. the subject gets a new data value
Now duck and all the rest of observers get a notification that the subject has changed
java built-in support of observer pattern
bullet point
* The observer pattern defines a one-to-many relationship between objects
* Subjects, or as we also know them, Observables, update Observers using a common interface
* Observers are loosely coupled in that the Observable knows nothing about them, other than that they implement the Observer interface
* You can push or pull data from Observable when using the pattern(pull is considered more "correct")
* Don't depend on a specific order of notification for your Observers.
* Java has several implementations of the Observer Pattern, including the general purpose java.util.Observable.
* Watch out for issues with the java.util.Observable implementation.
* Don't be afraid to create your own Observable implementation if needed
* Swing makes heavy use of the Observer Pattern. as do many GUI frameworks.
* You'll also find the pattern in many other places, including JavaBeans and RMI
分享到:
相关推荐
设计模式之观察者模式ObserverPattern
我们说学习Java应该从Swing开始,那么学习Swing最重要的思想就是对于观察者模式的理解(Observer Pattern)。因为,该设计模式在Java Swing框架中贯穿了始终。对于C#的委托、代理概念所使用的Callback(回调模式--...
ObserverPattern.unitypackage是一个以unity为例写的观察者模式的应用举例。有需要的同学请下载!
matlab开发-Observerpattern。观察者模式示例
Swift µframework 实现观察者模式Observer pattern
定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。
这是关于C#中的观察者模式的教程文章。我们首先介绍作为GoF模式之一的经典观察者模式。观察者模式通过使用事件机制集成到C#中,这将在接下来讨论。然后,我们讨论与在C#中使用事件/事件处理程序相关的问题。
设计模式之观察者模式和静态工厂的结合实践
例题代码:李先生希望及时知道气象站的天气预报及旅行社的每日旅行信息。 李先生就是具体的一个观察者,而气象站及旅行社就成为了主题。
设计模式之观察者模式和静态工厂的结合实践
NULL 博文链接:https://justcoding.iteye.com/blog/1476165
观察者模式 New's Station Observer Pattern演示
从生活中的例子可以看出,只要对订阅号进行关注的客户端,如果订阅号有什么更新,就会直接推送给订阅了的用户。从中,我们就可以得出观察者模式的定义。 观察者模式定义了一种一对多的依赖关系,让多个观察者对象...
主要介绍了C#观察者模式(Observer Pattern),主要以一个实例的形式讲述了C#观察者模式的实现过程,详细讲述了接口的定义、通知及动作的实现方法,需要的朋友可以参考下
组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的...
主要介绍了JavaScript编程设计模式之观察者模式(Observer Pattern),简单说明了观察者模式的概念、原理并结合实例形式详细给出了观察者模式的相关实现与使用技巧,需要的朋友可以参考下
在设计一组依赖的对象与它们所依赖的对象之间一致(同步)的交流模型时,观察者模式(Observer Pattern)很有用。它可以使依赖对象的状态与它们所依赖的对象的状态保持同步。这组依赖的对象指的是观察者(Observer)...
1、策略模式STRATEGY PATTERN ...16、观察者模式OBSERVER PATTERN 17、责任链模式 18、访问者模式VISITOR PATTERN 19、状态模式 20、原型模式 21、中介者模式 22、解释器模式 23、亨元模式 24、备忘录模式