250 likes | 306 Views
Learn how the Observer design pattern establishes dependencies between objects, enabling automatic updates and notifications. Understand its structure, applicability, benefits, and implementation methods. Explore the push and pull models for efficient observation management.
E N D
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw Chang
Outline • Intent • Motivation • Applicability • Structure • Consequence • Implementation Knowledge Engineering Lab
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. Knowledge Engineering Lab
Motivation Knowledge Engineering Lab
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 Knowledge Engineering Lab
Motivation • Describes how to establish these relationships • The key objects: • Subject and observer in the Observer pattern Knowledge Engineering Lab
Motivation • A subject may have any number of dependent observers • All observers are notified whenever the subject changed its state • Each observer will query the subject to make its state the same with the subject's state Knowledge Engineering Lab
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 . Knowledge Engineering Lab
Structure Knowledge Engineering Lab
Structure Knowledge Engineering Lab
Demo example Knowledge Engineering Lab
Consequences • Reuse subjects (observers) without reusing their observers (subjects) • Add observers without modifying the subject or other observers Knowledge Engineering Lab
Consequences • Further benefits and liabilities : • Abstract coupling between Subject and Observer. • Support for broadcast communication • The notification is broadcast automatically to all interested objects • Up to the observer to handle or ignore a notification • Unexpected updates • The simple update protocol provides no details on whatchanged in the subject Knowledge Engineering Lab
Implementation • 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 • Dangling references to deleted subjects • Notify its observers to reset their reference to it Knowledge Engineering Lab
Implementation • Who triggers the update? • 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 Knowledge Engineering Lab
Implementation • Who triggers the update? • Make clients responsible for calling Notify at the right time • Advantage :Avoiding needless intermediate updates • Disadvantage :Clients might forget to call Notify • Avoiding observer-specific update protocols: the push and pull models Knowledge Engineering Lab
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 Knowledge Engineering Lab
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 Knowledge Engineering Lab
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 Knowledge Engineering Lab
Thanks! Knowledge Engineering Lab