Presentation is loading. Please wait.

Presentation is loading. Please wait.

Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.

Similar presentations


Presentation on theme: "Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency."— Presentation transcript:

1 Observer Pattern Tu Nguyen

2 General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency between related objects without tightly coupling classes e.g. “reduces coupling between objects” “publish and subscription” services eBay

3 Key Players Subject Knows its observers – provides interface for attaching/detaching subjects Observer Defines an interface for notifying the subjects of changes to the object (ex. Data) ConcreteSubject Sends notification to observers when state changes by storing state to ConcreteObserver object ConcreteObserver Implements Observer interface to keep state consistent with subject

4 Observer UML Taken from Design Patterns ©

5 Consequences Abstract coupling between subject and observer Coupling is abstract, thus minimal (concrete class isn’t known) Can have multiple layers of abstraction Support for broadcast communication Subject doesn’t need to know its receivers Unexpected updates Can be blind to changes in the system if the subject is changed (i.e. doesn’t know “what” has changed in the subject)

6 Real-World Example “General Broadcast” Observers “tuning in” to the notification

7 CNN UML Model

8 Safeguards Mapping subjects to observers (subjects store references) Have state-setting operations of notifications of updates May be expensive and inefficient Make clients notify of changes at the right time Clients have an added responsibility and forget Dangling references to deleted subjects should be avoided Subject state should be self-consistent before notification Avoid observer-specific update protocols Push – subject sends observers detail at will Pull – observers ask for detail after notification is sent Specify modifications of interest explicity Encapsulate complex update semantics

9 Data Example XML Web Browser PDA Cell Phone Terminal Observers Subject xyz… Interface Browser PDA Cell Phone Terminal Data is sent to the various observers

10 Related Patterns Mediator – an object in the middle intercepts all update requests and broadcasts them to other objects Singleton – A “single” subject object instance can be implemented with a Singleton Decorator – an intermediate observer can be viewed as a subject’s decorator Proxy – observer do not interact directly with subject and can hide the way the subject is acceded

11 Code Example public interface Subject { public void addObserver( Observer o ); public void removeObserver( Observer o ); } public interface Observer { public void update( Subject o ); } Concept: jobserver_example.docjobserver_example.doc


Download ppt "Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency."

Similar presentations


Ads by Google