Understanding Aggregation and Composition with Examples
In object-oriented design, Aggregation and Composition are two important relationships that define how objects are connected to each other. Both are forms of association, but they differ in terms of their strength and how the objects involved depend on each other. Here’s a breakdown of both concepts with examples.
1. Aggregation
Aggregation represents a “has-a” relationship between two objects, but the relationship is looser and less dependent. In aggregation, one object can exist independently of the other. This means that if the container object is destroyed, the contained object may still exist.
Key Characteristics of Aggregation:
-
Looser relationship: The lifetime of the contained object is not controlled by the container object.
-
Independent existence: The contained object can exist on its own.
-
“Has-a” relationship: The container “has” a reference to another object, but that object can exist without the container.
Example of Aggregation:
Consider a University and Departments.
-
A university has many departments.
-
The departments can exist independently of the university (a department might exist without a university if it’s part of a different one or as a standalone entity).
-
If a university is closed, the departments can still exist (although they might be reassigned).
Code Example:
In this case, Department objects can exist without the University, and the relationship between them is aggregation.
2. Composition
Composition is a stronger form of aggregation. It also represents a “has-a” relationship, but here, the contained object cannot exist without the container object. If the container object is destroyed, the contained object will also be destroyed. This signifies a strong ownership of the contained objects.
Key Characteristics of Composition:
-
Stronger relationship: The container object is responsible for the lifetime of the contained object.
-
Dependent existence: The contained object cannot exist without the container object.
-
“Contains-a” relationship: The container contains the contained object, and the contained object is tightly coupled with the container.
Example of Composition:
Consider a House and Rooms.
-
A house contains rooms.
-
If the house is destroyed, the rooms also cease to exist (you cannot have a room without a house).
-
The room is part of the house and has no independent existence outside it.
Code Example:
In this case, if the House object is deleted or goes out of scope, the Room objects will also be destroyed since Room objects cannot exist independently without the House object.
Key Differences Between Aggregation and Composition:
| Feature | Aggregation | Composition |
|---|---|---|
| Dependency | The contained object can exist without the container. | The contained object cannot exist without the container. |
| Ownership | The container does not own the contained object. | The container owns the contained object. |
| Lifetime | The lifetime of the contained object is not controlled by the container. | The lifetime of the contained object is controlled by the container. |
| Example | University and Departments. | House and Rooms. |
Conclusion
Understanding the difference between aggregation and composition helps in determining how tightly objects should be coupled in a system. Aggregation provides more flexibility, allowing objects to exist independently, while composition enforces stronger relationships, where the contained object’s lifecycle is managed by the container.