liveBook · Manning

Composition relationship

As explained in Data-Oriented Programming: Reduce software complexity

A composition relation is an object relationship in which the composed object's life cycle depends on the composing object.

A composition relationship makes one object’s life cycle depend on another. If the composing object dies, its composed object dies with it. That is the practical question behind the UML diamond: can these objects live independently, or is one owned by the other? section 1.1.2

Library ownership

A Library and its Catalog form a one-to-one composition: destroying the Library also destroys its Catalog. A Library and its Members form a one-to-many composition: all Member objects share the Library’s life cycle. By contrast, Book and Author form a many-to-many association because books and authors can continue to exist independently. section 1.1.2

When composition becomes data rather than an object diagram, make the relation explicit:

  • Represent one-to-many ownership through the composing entity and its collection of composed entities. section 3.1
  • Distinguish composition from association by modeling whether entities share a life cycle or retain independent identities. section 1.1.2
  • Represent related entities explicitly with identifiers and collections; for example, a Catalog can hold an index for retrieving Books. section 3.1
Figure 1.2 The two kinds of composition: one-to-one and one-to-many. In both cases, when an object dies, the composed object dies with it.

Compare the one-to-one and one-to-many forms of composition and their shared lifecycle rule. section 1.1.2

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains lifecycle semantics, UML symbols, cardinalities, and explicit identifier-based data representations.
Examples
Uses Library-Catalog, Library-Member, and Book-Author relationships to contrast composition with independent association.
What it assumes
Starts UML relationship terminology from basic distinctions between independent and dependent object lifecycles.
What you can do after
Evaluate whether related objects or entities share a lifecycle and represent their relationship explicitly.
How it connects
Connects composition with association, UML, data-oriented programming, and object-oriented programming.

Also explained in

Compare how 2 books explain composition relationship →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.