Applied Design Patterns with Java
Behavioral :: Observer (293) {C ch 22}
Applicability
Use the Observer pattern in any of the following situations:
- When an abstraction has two aspects, one dependent on
the other. Encapsulating these aspects in separate objects allows varying and reusing them independently.
- When a change to one object requires changing others,
and how many objects need to be changed is not known in advance.
- When an object should be able to notify other objects
without making assumptions about who these objects are. That is, these objects should NOT be tightly coupled.
Structure
Participants
- Subject
knows its Observers. Any number of
Observer objects may observe a subject. The Subject
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.
- ConcreteSubject
stores state of interest to ConcreteObserver objects, and it sends a notification to its Observers when its state changes.
- ConcreteObserver maintains a reference to a ConcreteSubject object, it stores a state that should stay
consistent with the Subject's, and it implements the Observer updating interface to keep its state consistent with the Subject's.
Catalog Behavioral Prev Next