Lesson 45: Design Pattern: Observer
15 Aug 2026 1 min Swarnil Singhai
Observer lets objects subscribe to events on another object — the foundation of event-driven systems and GUI/reactive frameworks.
Real-time example
A stock-price ticker notifies every subscribed dashboard widget the instant a price changes, without knowing what those widgets are.
interface PriceListener { void onPriceChange(double newPrice); }
class StockTicker {
private List<PriceListener> listeners = new ArrayList<>();
void subscribe(PriceListener l) { listeners.add(l); }
void updatePrice(double price) {
for (PriceListener l : listeners) l.onPriceChange(price);
}
}
StockTicker ticker = new StockTicker();
ticker.subscribe(price -> System.out.println("Dashboard sees: " + price));
ticker.updatePrice(142.50);
What's happening
StockTicker never imports or knows about any specific dashboard class — it only depends on the PriceListener interface.
This decoupling is exactly what makes event systems (GUI clicks, message queues, webhooks) extensible without modifying the publisher.
Advertisement