Associations, Aggregation, and Composition
Object-oriented systems model interactions through well-defined relationships between classes.
1. Association
Objects are connected and interact, but neither class owns the lifecycle of the other.
- Example:
Student and Course. A student enrolls in multiple courses; a course has multiple students.
2. Aggregation (Weak Whole-Part)
A "has-a" whole-part relationship where the part can exist independently of the whole.
- UML Notation: Solid line with a hollow diamond ⋄ at the whole/owner end.
- Example:
Team and Player. If a team dissolves, the players continue to exist and can join other teams.
3. Composition (Strong Whole-Part)
A whole-part relationship where the part's lifecycle is bound strictly to the whole.
- UML Notation: Solid line with a filled diamond ⧫ at the whole/owner end.
- Example:
House and Room. If the house is destroyed, its rooms cease to exist.
4. Dependency (Uses-A)
A temporary relationship where one class uses another (e.g. as a parameter in a method).
- UML Notation: Dashed line with open arrowhead ⇢.
- Example:
OrderProcessor uses PaymentGateway to charge a transaction.
Exercise 1
Question
How do you distinguish aggregation from composition in terms of lifecycle?
Show solution ↓Hide solution ↑
Solution
In aggregation, parts exist independently of the whole (weak ownership). In composition, the lifetime of the part is managed directly by the whole (strong ownership), so destroying the whole destroys the parts.