liveBook · Manning

Publish/subscribe model

As explained in Object-Oriented Software Design in C++

A communication model in which a publisher provides state or event information to one or more subscribers without depending on their specific behavior.

The publish/subscribe model separates a component that produces state or event information from components that process it independently. The publisher does not depend on subscriber implementations or behavior. section 12.0 section 12.2

A useful publish/subscribe design lets:

  • The publisher create content at arbitrary times and immediately notify current subscribers. section 12.1.1
  • Subscribers choose whether to request new content and decide independently what to do with it. section 12.1 section 12.1.1
  • Subscribers join or leave without changing publisher code, while both sides remain unaware of each other’s implementation. section 12.1.1 section 12.2

Baseball game reports

An athletics application publishes hit events as a baseball game progresses. A log report prints each event immediately, while graph and table reports accumulate events and produce summaries after the game. A hardcoded distributor that calls each report’s differently named method works, but adding or removing reports requires changing the distributor and couples it to every report implementation. section 12.1 section 12.1.2

Figure 12.1 An example of the publisher–subscriber model. The publisher component produces hit events that are consumed as they occur by the subscriber components to generate the various reports.

Use the architecture figure to trace hit events from the publisher to independent report subscribers. section 12.1

How Object-Oriented Software Design in C++ covers it

How deep it goes
Defines notification, content requests, subscription, and coupling, then contrasts them with a hardcoded C++ distributor.
Examples
Uses an online news service and baseball hit events feeding log, graph, and table reports.
What you can do after
Evaluate/understand how to separate event production from subscriber behavior and recognize hardcoded coupling that prevents independent subscription changes.
How it connects
Connects the Observer Design Pattern with the producer–consumer model, subjects, observers, loose coupling, and event handling.

Also explained in

Compare how 5 books explain publish/subscribe model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.