Observer Pattern

The Observer pattern (also called publisher/subscriber) is useful for designing a communication model between a set of dependent objects(observers) and an object that they are dependent on(subject) allowing observers to synchronize their state with the subject.

Any object with interest in the state of the subject needs to explicitly register/subscribe itself as an observer with the subject.

Whenever the subject undergoes a change in its state, it notifies all of its registered observers so that they can synchronize. Subject-observer relation can be 1-m or m-m.

How to implement:
1- The subject should provide an interface for registering and unregistering observers.
2- The subject should provide an interface that enables observers to query the subject for state (pull model) OR the subject should send the state information that the observers may be interested in (push model)
3- Observers should provide an interface for receiving notifications from the subject.

observer.jpeg
page_revision: 5, last_edited: 1229332891|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License