Skip to content
DiaGO

Object Relationships

Objects rarely work alone. This is the map of how they connect — dependency, association, aggregation, composition, and inheritance — and how to choose between them.

Why relationships are the design

A class list is not a design. The design lives in the connections: which object knows about which, who owns whom, and what breaks when something changes. Get the classes roughly right and the relationships wrong, and the system is still hard to change. This article is the map of the connection types, ordered from weakest to strongest coupling, so you can reach for the lightest one that does the job.

The spectrum of coupling

Every relationship is a form of coupling — a way one class becomes sensitive to another's changes. Stronger relationships promise more but cost more when the model shifts.

RelationshipStrengthMeaningNotation
DependencyWeakestUses transiently (parameter, local, call)Dashed open arrow
AssociationMediumHolds a lasting referenceSolid line
AggregationMediumShared whole–partHollow diamond
CompositionStrongExclusive, lifetime-bound whole–partFilled diamond
GeneralizationStrongIs-a; full substitutabilityHollow triangle

The skill is not memorizing the symbols — it is asking the right question in order and stopping at the first "yes".

The decision, in order

Ask these about a pair of classes, top to bottom, and take the first match:

  1. Is one a kind of the other, fully substitutable? Use generalization. A SavingsAccount is an Account. Be strict: if you only want to reuse code, this is the wrong answer.
  2. Does one exclusively own the other, so the part dies with the whole? Use composition. An Order owns its OrderLines.
  3. Is it a whole–part where parts are shared or outlive the whole? Use aggregation — and know that its formal meaning is weak (many modelers treat it as documentation intent).
  4. Do instances hold a lasting reference to each other? Use association, with multiplicities and roles.
  5. Does one just use the other briefly? Use dependency — and only draw the ones that carry design meaning.

Structure vs. behavior

Dependency, association, aggregation, and composition are all structural — they describe how objects are wired together at rest. Generalization is different: it is about substitutability, a promise that a subtype honors the supertype's contract everywhere. That is why mixing "inherit to reuse code" with real is-a relationships causes so much pain: it borrows the strongest structural bond to solve a problem that composition would solve more safely.

A useful reframing that runs through modern design advice: prefer composition over inheritance as a default, not because inheritance is bad, but because a has-a you can rewire is cheaper to change than an is-a you have baked into a type hierarchy. Inheritance is the right tool when substitutability is genuinely what you mean — not a shortcut for sharing methods.

A worked example

Consider a music app. A Playlist aggregates Songs — the same song appears in many playlists and outlives any of them. A Song is composed of AudioSegments that are meaningless on their own. A Player depends on a Codec it receives to decode a stream, but does not keep it. A PremiumPlaylist is a Playlist with extra rules. Four relationships, four different strengths — each chosen by walking the questions above.

Common mistakes

  • Everything becomes an association. If two objects never hold references to each other, a dependency is the honest relationship.
  • Inheritance for code reuse. The classic Stack extends ArrayList trap — you inherit add(index, element), which breaks the stack's contract.
  • Agonizing over aggregation vs. association. The distinction is low-stakes and formally weak; don't spend design meetings on it.
  • Ignoring direction. Coupling has a direction — the arrow shows what depends on what, and therefore what breaks when you change something.

Practice

Take a BlogPost, Comment, Author, and Tag. For each pair that relates, walk the five questions and pick the relationship. Then build it in Studio and check that a reader could reconstruct your ownership decisions from the diamonds and arrows alone.