When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
When a change to one object requires changing others, and you don't know how many objects need to be changed.
When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.
Subject - knows its observers. Any number of Observer objects may observe a subject. Also provides an interface for attaching and detaching Observer objects.
Observer - defines an updating interface for objects that should be notified of changes in a subject.
Concrete Subject - stores state of interest to Concrete Observer objects. Also sends a notification to its observers when its state changes.
Concrete Observer - maintains a reference to a Concrete Subject object. Stores state that should stay consistent with the subject's. Plus implements the Observer's interface for updates in order to keep its state consistent with the subject's.
ConcreteSubject notifies its observers whenever a change occurs that could make its observers' state inconsistent with its own.
After being informed of a change in the concrete subject, a ConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the subject.
The following Sequence Diagram illustrates the collaborations between a subject and two observers:
Notice the Observer that initiates the change request postpones its update until it gets a notification from the subject. Notify is not always called by the subject. It can be called by an observer or by another kind of object entirely.
The Observer pattern lets you vary subjects and observers independently. You can reuse subjects without reusing their observers, and vice versa. It lets you add observers without modifying the subject or other observers. See the Gamma And Helm book for common Observer variations and important implementation concerns.