The Observer Design Pattern Author Erich Gamma et
The Observer Design Pattern Author : Erich Gamma, et al. Source : Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw Chang 2006/6/13 Knowledge Engineering Lab
Outline • • • 2006/6/13 Intent Motivation Applicability Structure Consequence Implementation Knowledge Engineering Lab 2
Intent • The Observer Pattern: – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 2006/6/13 Knowledge Engineering Lab 3
Motivation 2006/6/13 Knowledge Engineering Lab 4
Motivation • Data changes in one place, but many other components depend on this data • Separate the presentational aspects from the application data • Application data and presentations can be reused independently 2006/6/13 Knowledge Engineering Lab 5
Motivation • • Describes how to establish these relationships The key objects: – Subject and observer in the Observer pattern 2006/6/13 Knowledge Engineering Lab 6
Motivation 1. A subject may have any number of dependent observers 2. All observers are notified whenever the subject changed its state 3. Each observer will query the subject to make its state the same with the subject's state 2006/6/13 Knowledge Engineering Lab 7
Applicability • When an abstraction has two aspects, one dependent on the other. • When a change to one object requires changing others, and you don't know how many objects need to be changed. • When an object should notify other objects , and you don’t know who these objects are. 2006/6/13 Knowledge Engineering Lab 8
Structure 2006/6/13 Knowledge Engineering Lab 9
Structure 2006/6/13 Knowledge Engineering Lab 10
Demo example 2006/6/13 Knowledge Engineering Lab 11
Consequences • Reuse subjects (observers) without reusing their observers (subjects) • Add observers without modifying the subject or other observers 2006/6/13 Knowledge Engineering Lab 12
Consequences • Further benefits and liabilities : 1. Abstract coupling between Subject and Observer. 2. Support for broadcast communication – The notification is broadcast automatically to all interested objects – Up to the observer to handle or ignore a notification 3. Unexpected updates – The simple update protocol provides no details on what changed in the subject 2006/6/13 Knowledge Engineering Lab 13
Implementation 1. Observing more than one subject. • • Extend the Update interface to let the observer know which subject is sending the notification The subject can pass itself as a parameter 2. Dangling references to deleted subjects • 2006/6/13 Notify its observers to reset their reference to it Knowledge Engineering Lab 14
Implementation 3. Who triggers the update? a. Have state-setting operations on Subject call Notify after they change the subject's state • • 2006/6/13 Advantage : Clients don't have to remember to call Notify on the subject Disadvantage : Cause several consecutive updates, which may be inefficient Knowledge Engineering Lab 15
Implementation 3. Who triggers the update? b. Make clients responsible for calling Notify at the right time • • Advantage : Avoiding needless intermediate updates Disadvantage : Clients might forget to call Notify 4. Avoiding observer-specific update protocols: the push and pull models 2006/6/13 Knowledge Engineering Lab 16
Implementation • The push model: – – • The pull model: – – 2006/6/13 The subject sends all changed data when it notifies the observers Assumes subjects know something about their observers' needs The subject only sends minimal information when sending a change notification Emphasizes the subject's ignorance of its observers Knowledge Engineering Lab 17
Implementation • The push model: – Less reusable • Assumptions might not always be true ü When the observers need the subject information most of the time • The pull model: – Inefficient • Observer classes must ascertain what changed without help from the Subject. ü When observers can decide if and when they need a specific piece of information 2006/6/13 Knowledge Engineering Lab 18
The Publisher-Subscriber Pattern • Source:Pattern-Oriented Software Architecture • The Publisher-Subscriber Pattern: – One publisher notifies any number of subscribers about changes to its state. • The key objects: – Publisher and subscriber in the Publisher -Subscriber pattern 2006/6/13 Knowledge Engineering Lab 19
Thanks! 2006/6/13 Knowledge Engineering Lab 20
- Slides: 20