Observer Pattern: Difference between revisions
Line 8: | Line 8: | ||
The Observer pattern provides two ways to get the new state from the subject to the observer: | The Observer pattern provides two ways to get the new state from the subject to the observer: | ||
* push model - the | * push model - the update() call on each observer contains the new state. | ||
* pull model - the subject sends basic notification and each observer requests the new state from the subject. | * pull model - the subject sends basic notification and each observer requests the new state from the subject. |
Revision as of 23:08, 8 January 2018
Internal
Overview
The Observer pattern documents a design through which an object can notify its dependents of a change, while keeping that object decoupled from its dependents do that the object works just fine no matter how many dependents it has, even if it has none at all. Its participants are a Subject - the object announcing changes in its state - and Observers - objects interested in receiving notification of changes in the Subject. When a subject's state changes, it calls its notify() method, whose implementation knows the list of observers and calls update() on each of them. Some observers may not be interested in this state change, but those that are can find out what the new state is by calling getState() on the subject. The subject must also implement attach(Observer) and detach(Observer) mehtods that the observers use to register interest.
The Observer pattern provides two ways to get the new state from the subject to the observer:
- push model - the update() call on each observer contains the new state.
- pull model - the subject sends basic notification and each observer requests the new state from the subject.