Learn the vocabulary of automatically notifying registered observers whenever a subject's state changes.
0 / 5 completed
1 / 5
At standup, a dev mentions letting objects called observers register interest in a subject, so that whenever the subject's state changes, every registered observer is automatically notified without the subject needing to know any observer's concrete type. What is this design pattern called?
The observer pattern is exactly this: the observer pattern lets objects called observers register interest in a subject, so that whenever the subject's state changes, every registered observer is automatically notified through a common interface, without the subject needing to know any observer's concrete type. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This register-and-auto-notify approach is exactly why the observer pattern decouples a subject from the specific things that need to react to its changes.
2 / 5
During a design review, the team applies the observer pattern to a subject that many different parts of the system need to react to, specifically because notifying registered observers through a common interface avoids the subject needing to know about every specific thing that reacts to it. Which capability does this provide?
The observer pattern here provides Decoupled, one-to-many notification of state changes, since the subject only needs to notify observers through a common interface, and new observers can be added later without ever modifying the subject. A subject that calls every specific reacting piece of code directly by name must be edited every time a new piece of reacting code is added. This decoupled, add-observers-without-touching-the-subject behavior is exactly why the observer pattern is favored whenever many different parts of a system need to react to one subject's changes.
3 / 5
In a code review, a dev notices a subject with several unrelated pieces of code that need to react to its state changes calls each reacting piece of code directly by name inside its own method, instead of notifying registered observers through a common interface. What does this represent?
This is a missed observer-pattern opportunity, since notifying registered observers through a common interface would let new reacting code be added without ever modifying the subject, instead of the subject calling every reacting piece of code directly by name. A cache eviction policy is an unrelated concept about discarded cache entries. This call-reacting-code-directly pattern is exactly the kind of tight coupling a reviewer flags once several unrelated pieces of code need to react to the same subject.
4 / 5
An incident report shows adding one new piece of reacting code required modifying a subject's own method directly, breaking an unrelated existing reaction that was hardcoded in the same method, because the subject called every reacting piece of code directly by name instead of notifying registered observers. What practice would prevent this?
Introducing the observer pattern lets new reacting code register as an observer instead of being hardcoded directly inside the subject, avoiding interference between unrelated reactions. Continuing to call every reacting piece of code directly by name inside the subject's own method regardless of how many unrelated pieces of reacting code need to react to it is exactly what caused the issue described in this incident. This observer-based approach is the standard fix once several unrelated pieces of code are confirmed to react to the same subject.
5 / 5
During a PR review, a teammate asks why the team reaches for the observer pattern instead of simply calling each reacting piece of code directly from inside the subject whenever its state changes. What is the reasoning?
The observer pattern notifies registered observers through a common interface, so new reactions can be added without modifying the subject, while calling reacting code directly is simpler for a single, fixed reaction but requires editing the subject every time a new reaction is added. This is exactly why the observer pattern is favored once multiple, evolving reactions to a subject are expected, while direct calls remain simpler for one fixed, unchanging reaction.