Overview
These are three strengths of 'has-a' relationships. Association: one object knows and uses another, with independent lifecycles (a Doctor treats Patients). Aggregation: a whole contains parts that can exist independently and may be shared (a Team has Players). Composition: a whole owns its parts, which are created with it and destroyed with it (an Order has OrderLines).
The distinction drives code decisions: who creates the object, who is allowed to hold references, whether deleting the whole cascades to the parts, and whether parts can be shared. In code, composition usually means the whole constructs the parts internally and never exposes them for outside mutation.
A professor is associated with students (they interact but live independently). A department aggregates professors (professors can move to another department). A building is composed of rooms (demolish the building and the rooms are gone).
When to use it
- Drawing class diagrams.
- Deciding object creation and ownership.
- Designing deletion and cascading rules.
Where it shows up in interviews
Recognize it when: who creates and deletes what?
- Design a library system
- Design an order system
Where it is used in real software
JPA's cascade and orphanRemoval, and database ON DELETE CASCADE, implement composition.
Aggregate roots compose their internal entities; other aggregates are referenced by ID (association).
A window composes its widgets; closing it destroys them.
Key terms
- Association
- Uses/knows; independent lifecycles.
- Aggregation
- Whole-part, parts can exist alone or be shared.
- Composition
- Whole-part with exclusive ownership and shared lifecycle.
- Dependency
- Weakest link: uses temporarily (parameter or local).
How it works, step by step
- 1Ask: can the part exist without the whole?
No: composition.
- 2Ask: can the part be shared among wholes?
Yes: aggregation or association.
- 3Decide creation
Composition: whole creates parts.
- 4Decide exposure
Composition: expose copies or read-only views.
- 5Decide deletion
Composition cascades; aggregation does not.
Classifying relationships
Examples from common LLD problems
| Pair | Relationship | Why |
|---|---|---|
| Order - OrderLine | Composition | Lines have no meaning without the order |
| Team - Player | Aggregation | Players exist and can change teams |
| Member - Loan (library) | Association | Independent lifecycles, linked by activity |
| Chess Board - Square | Composition | Squares are created with the board |
| Car - Driver | Association | Drivers use many cars |
NOWPair: Order - OrderLine | Relationship: Composition | Why: Lines have no meaning without the order
The question is always about lifecycle and ownership, not about how the objects look.
Implementation
// Composition: Order creates and owns its linesclass Order { #lines: { sku: string; qty: number }[] = []; addLine(sku: string, qty: number) { this.#lines.push({ sku, qty }); } // created inside get lines() { return this.#lines.map((l) => ({ ...l })); } // never shared} // Aggregation: Team holds players created elsewhereclass Player { constructor(readonly name: string) {} }class Team { private players = new Set<Player>(); add(p: Player) { this.players.add(p); } remove(p: Player) { this.players.delete(p); } // player still exists} // Association: a Doctor references patients it treatsclass Patient { constructor(readonly id: string) {} }class Doctor { treat(patient: Patient) { return `treating ${patient.id}`; }}Complexity and performance
Parts go with the whole.
Reference removal only.
Trade-offs
Composition protects invariants but prevents sharing; aggregation allows reuse but invites aliasing bugs.
Aggregation is often ambiguous; many teams just use association and composition.
Variants and related techniques
Across aggregates, hold IDs rather than object references.
Language-level references that do not prevent garbage collection.
Common mistakes
- Exposing composed parts for external mutation.
Fix: Return copies or read-only views.
- Calling every has-a composition.
Fix: Check lifecycle independence first.
Interview questions
Composition vs aggregation with an example?
Composition: an Order and its OrderLines; lines are created by the order and deleted with it. Aggregation: a Team and its Players; players exist independently and can join other teams.
How does composition show up in code?
The whole creates its parts internally, keeps them private, exposes only copies or behavior, and deletion of the whole removes the parts.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Classify 8 relationships in a hotel system | Easy | Lifecycle questions. |
| Model a chess board and pieces | Medium | Composition vs association. |