Observer lets objects subscribe to events from another object, so each can react when something changes.
The publisher keeps a list of interested observers. It sends each one a notification through the same contract. The caller controls who joins and who leaves.
What changes?
The listeners interested in an event.
What stays stable?
The publisher and the notification contract.
Who manages the connection?
The caller subscribes listeners and cleans up when they leave.
THE STRUCTURE / FOUR ROLES
Register interest. Notify through one contract.
Application setup subscribes observer objects to a WeatherStation. The station keeps their references and calls update(reading) on each registered observer. Each observer decides how to use the reading.
What changes when a subscriber leaves?
In this two-reading example, Display and History first receive 18 °C. Choose whether History stays subscribed before the station publishes 4 °C.
registers observers
notifies registered observers through WeatherObserver
both implement WeatherObserver
01 / CLIENTOwns the subscriptions
Application setup
const stop = station.subscribe(history)
Registers Display and History, and keeps the returned cleanup function for each subscription.
History stays registered. Its cleanup function is not called before the second reading.
Stores WeatherObserver references. When publishing, it calls update(reading) on each observer in that broadcast’s recipient list.
Registered for the second reading: Temperature display and Reading history.
notifies registered observers through WeatherObserver
03 / OBSERVER INTERFACEShared contract
WeatherObserver
update(reading: Reading): void
Every observer exposes this method. The station supplies the reading; it does not need to know how a panel displays or stores it.
Implementing the interface makes an object compatible. It still needs to subscribe to receive updates.
both implement WeatherObserver
04 / CONCRETE OBSERVERSIndependent reactions
Display and History
Temperature display
Uses its latest received reading to show the temperature. Remains subscribed in this example.
Reading history
Retains received readings for later inspection. Subscribed to both readings.
These lesson observers are objects with update methods. Separate classes are optional; both obey the same WeatherObserver contract.
Setup registers observers. The publisher calls their shared interface. Concrete observers react independently; subscription cleanup changes the audience without editing the publisher.
After the second publication: 4 °C
Display received18 °C → 4 °C
History received18 °C → 4 °C
Both are subscribed, so both receive the second reading. The publisher’s code is unchanged.
Try the demo below to change the audience yourself and watch each reading reach the subscribed panels.
WATCH THE CONNECTIONS
One reading. Only interested listeners.
Try it yourself
Choose who subscribes, then publish a reading. Watch it reach only the connected panels.
THE PUBLISHER
Weather station
18 °C
2 of 3 panels subscribed
Display
No reading yet
Shows the latest reading
History
No reading yet
Remembers the last five
Frost alert
No reading yet
Warns at 0 °C or below
Display and History are subscribed. Publish a reading to see them react.
Useful when the audience changes.
Several independent objects need to react to the same event, and listeners join or leave over time.
Connections need care.
Subscriptions add lifecycle work and make the call flow less obvious. A direct call may be simpler for one fixed receiver. Observer does not automatically make delivery asynchronous or reliable.