Composition and Aggregation: Ownership and Object Lifetimes
Composition and aggregation describe whole–part relationships in object-oriented design. They help you decide who owns a part, whether several objects may use it, and what happens when a whole is deleted. These decisions matter in applications such as document editors, music libraries, and school scheduling systems. Start with the application's ownership rules rather than assuming that every object stored inside another object has the same lifetime.
Ownership is the deciding distinction
Composition gives a whole exclusive ownership of its parts: a part has at most one composite owner at a time. When the whole is deleted, the parts it still owns are deleted with it. A report and its privately owned page objects can follow this contract. If the model permits detaching a page before deleting its report, that detached page need not be deleted with the report.
Aggregation expresses a whole–part grouping without that exclusive lifetime ownership. A reading list can group book objects that a catalog manages independently. Removing the list does not, by itself, require deleting those books. Sharing the same book between lists is possible if the application's rules allow it.
Aggregation's exact sharing and deletion rules should be documented. In particular, a hollow diamond does not supply a complete object-management policy. Also check that the relationship really represents a whole and its parts; an arbitrary connection between two objects does not automatically make it aggregation.
Design question | Composition | Aggregation |
|---|---|---|
Who owns the part's lifetime? | The current composite whole | Managed independently of this grouping |
Can two wholes own the same part simultaneously? | No | Exclusive composite ownership is not imposed |
What happens when the whole is deleted? | Its currently owned parts are deleted | The grouping alone does not require part deletion |
Can other objects hold references to a part? | Yes, without becoming composite owners | Yes, under the application's sharing rules |
The minimal UML notation below places a diamond at the whole end. You only need the ownership distinction here.
References do not multiply objects
A reference lets an object access another object. Several references can lead to the same instance. A reference is not a copy: changing shared state through one reference can be observed through another reference to that same object.
For disjoint categories of objects, the basic counting rule is:
Count each distinct object once, regardless of how many links reach it. Include only the object categories requested by the problem; a stored link is not an additional instance of its target.
Example: Two reading lists each reference three books, with two book objects appearing in both lists. There are six list-to-book links, but the number of distinct book objects is:
Subtract the overlap because those two objects were counted in both lists. If the catalog retains all four books, deleting either list does not change that book count.
Make lifetime rules explicit
A field declaration alone rarely establishes the full ownership contract. Receiving an object as a parameter does not automatically make the relationship aggregation, and creating an object inside a constructor does not by itself prove composition. Check whether parts can be shared, transferred, detached, or deleted independently, and make those operations follow the intended model.
Deletion here means ending an object's valid lifetime in the application model. It does not promise immediate recovery of its memory. In a language with garbage collection, physical cleanup may happen later. Conversely, an outside reference does not authorize continued use of a part that the model has deleted.
What to remember
- Composition gives each part at most one composite owner at a time.
- Deleting a composite deletes the parts it still owns.
- Aggregation needs explicit sharing and lifetime rules.
- Count distinct instances, not references to them.
Example: Counting Objects in a Playlist App
Example: A music app contains three Playlist objects. Each Playlist exclusively owns four distinct Entry objects. Each Entry stores one reference to an existing Song object. Across all entries, seven distinct Song objects are used; some songs appear in more than one playlist.
The Playlist–Song relationship groups songs without owning their lifetimes. A separate catalog retains all seven Song objects throughout. Deleting a Playlist logically deletes its owned Entries but does not delete any Song. No Entries are detached or transferred.
Count only Playlist, Entry, and Song objects. Exclude the catalog, stored references, and all other implementation objects.
a) Classify Playlist–Entry and Playlist–Song as composition or aggregation. Explain each choice using ownership and lifetime.
Playlist–Entry is composition. Each Entry has one exclusive Playlist owner, and deleting that Playlist deletes its currently owned Entries. The same Entry is not simultaneously owned by another Playlist.
Playlist–Song is aggregation. The Playlist groups independently managed songs, and multiple playlists may include the same Song object. The catalog retains those objects, so deleting a Playlist does not end their lifetimes.
The classifications follow the stated lifetime contracts. Merely storing an Entry or Song reference would not, on its own, establish either ownership contract.
b) Find the initial total number of live objects in the three counted categories.
First count the exclusively owned Entries. The number of Entry objects is ________, calculated as 3×4. Because every Entry stores one Song reference, there are ________. These links do not create twelve Song objects: the problem specifies seven distinct songs.
Add each object category once:
The initial total is 22 objects. Counting Song references as separate Song instances would overcount the shared objects.
c) Two Playlists are deleted. Find the total number of live objects remaining in the counted categories.
Deleting two Playlists also deletes ________, calculated as 2×4. The number of Playlist objects remaining is ________, calculated as 3−2.
The surviving Playlist still owns four Entries. All seven Songs remain because the catalog retains them independently of playlist membership. A Song need not remain in a surviving playlist to remain in the catalog.
The remaining total is 12 objects. As a check, the operation removed two Playlists and eight Entries, so 22−(2+8)=12. These are logical lifetime counts, not a prediction of when memory cleanup occurs.
A Python dispatch application stores independently managed Vehicle objects in a fleet registry. A TripGroup aggregates vehicles by retaining references to them. The Vehicle class defines == to compare registration strings, so separate instances can compare equal.
After group.add_vehicle(vehicle) adds the group's first member, complete this test with the Python operator that checks whether the stored member is the exact instance supplied. The completed expression should evaluate to True:
group.vehicles[0] vehicle
Two distinct reading lists, North and South, use aggregation to group books. Each has its own membership collection, but both reference the same Book object. A catalog independently retains that object. Each list displays a book's title by reading its current title field.
North changes the shared Book object's title from First Edition to Revised Edition, then removes only its own membership link to the book. What happens to South's display and the catalog's book?