Observer – defines a ont-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- Strive for loosely coupled designs between objects that interact.
- The Observer Pattern defines a ont-to-many relationship between objects.
- Subjects, or we also known them, Observables, update Observers using a common interface.
- Observers are loosely coupled in that the observalbe knows nothing about them, other than that they implement the Observer interface.
- You can push or pull the data of the Observalbe when using the pattern(pull is considered more “correct”).
- Don’t depend on a specific order of notification for you Observers.
- Java has several implementations of the Observer Pattern, including the gerneral purpose jave.util.Observable.
- Watch out for issues with the java.util.Observable implementation.
- Don’t be afraid to create our own Observable implementation if needed.
- Swing makes heavy use of the Observer Pattern, as do many GUI frameworks.
- You’ll also find the pattern in many other places, incluing JavaBeans and RMI.