Association
An association is a relationship between two software components in which one component uses, depends on, or otherwise has knowledge of the other. It is the most general of the relationships between classes in object-oriented programming, and the one from which the more specific composition and aggregation relationships are derived.
An association is typically realized as one object holding a reference to another, either to a concrete class or through an interface. The holder can invoke the collaborator’s operations, and the collaborator may in turn hold a reference back, forming a bidirectional association.
Directionality and multiplicity — one-to-one, one-to-many, many-to-many — are what distinguishes one type of association from another.
The Unified Modeling Language notation represents association as a solid line drawn between two classes in a class diagram. The line may carry multiplicities at each end (eg. 1, 0..1, *), role names describing each end’s purpose, and an open arrowhead indicating navigability. A line with no arrowheads denotes a bidirectional association.
In UML, there are two refined forms of association.
- Aggregation. A "has a" relationship in which the part can exist independently of the whole. It is the strongest form of association. It is drawn with a hollow diamond at the whole end.
- Composition. A stronger "owns a" relationship in which the part’s lifetime is bound to the whole. When the whole is destroyed, so are its parts. It is drawn with a filled diamond at the whole end.
Association is distinct from inheritance. Inheritance expresses an "is a" relationship between classes. Association expresses a "uses a" or "has a" relationship. The two are often contrasted. Inheritance ties a subclass to its parent’s implementation at compile time, while an association lets a class reach a collaborator through a reference that can be swapped at runtime (when the reference is held behind an interface).
Composition is recommended over inheritance for code reuse, because it keeps coupling lower and behaviors replaceable.