The Observer Pattern and the Weather Station That Would Not Stop Being Asked

Why a display that keeps asking a weather station "have you changed yet?" wastes work no matter how often it asks, and the one-sentence fix that flips the whole thing around.

September 9, 20262 min read4 / 19

Here's the idea in one sentence: instead of one thing constantly asking another thing whether it changed, let the thing that changed be the one that speaks up.

A Weather Station and a Phone That Keeps Asking

Picture a weather station. It's really just a thermometer with a brain, and every so often, the temperature it reads changes.

Now picture your phone. It wants to show that temperature on screen, so it needs to know the moment the weather station's number changes.

The obvious way: keep asking. Your phone asks the weather station, "Has your temperature changed?" Then it asks again. Then again. Forever.

This is called polling, and it has one real problem: you have to pick how often to ask.

Ask every second, and you might miss a change that happened half a second later. Ask every millisecond, and your phone is now asking a thousand questions a second, almost all of them answered "no."

Most of that asking is wasted work.

More Than One Display Makes It Worse

One phone polling is already wasteful. Add a window display, a second phone, and a smart speaker, and now all of them ask that same weather station the same question, every second. The number of pointless questions grows with every new display, not just with how often you ask.

There's a name for each side of this. The thing that changes, the weather station, is called the observable. The things that want to know about the change, the displays, are called observers. One observable, many observers: that's the shape of this problem, no matter how many devices you add.

Flipping It Around

Here's the fix: let the weather station speak up instead of being asked. It already knows the exact moment its temperature changes; nobody else does. So it tells every observer, once, only when it's actually true: "Hey, I changed."

Before any of this works, every observer has to introduce itself first. It has to say "add me to your list, I want to know when you change." Only after that can the weather station actually reach it.

The Two Jobs This Pattern Needs Done

Split this into two separate jobs.

Job one belongs to the observable: keep a list of watchers, with a way to add one, remove one, and notify all of them.

Job two belongs to each observer: just be able to get told "something changed," and react to it.

Every weather station, chat room, or stock ticker built with this pattern is doing exactly these two jobs, just with different details filled in.

The next post turns this into real code: a weather station that keeps a list of displays, and the one small trick that lets each display actually read the new temperature once it's been told something changed.