What is the Observer design pattern in Java?
Why Interviewers Ask This
This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.
Answer
The Observer pattern defines a one-to-many dependency between objects: when one object (the Subject/Observable) changes state, all registered observers are automatically notified and updated. This enables loose coupling — the subject does not need to know anything about its observers except that they implement a common interface. Java provides java.util.Observable (deprecated in Java 9) and java.util.EventListener. Modern approaches use custom listener interfaces, property change support, or reactive libraries (RxJava, Project Reactor). The pattern is the foundation of event-driven programming, GUI frameworks, and publish-subscribe messaging systems. Spring's ApplicationEventPublisher is a common real-world example.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Java answers easy to follow.