Composition
Composition models strong whole–part ownership, where the part's lifetime is bound to the whole. Learn the filled-diamond notation, when to reach for it, and how it differs from aggregation.
Open this example in StudioWhat composition means
Composition is the strongest form of the whole–part relationship in UML. It says two things at once:
- The whole owns its parts exclusively — a part belongs to at most one whole at a time.
- The part's lifetime is bound to the whole — when the whole is destroyed, its parts normally go with it.
An Order and its OrderLines are the classic case. An order line has no meaning outside the order that contains it. You never share one order line between two orders, and deleting the order deletes its lines.
Notation
Composition is drawn as an association with a filled (solid) diamond on the side of the whole:
Order ◆────────── OrderLine
1 1..*The diamond always sits at the whole, not the part. Multiplicities read as usual: one Order is composed of one or more OrderLines, and each OrderLine belongs to exactly one Order.
Composition vs. aggregation
Both are whole–part relationships; the difference is the strength of ownership.
| Question | Composition (filled ◆) | Aggregation (hollow ◇) |
|---|---|---|
| Can the part exist independently? | No | Yes |
| Can the part be shared by wholes? | No | Yes |
| Does deleting the whole delete the part? | Usually | No |
A Team aggregates Players — players exist before and after the team. An Invoice is composed of InvoiceLines — the lines never outlive the invoice. If you find yourself unsure, ask: "if the whole disappears right now, is keeping the part around meaningful?" If yes, you want aggregation or a plain association.
A software example
public class Order {
// Order creates and owns its lines; they are never handed out for reuse.
private final List<OrderLine> lines = new ArrayList<>();
public void addLine(Product product, int quantity) {
lines.add(new OrderLine(this, product, quantity));
}
}The design signals composition in two ways: Order constructs its own OrderLines, and no other object is given a way to attach an existing line. In garbage-collected languages the "delete the whole, delete the parts" rule is enforced by reachability — once the order is gone, nothing else references its lines.
Design reasoning
Composition is a statement about invariants, not just object graphs. Declaring Order ◆— OrderLine tells every reader:
- code may assume a line's
orderreference is never null and never changes; - the order can enforce rules across its lines (say, "no duplicate products") because nothing mutates lines behind its back;
- persistence can cascade deletes safely.
That is why choosing composition versus aggregation is a design decision, not a drawing preference — it changes what other code is allowed to do.
Reach for it
- The part is meaningless outside its whole (OrderLine, InvoiceItem, a Paragraph in a Document)
- The whole must enforce invariants across its parts
- Deleting the whole should delete the parts (cascade)
Avoid it
- The part is shared between wholes (a Student in many Courses)
- The part has its own independent lifecycle (a Player joining another Team)
- You only mean “uses” or “knows about” — that is an association or dependency
Common mistakes
- Diamond on the wrong end. The filled diamond belongs on the whole.
- Composing shared objects. If two wholes point at the same part, it is not composition — the exclusivity rule is broken.
- Using composition for every "has-a". A
Carhas aDriver, but drivers are not parts of cars. Reach for plain association first, and upgrade only when ownership and lifetime rules genuinely apply. - Forgetting multiplicities.
1on the whole side is implied by exclusivity, but the part side still needs an explicit multiplicity (1..*,0..*, …).
Practice
Model a Document composed of Sections, each composed of Paragraphs. Then add an Author — decide whether the author is a part (it is not) and pick the right relationship. Open the example below in Studio and try it.