Skip to main content

What It Is

The Observer pattern lets one object announce events to many subscribers without knowing who they are. Subscribers register once; from then on, every event reaches all of them automatically.

The publisher depends only on a small subscriber protocol — new kinds of listeners appear without one line changing in the publisher.


Example: One Alert, Every Subscriber

The monitor keeps a list of subscribers. When something happens, it notifies the whole list — an email alert and a dashboard, today; a chat message tomorrow, by just subscribing it.

Publish once, deliver to all

One alert() call, two deliveries. The monitor has no idea what an email or a dashboard is — it only knows things that can be notified.

Prompt
Write a Python program with the Observer pattern: a Subscriber protocol with notify, an EmailAlert and a Dashboard subscriber, and a Monitor publisher with subscribe and alert methods that notifies every subscriber of an event. Subscribe both and raise one alert.
Output: complete Python program
Publish once, deliver to all

When to Use It

Use it when several parts must react to the same events and the list of reactors changes — UI updates, alerting, cache invalidation, activity feeds.

Skip it when exactly one, fixed receiver exists — call it directly. And for very simple cases, a plain list of callback functions gives the pattern's shape with no class machinery at all.


Pros and Cons

Pros: publisher and subscribers stay decoupled; subscribers come and go at run time; new reactions need no publisher changes.

Cons: the flow becomes invisible — "who reacts to this?" takes searching; a slow or failing subscriber can stall the notify loop; forgotten unsubscribes leak.