Observer
Usage
- Also known as: Publisher / Subscriber; Dependents
- A way of notifying change to a number of classes
- Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically
Participants
Subject
ConcreteSubject
- Knows its observers. Any number of Observer objects may observe a subject
- Provides an interface for attaching and detaching Observer objects
Observer
- Stores state of interest to ConcreteObserver
- Sends a notification to its observers when its state changes
ConcreteObserver
- Defines an updating interface for objects that should be notified of changes in a subject
- Maintains a reference to a ConcreteSubject object
- Stores state that should stay consistent with the subject's
- Implements the Observer updating interface to keep its state consistent with the subject's
