What is the Observer Pattern?
Learn the Observer design pattern: one-to-many notification, subject-observer decoupling, and how it powers event-driven systems.
Expected Interview Answer
The Observer pattern defines a one-to-many dependency where a subject notifies a list of registered observers automatically whenever its state changes.
The subject exposes methods to subscribe and unsubscribe observers, and maintains a collection of them without knowing their concrete types, only that they implement an observer interface with an update method. When the subject’s state changes, it iterates the collection and calls update() on each observer, letting each one react independently. This decouples the subject from the specific logic that runs on change, so new observer types can be added without modifying the subject, which is the basis for event systems, UI data binding, and publish-subscribe messaging.
- Decouples state changes from reaction logic
- New observers can be added without touching the subject
- Supports broadcast-style one-to-many notification
- Foundation for event-driven and reactive architectures
AI Mentor Explanation
A stadium’s scoreboard system is the subject, and every fan’s phone app that subscribed for score alerts is an observer. The moment a wicket falls, the scoreboard pushes an update, and every subscribed app reacts independently — one shows a push notification, another updates a live widget — without the scoreboard needing to know how each app displays it. That decoupled one-to-many notification, triggered by a single state change, is exactly the Observer pattern.
Step-by-Step Explanation
Step 1
Define the observer interface
Declare an update() method that all observers must implement.
Step 2
Build the subject
Give it a list of observers plus subscribe()/unsubscribe() methods.
Step 3
Trigger notification on state change
Whenever internal state changes, iterate the list and call update() on each observer.
Step 4
Register concrete observers
Instantiate observer implementations and subscribe them to the subject at runtime.
What Interviewer Expects
- A clear one-to-many relationship description
- Mentioning subscribe/unsubscribe and the observer interface
- Real-world usage: event listeners, pub-sub, UI data binding
- Awareness of pitfalls like memory leaks from unremoved observers
Common Mistakes
- Confusing Observer with the Mediator pattern
- Forgetting to provide an unsubscribe mechanism, causing memory leaks
- Not mentioning that the subject shouldn’t know observer concrete types
- Giving no real-world example (event listeners, pub-sub)
Best Answer (HR Friendly)
“The Observer pattern is how one object automatically tells a group of other objects whenever something changes, without needing to know the details of who they are or what they do with that information. It is the same idea behind subscribing to notifications — you get pinged when the state you care about changes.”
Code Example
interface Observer { void update(int newState); }
class Subject {
private final List<Observer> observers = new ArrayList<>();
private int state;
void subscribe(Observer o) { observers.add(o); }
void unsubscribe(Observer o) { observers.remove(o); }
void setState(int newState) {
this.state = newState;
for (Observer o : observers) {
o.update(newState);
}
}
}
class LoggingObserver implements Observer {
public void update(int newState) {
System.out.println("State changed to " + newState);
}
}Follow-up Questions
- How does the Observer pattern differ from the Mediator pattern?
- How can unsubscribed observers cause memory leaks?
- How does Observer relate to publish-subscribe messaging systems?
- How would you make the notification thread-safe?
MCQ Practice
1. The Observer pattern establishes what kind of relationship?
One subject broadcasts state changes to many registered observers.
2. What must every observer implement?
Observers implement a common interface, typically with an update() method the subject calls.
3. A common bug in Observer implementations is?
If observers are never removed, the subject holds references to them indefinitely, leaking memory.
Flash Cards
What is the Observer pattern? — A behavioral pattern where a subject notifies many registered observers automatically on state change.
What interface do observers implement? — A common observer interface, typically with an update() method.
Name a real-world use of Observer. — Event listeners, publish-subscribe systems, UI data binding.
What is a common pitfall? — Forgetting to unsubscribe observers, which causes memory leaks.