The Observer pattern is also known as Dependents and Publish-Subscribe.
An example of using the observer pattern is the graphical interface toolkit which separates the presentational aspect with application data. The presentation aspect is the observer part and the application data aspect is the subject part.
In a spreadsheet program, the Observer pattern can be applied as in the following diagram. Each rectangular box in the diagram in an object. SpreadSheetFormula, BarGraph, and PieChart are the observer objects. SpreadsheetData is the subject object. The SpreadsheetData object notifies its observers whenever a data changes that could make it's state inconsistent with the observers.
| Subject |
|
Send notify signal to observer object whenever data changes |
| Observer |
|
Request subject for change information in order to update itself accordingly |
Singleton : Ensure a class only has one instance, and provide a global point of access to it.

Observing more than one subject. It might make sense to implement many-to-many relationship between subject and observer. The Update interface in observer has to know which subject is sending the notification. One of the implement is that subject can pass itself as a parameter in the Update operation.
Who triggers the update (Notify operation in Subject).
Making sure subject state is self-consistent before notification. Otherwise, an observer can query subject's intermediate state through GetState operation.
Avoiding observer-specific update protocols: push and pull models.
Encapsulating complex update semantics. For any complex set of subject and observer relationships, one can implement Change Manage to handle their Update operation. For example, if multiple subjects have to change state before any of their observers can update. Change Manager can handle change and update sequence for the operation.
Subject() is the constructor. It is not used in our implementation and this helps to keep the code generic. However, there are possibilities to use it for some applications.
~Subject() is the distructor. It is not used in our implementation and this helps to keep the code generic. However, there are possibilities to use it for some applications.
void Attach(Observer*) establishes the relationship between Subject and Observer by attaching the input Observer to the _observers list.
void Detach(Observer*) terminates the relationship between Subject and Observer by removing the specified Observer from the _observers list.
vector<Observer*> _observers declares an array of Observer type pointers.
Observer() is the constructor. It is not used in our implementation and this helps to keep the code generic. However, there are possibilities to use it for some applications.
~Observer() is the distructor. It is not used in our implementation and this helps to keep the code generic. However, there are possibilities to use it for some applications.
virtual void Update(Subject *theChangeSubject) = 0 defines the function Update() that interfaces with the Subject class. This function is called by object of the type Subject.
The ConcreteSubjectClass needs to implement functions for its observer objects to query subject state after sending the notify signal.
It needs function to maintain its internal state. Whenever it has a state change, it has to notify all of its observers.
ConcreteObserver(ConcreteSubject *) establishes the relationship with the input subject by using the Subject class function Attach()
~ConcreteObserver() terminates the relationship with the subject by using the Subject class function Detach()
void Update(Subject *) updates the object's internal state to synchronize with its subject. It uses member functions in the subject object to find out the subject's current state. This Update() function is called by the subject object.
ConcreteSubject *_subject maintains a reference to its subject.
| Observer Pattern
Class name |
Clock Program
Class Name |
C++ file |
| Subject | Subject | observer.cpp
observer.h |
| ConcreteSubject | ClockTimer | ClockTimer.cpp
ClockTimer.h |
| Observer | Observer | observer.cpp
observer.h |
| ConcreteObserver | AnalogClock
DigitalClock |
AnalogClock.cpp
AnalogClock.h DigitalClock.cpp DigitalClock.h |
Both the Subject class and the Observer class are written in a very generic way and they can be reused in other application without modification.
The ConcreteSubject class and the ConcreteObserver class contain application specific code. However, their class structure can still be reused with modification.
The clock program is written in MS-Visual C++ 4.0. For demo. purpose,
both the AnalogClock and the DigitalClock only prints out time in ASCII
when they receives the notify signal from the ClockTimer. The following
are the C++ sources filenames.
Buschmann, F., Meunier, R., Rohnert, H., Sommerlad, P. & Stal, M. (1996). A System of Patterns: Pattern-Oriented Software Architecture. West Sussex, England, John Wiley & Sons.