Observer Pattern

Source: comp.object
Date: 09-Oct-97

Related Sites


------------------------------

o-< Problem: How to keep multiple views of a single object in sync with the object?


---------------

o-< Frank Prindle asked:

In MS Windows 95 or Windows NT, one can easily launch many independently developed applications which each display a slider/scale widget showing the soundcard volume, for example. If the volume is changed by moving any of these sliders/scales, all the other sliders/scales IMMEDIATELY reflect the change.

Is there a commonly used programming method whereby, if an object has visual representations in one or more windows [...], that when the value of that object changes [...], all its visual representations are informed of, and display the new value?


---------------

o-< From the book Design Patterns by Gamma, Helm, Johnson & Vlissides:

The Observer pattern describes how to establish these relationships. The key objects in this patterns are subject and observer. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. In response, each observer will query the subject to synchronize its state with the subject's state.

This kind of interaction is also known as publish-subscribe. The subject is the publisher of notifications. It sends out these notifications without having to know who its observers are. Any number of observers can subscribe to receive notifications.


------------------------------

o-< More Info:

Complete description of the Observer pattern

Another description of the Observer pattern

C++ source code


---------------

o-< Advanced Topics:

John Vlissides, The Trouble with Observer

Constantin Szallies, On Using the Observer Design Pattern

Dirk Riehle, The Event Notification Pattern


------------------------------