liveBook · Manning

Observer pattern

As explained in Software Design for Python Programmers

A behavioral design pattern in which dependent objects are notified when an observed object's state changes.

The Observer pattern solves a publisher–subscriber problem: one subject produces data, while multiple observers process each update independently. The subject tracks subscribers but does not depend on their implementations or purposes. section 12.0 section 12.1

A typical implementation provides:

  • A Subject stores observers and exposes attach() and detach() operations. section 12.1.3
  • An Observer interface requires an update() method for receiving notifications. section 12.1.3
  • When the subject receives new state, notify() calls update() on every subscribed observer. section 12.1.3
  • Observers may fetch the subject’s current state after notification and decide how to use it. section 12.1.3 section 12.2

Baseball reports

BaseballReporter subclasses Subject and publishes each hit Event. LogReport records events immediately, TableReport accumulates per-player statistics until the game ends, and GraphReport counts hit types for a final bar chart. A FanClubReport can subscribe later for one named player without changing BaseballReporter. section 12.1 section 12.1.3

Figure 12.5 The generic model of the Observer Design Pattern. Compare with figure 12.3. During run time, we can add subscriber Observer objects to or remove them from the publisher Subject class’s collection of Observer objects. Whenever an ExemplarSubject changes state, the Subject class’s notify() method iterates over the observers collection and calls method update() of each ConcreteObserver object. Each ConcreteObserver can then choose to call the get_state() method of the ExemplarSubject class to get the new state.
A diagram of a state

AI-generated content may be incorrect.

Use the generic model to connect subject state changes, runtime subscription management, notification, and observer-controlled state retrieval. section 12.1.4

How Software Design for Python Programmers covers it

How deep it goes
Defines Subject, Observer, notification, attachment, detachment, and state retrieval through a worked Python application and generic model.
Examples
Uses news streaming, baseball hit events, logs, statistical tables, bar charts, and a fan-club bulletin.
What it assumes
Assumes familiarity with objects and interfaces while teaching the publisher–subscriber relationship through a concrete reporting application.
What you can do after
Build it from scratch by defining Subject and Observer roles, wiring subscriptions, notifying observers, and letting each observer process state independently.
How it connects
Connects subjects, observers, publisher–subscriber systems, producer–consumer systems, state changes, and loose coupling.

Also explained in

Compare how 4 books explain observer pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.