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/13Knowledge Engineering Lab2 Outline Intent Motivation Applicability Structure Consequence Implementation
2006/6/13Knowledge Engineering Lab3 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/13Knowledge Engineering Lab4 Motivation
2006/6/13Knowledge Engineering Lab5 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/13Knowledge Engineering Lab6 Motivation Describes how to establish these relationships The key objects : –Subject and observer in the Observer pattern
2006/6/13Knowledge Engineering Lab7 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/13Knowledge Engineering Lab8 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/13Knowledge Engineering Lab9 Structure
2006/6/13Knowledge Engineering Lab10 Structure
2006/6/13Knowledge Engineering Lab11 Demo example
2006/6/13Knowledge Engineering Lab12 Consequences Reuse subjects (observers) without reusing their observers (subjects) Add observers without modifying the subject or other observers
2006/6/13Knowledge Engineering Lab13 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/13Knowledge Engineering Lab14 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 Notify its observers to reset their reference to it
2006/6/13Knowledge Engineering Lab15 Implementation 3.Who triggers the update? a.Have state-setting operations on Subject call Notify after they change the subject's state Advantage : Clients don't have to remember to call Notify on the subject Disadvantage : Cause several consecutive updates, which may be inefficient
2006/6/13Knowledge Engineering Lab16 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/13Knowledge Engineering Lab17 Implementation The push model : –The subject sends all changed data when it notifies the observers –Assumes subjects know something about their observers' needs The pull model : –The subject only sends minimal information when sending a change notification –Emphasizes the subject's ignorance of its observers
2006/6/13Knowledge Engineering Lab18 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/13Knowledge Engineering Lab19 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/13Knowledge Engineering Lab20 Thanks!