Definition
“Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically”Explanation
One object (Subject) maintains a list of observers and sends updates to them when its state changes. Observers implement a common interface and register with the subject. When the subject’s state updates (e.g. data model change), it callsnotifyObservers(), causing each observer’s update method to run. This decouples subjects and observers.
Code
In this Python example,ConcreteSubject maintains a list of observers. Whenever its internal state changes
(some_business_logic), it calls notify(), which in turn calls update() on each attached observer (ConcreteObserverA,
ConcreteObserverB).
Analogy
A news agency and subscribers. When breaking news happens (subject state change), the agency automatically pushes the news to all registered subscriber clients. Or a magazine subscription: one paper, many readers who get the new issue when released.Interview Insights
Use Case: Event-driven systems (UI event listeners), data binding (e.g. MVC frameworks), messaging systems (pub/sub ), caching (invalidate caches when data changes). Useful when changes must propagate to many objects (e.g. model–view synchronization).Advantages: Promotes loose coupling: subjects don’t need to know concrete observer classes. Makes it easy to add new observers at runtime. Simplifies broadcast of events
Disadvantages: Can lead to memory leaks if observers aren’t unsubscribed. Notification order is undefined. If many observers, notifications can slow down updates. Notification errors in one observer could affect others.
Common questions: “How do you prevent or remove observers (memory leak)?”, “How is this pattern implemented in your language (e.g. event listeners in Java/.NET)?”, “Difference between Observer and Mediator?”, “How to handle threading (e.g. asynchronous events)?”.\