Observer pattern
A shopping cart needs to tell other parts of the page when it changes. We build the subscribe-and-notify list that lets it do so without knowing who is listening.
In 30 seconds
The observer pattern lets one object announce changes without knowing who cares. The object, called the subject, keeps a list of functions. Anything interested, an observer, adds its function to that list: it subscribes.
When the subject changes, it calls every function on the list: it notifies. New observers can join without any change to the subject, which keeps the parts of a program independent.
Button clicks and self-updating screens work this way. The usual mistakes are forgetting to unsubscribe and sending far more notifications than needed.
Observer pattern, step by step
Simplified: real implementations also decide what happens when an observer throws an error, and whether observers are called at once or a little later.
- Step 1: The cart calls everyone. A shopping cart changes, and two parts of the page must follow: the badge showing the item count and the box showing the total. The direct way is for the cart to call each of them by name. It works, but the cart now has to know every part that cares, and each new part means editing the cart again.
- Step 2: Subscribe. Turn it around. The cart keeps a plain list and offers one operation, subscribe: “give me a function, and I will call it when I change”. The badge and the total each hand over their update function. The cart is called the subject, and the parts that signed up are its observers.
- Step 3: Notify. A third item goes into the cart. After changing its own data, the cart walks down the list and calls every function on it, passing the new value. The badge now shows 3 and the total €18. The cart did not need to know what either of them does with the news.
- Step 4: Loose coupling. A new part, the saver, wants to store the cart after every change. It subscribes, and that is all: the cart’s code is untouched. The cart depends only on “a list of functions”, never on who is behind them, so observers can be added, replaced or tested on their own. This is what loose coupling means.
- Step 5: Where you meet it. The pattern is everywhere under other names. A button is a subject: addEventListener subscribes a function, and a click notifies it. In user interfaces, components subscribe to a store of data and redraw themselves when it changes. Listener, handler, subscriber and callback all mean observer.
- Step 6: Pitfall: never unsubscribing. The total box is closed and removed from the page, but nobody took it off the list. The cart still calls it on every change, and because the list still points at it, its memory can never be freed. Every subscribe needs a matching unsubscribe when the observer goes away.
- Step 7: Pitfall: update storms. A loop adds 100 items one by one. Each change notifies all three observers, so the page redraws and saves 300 times when only the last result matters. The cure is batching: make all the changes, then notify once. A related trap is an observer that changes the subject it is watching, which sets off another round.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?