UML Class Diagram – Differences Between Association, Aggregation, and Composition

class-diagramobject-orienteduml

I'm confused about some of the notations of UML class diagrams.

enter image description here

Pretty sure I know what Association means. Any relationship between instances of two classes, where an instance of one class needs to know about an instance of the second class in order to perform it's work – is an Association relationship. An Association often means class A has a reference (field) to an instance of class B.

However, I'm having trouble understanding what the Aggregation and Composition arrows mean. Part of my confusion was caused by encountering different definitions of these notations.

Two definitions of the Aggregation notation:

Definition 1: An Aggregation notation between two classes is suitable whenever an instance of class A holds a collection of instances of class B (e.g. a List, Array, whatever).

Definition 2: An Aggregation link between two classes is suitable if an instance of class A holds a reference to an instance of class B, and the B instance is dependent on the lifecycle of the A instance. Meaning: When the instance of class A get's deleted, so will the instance of class B. The instance of class B is entirely contained by the instance of class A, as opposed to the instance of class A simply owning a reference to the instance of class B (which is regular Association).

Regarding what the Composition notation means and how it differs from the Aggregation notation, I'm not sure.

Please clarify the definitions and help me understand. Concrete examples would be welcome.

Best Answer

The three links Association, Aggregation and Composition form a kind of scale on how closely two classes are related to each other.

On the one end of the scale, there is Association, where objects of the two classes can know about each other, but they do not affect each others lifetime. The objects can exist independently and which class A object knows about which class B objects can vary over time.

On the other end of the scale, there is Composition. Composition represents a part -- whole relationship such that class B is an integral part of class A. This relationship is typically used if objects of class A can't logically exist without having a class B object.

The Aggregation relation is somewhere between those two ends, but nobody seems to agree where exactly, so there is also no universally agreed definition of what an Aggregation means. In that sense, both definitions that you found are correct and if you ask 10 people, you risk getting 11 different definitions.