liveBook · Manning

Composite design pattern

As explained in Software Design for Python Programmers

The Composite Design Pattern is a structural design pattern that represents part-whole hierarchies as tree structures while allowing clients to treat individual objects and compositions uniformly.

The Composite Design Pattern represents part–whole hierarchies as trees while giving individual objects and compositions a common interface. Clients can therefore process either kind uniformly. section 14.0 section 14.2.3

A cost-report application initially separates leaf classes such as Ball and Glove from group classes such as EquipmentGroup and UniformGroup. CostReport must receive each group separately, compute each subtotal explicitly, and understand the hierarchy. section 14.2.2

The refactoring makes ProvisionGroup a subclass of ProvisionItem. Both leaves and composites then expose operations such as cost, print_item, add, and remove; unsupported leaf operations raise an invalid-operation exception, while groups implement them. section 14.2.3

A Composite implementation typically organizes the hierarchy around these roles:

  • Component: ProvisionItem defines shared properties and operations. section 14.2.4
  • Leaf: Ball, Bat, Glove, Cap, Jersey, Pants, Socks, Shoes, and Sunscreen represent individual objects. section 14.2.4
  • Composite: ProvisionGroup stores child ProvisionItem objects and computes its cost by summing their costs. section 14.2.3 section 14.2.4
  • Client: CostReport receives one tree root and asks it to print itself, without distinguishing leaves from groups. section 14.2.3

Baseball provisions cost tree

A PROVISIONS root contains EQUIPMENT, UNIFORM, and sunscreen. UNIFORM contains FOOTWEAR, allowing the same recursive reporting operation to print item costs, group subtotals, and the $200 overall total. section 14.2 section 14.2.3

Figure 14.4 The hierarchy of objects in a tree data structure that represents the provisions for a baseball player and the cost of each item. The composite objects are shaded gray.
Diagram

Description automatically generated

Inspect the hierarchy showing individual provision items, composite groups, and their part–whole relationships. section 14.2

How Software Design for Python Programmers covers it

How deep it goes
Descends from the generic Component–Leaf–Composite model into Python classes, recursive costs, printing, and invalid leaf operations.
Examples
Uses a baseball provisions tree containing equipment, uniform, footwear, individual items, subtotals, and a total cost.
What it assumes
Assumes familiarity with tree data structures and class inheritance while demonstrating the pattern through small domain-focused Python classes.
What you can do after
Build it from scratch by composing Python objects into a tree that computes costs and prints leaves and groups uniformly.
How it connects
Connects tree data structures with the Visitor Design Pattern and shared component interfaces.

Also explained in

Compare how 4 books explain composite design 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.