liveBook · Manning

One-to-many relationship

As explained in Software Design for Python Programmers

A one-to-many dependency is a relationship in which one object has multiple dependent objects that can be notified of its changes.

A one-to-many relationship lets one object notify multiple dependent objects when its state changes. The Observer pattern separates the publisher from subscribed consumers. section 12.1.3

A typical implementation provides:

  • An Observer implements update and chooses how to respond to publisher data. section 12.1.3

Baseball reporting

BaseballReporter publishes hit events to LogReport, TableReport, GraphReport, and FanClubReport. Each report attaches itself, receives update calls, and processes the event independently.

Figure 12.4 This version of the application is modeled from the Observer Design Pattern. The content publisher BaseballReporter is a subclass of Subject, and the report generation classes each implement the interface Observer. The Subject superclass’s private instance variable _observers is a list of subscribed observers. The grayed-out portions of the diagram have not changed logically from figure 12.3.
A diagram of a server

AI-generated content may be incorrect.

Shows the publisher, Subject observer list, and multiple report observers in one design. section 12.1.3

How Software Design for Python Programmers covers it

How deep it goes
Defines Subject, Observer, attachment, detachment, notification, and update flow through concrete Python classes.
Examples
Uses baseball hit reporting with log, table, graph, and fan-club observers.
What it assumes
Assumes familiarity with objects, classes, inheritance, and interfaces represented through Python abstract base classes.
What you can do after
Build it from scratch using attach, detach, notification, and observer update methods.
How it connects
Connects Subject, Observer, publishers, subscribers, and the Observer pattern.

Also explained in

Compare how 12 books explain one-to-many relationship →

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.