1 / 10

Observer Pattern

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 a.k.a Dependence mechanism / publish-subscribe / broadcast / change-update. Subject & Observer. Subject

calvin
Download Presentation

Observer Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 • a.k.a Dependence mechanism / publish-subscribe / broadcast / change-update Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  2. Subject & Observer • Subject • the object which will frequently change its state and upon which other objects depend • Observer • the object which depends on a subject and updates according to its subject's state. Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  3. b a c a b c x 60 30 10 y 50 30 20 z 80 10 10 Observer Pattern - Example Observers a b c a = 50% b = 30% c = 20% Subject requests, modifications change notification Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  4. Observer Pattern - Working A number of Observers “register” to receive notifications of changes to the Subject. Observers are not aware of the presence of each other. When a certain event or “change” in Subject occurs, all Observers are “notified’. Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  5. Observer Pattern - Key Players • Subject • has a list of observers • Interfaces for attaching/detaching an observer • Observer • An updating interface for objects that gets notified of changes in a subject • ConcreteSubject • Stores “state of interest” to observers • Sends notification when state changes • ConcreteObserver • Implements updating interface Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  6. observers Observer Update() Subject For all x in observers{ x.Update(); } attach (Observer) detach (Observer) Notify () ConcreteObserver ConcreteSubject subject Update() SetState() GetState() observerState subjectState observerState= subject.getState(); Observer Pattern - UML Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  7. Observer Pattern - UML observers Subject Observer Attach(Observer) Detach(Observer)Notify() Update() for all o in observers { o -> Update()} ConcreteObserver observerState = subject->GetState() Update() subject ConcreteSubject observerState SetState() GetState() return subjectState subjectState Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  8. :ConcreteSubject :ConcreteObserver-1 :ConcreteObserver-2 SetState() Notify() Update() GetState() Update() GetState() Observer Pattern - Collaborations Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  9. Java terminology for Subject. Observer Pattern - Implementation interface Observer { void update (Observable sub, Object arg) // repaint the pi-chart } class Observable { … } public void addObserver(Observer o) {} public void deleteObserver (Observer o) {} public void notifyObservers(Object arg) {} public boolean hasChanged() {} Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

  10. Observer Pattern - Consequences • Loosely Coupled • Reuse subjects without reusing their observers, and vice versa • Add observers without modifying the subject or other observers • Abstract coupling between subject and observer • Concrete class of none of the observers is known • 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) Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09

More Related