Part 1 of 6 · 1 min read
Why this page comes second
In this module you will
- State the Observer pattern and name the subject and the observers.
- Write a subject with subscribe, unsubscribe and notify in Java.
- Choose between sending a full event and sending only a signal.
- Avoid the two classic bugs: changing the list while notifying, and calling unknown code under a lock.
- Name the main risk: cascades of updates that nobody planned.
Strategy let the caller choose an algorithm. Observer solves a different problem: one object changes, and several others must react, without the first object knowing who they are. Notification systems, event buses and user interface listeners all use it. The sources are "Design Patterns" (GoF) chapter 5, "Head First Design Patterns" (HF) chapter 2, and "Effective Java" (EJ) Item 79. Code was compiled and run before it was written here. Numbers marked "made up" only show the idea.