Learn

Architecture & pattern guides

Read the idea, build it on a Grid, prove it in a challenge. Short, practical guides — illustrated with RodGrid's own nodes — on system design, design patterns, flow modeling, and how to get the most out of the app.

All articles

Association, aggregation, composition

The lines between class boxes, the ownership question that picks one, and the multiplicity and arrowheads that make a line say something precise.

The brief in one sentence

The Shopping Cart Model Challenge describes its whole domain in one sentence: a cart whose line items cannot outlive it, and products that are shared across carts and outlive them. Placing the three boxes, Cart, CartLine and Product, takes a minute. The sentence is not about the boxes, though. It is about what connects them, and a class diagram has a different line for each claim the sentence makes.

What dies with the whole

One question sorts most connections: if the whole is destroyed, does the part die with it? If yes, and the part belongs to that one whole only, that is composition — a filled diamond at the owner. A CartLine is "two mugs at €9.90" inside one particular cart. Delete the cart and the line means nothing, and no line belongs to two carts at once. If the part survives and can be shared, the filled diamond is out: a hollow diamond, aggregation, says it is still a part of the whole; a plain line, association, says they merely know each other. And when parts and wholes share one type, so code treats a file and a folder of files alike, the structure is the Composite pattern.

A fourth line is weaker than all three. A dependency, drawn as a dashed line with an open arrowhead, says a class merely uses another, as a parameter or a local, with no structural link between their objects. If total(taxes: TaxTable) reads a tax table only while it adds up the cart, Cart depends on TaxTable, but no cart keeps one. The hollow triangle, for one type being a kind of another, is a different relationship again, taught in Inheritance, interfaces and abstract types.

One cart, two kinds of diamond

Cart owns its lines (filled diamond). Each line aggregates one shared Product, which outlives every cart (hollow diamond at the line). The Catalog groups the same Products (hollow diamond). Each label is the multiplicity at the part's end.

Watch outThe diamond sits at the owner end, not at the part. A filled diamond drawn on the CartLine would say a line owns the cart, which is the opposite of the design. It is one of the most common ways to misread a class diagram, and to misdraw one.

Hollow diamond or plain line

Product survives every cart, so the destruction question rules out a filled diamond. It cannot choose between a hollow diamond and a plain line, though, because in both the part outlives the whole. The hollow diamond adds only a claim that the CartLine is a whole and the Product one of its parts, and the UML specification leaves the precise meaning of that shared aggregation to the application area and the modeller. The Shopping Cart Model brief chooses aggregation, as the diagram does; a plain association, saying only that a line refers to a product, is the other defensible reading.

How many, and in which direction

Multiplicity is written at the end of the class it counts, as 1, 0..1, 1..* or *. Write it on both ends when both are interesting. "A cart has many lines" is worth half as much as "a cart has 1..* lines, and a line belongs to exactly 1 cart": the 1..* sits at the CartLine end, the 1 at the Cart end. Whether a bound like that 1..* holds for real carts is something to test against instances, as Object diagrams: one moment of a running system shows.

An open arrowhead at one end of an association is navigability: drawn from CartLine to Product, it says a line can reach its product and promises nothing about the reverse. With no arrowhead at either end, the direction is left unspecified.

NoteOn a Challenge Grid every connection is drawn as the default line and arrow — the Edge Inspector that sets diamonds and dashes is hidden. Write the kind in the connection's label instead (double-click it): composition 1..*, aggregation 0..*, or «use» for a dependency. A connection has one label, so when both ends matter, write both multiplicities in it, the end the line starts from first: 1 : 1..* from Cart to CartLine.

Lines that claim too much, or too little

  • Using composition everywhere because it looks decisive. If the part can be shared, or has to survive the deletion of the whole it belongs to, the filled diamond is a claim the code does not honour, and whoever reads it will expect deleting the whole to take the parts with it.
  • Leaving every line bare. An association with no multiplicity and no label is a note saying "these two are related somehow" — which the reader already assumed.
  • Drawing an arrow when you mean a dependency. A dependency is a dashed line with an open arrowhead; a solid arrow is a navigable association, a stronger claim: a structural relationship along which one object can reach the other. For a class that only uses another as a parameter or a local, draw the dependency.

Draw the sentence

Open Shopping Cart Model and place the boxes first — Cart, CartLine and Product — without any lines. Then, for each pair, say the destruction sentence out loud — "if I delete this, does that die?" — and only then choose composition, aggregation or a plain association, written in the connection's label with its multiplicity. When the part survives, choose between the last two as above: aggregation for a whole–part claim, a plain association for a mere reference. When every line is labelled, read the Grid back as one sentence: it should say what the brief's sentence says, a cart whose lines cannot outlive it and products shared across carts that outlive them.

Practice this