Observer
The Observer pattern lets one thing automatically ping a whole crowd the moment something changes — just like a YouTube channel alerting its fans. Hit subscribe once, and every new video shows up for you without you ever refreshing the page. The channel does not chase each fan one by one; it just posts, and everyone who signed up hears about it. Here is the Observer pattern with a real world example — the idea first, then the code that wires it up.
What is it
The Observer pattern connects one busy thing — call it the channel — to any number of watchers who want to know when it changes. The watchers sign up once. From then on, whenever the channel has news, it goes down its list and tells every watcher, one after another. Think of a YouTube channel and its subscribers. You subscribe a single time, and every upload lands in your feed on its own. You never have to keep checking; the news comes to you.
How it works
The channel keeps a list of everyone who wants updates. Signing up adds you to the list; unsubscribing crosses you off. When something changes, the channel walks its list and pings each name on it, and each watcher reacts however it likes. The channel does not need to know who its watchers are or what they do with the news. It only knows the list. That loose connection is the whole point: you can add a new watcher any time without touching the channel, the same way a new subscriber never changes how the channel posts.
When to use
Reach for the Observer pattern when one change needs to wake up many parts at once. A spreadsheet cell that recalculates the instant its inputs change. A screen that redraws the moment the data behind it updates. A game where scoring a point must update the scoreboard, the sound, and the leaderboard together. Whenever you catch yourself wanting several things to react to one event, this is the shape that keeps them in sync without wiring each pair by hand. It is the engine behind everything that feels live: a chat window that pops new messages, a dashboard that moves as numbers change, the Follow button that keeps feeding you new posts.
Watch out for
The catch is the forgotten goodbye. If a watcher signs up but never unsubscribes, the channel keeps pinging it long after it should be gone — a slow leak of memory and wasted effort spent on watchers who moved away. Update storms are the other trap: one small change can wake hundreds of watchers, which each change something, which wakes hundreds more. And because the channel and its watchers are so loosely tied, it can be hard to trace who reacted to what when something goes wrong. If you need to swap out how one watcher behaves rather than who gets told, the Strategy pattern is the closer fit.
In real code
A channel telling every subscriber about a new post.
Pseudocodeclass Channel:
subscribers = []
subscribe(watcher): subscribers.add(watcher)
unsubscribe(watcher): subscribers.remove(watcher)
post(news):
for watcher in subscribers:
watcher.update(news) # tell everyone, one by one
Each subscriber decides for itself how to react.
Javainterface Observer { void update(String news); }
class Channel {
private List<Observer> subscribers = new ArrayList<>();
void subscribe(Observer o) { subscribers.add(o); }
void unsubscribe(Observer o) { subscribers.remove(o); }
void post(String news) {
for (Observer o : subscribers) {
o.update(news); // notify each subscriber
}
}
}
Analogy
A YouTube channel and its subscribers. You subscribe once, and every new video reaches you on its own — no refreshing, no checking. Unsubscribe and the alerts stop. The channel just posts to its list, and everyone on the list hears about it.
Practice lessons
Lessons coming soon.