Association

The general link between two classes: each object keeps its own lifecycle, and the link carries a direction and a multiplicity.

Overview

An association is the most general relationship between two classes. It says that objects of one class are connected to objects of the other: a doctor treats patients, a student takes courses, an order belongs to a customer. Neither side owns the other. Each object exists on its own, and the link can be made or broken without creating or destroying anything.

Aggregation and composition are associations with something added: a whole-part meaning, and for composition a lifecycle rule. UML 2.5.1 models this as a property of the association end called aggregation, with the values none, shared and composite. So every diamond is an association first; the diamond only adds ownership.

Objects that know about each other

Three kinds of link sit on one scale, from loosest to tightest.

  • Association: the objects are linked, and their lifecycles are independent. UML draws a plain line.
  • Aggregation: one side groups the other, and the lifecycles are still independent. UML draws a hollow diamond at the whole.
  • Composition: one side owns the other, and the part ends with the whole. UML draws a filled diamond at the whole.

The diagram below draws two plain associations, each a line with multiplicities at both ends.

Interview questions

  • What is the relationship between association, aggregation and composition?
  • How do you keep a bidirectional association consistent?
  • What is multiplicity, and how does it change your code and your schema?
  • When should you introduce an association class?
  • How do associations translate to a database schema?
  • Should associations be one-way or two-way by default?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio