The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Understanding Aggregation and Composition with Examples

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:

python
class Department: def __init__(self, name): self.name = name class University: def __init__(self, name): self.name = name self.departments = [] # Aggregation relationship def add_department(self, department): self.departments.append(department) # Example Usage cs_department = Department("Computer Science") math_department = Department("Mathematics") uni = University("Tech University") uni.add_department(cs_department) uni.add_department(math_department)

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:

python
class Room: def __init__(self, name): self.name = name class House: def __init__(self, address): self.address = address self.rooms = [] # Composition relationship def add_room(self, room): self.rooms.append(room) def __del__(self): print(f"House at {self.address} is destroyed, all rooms are also destroyed") # Example Usage bedroom = Room("Bedroom") living_room = Room("Living Room") house = House("1234 Elm Street") house.add_room(bedroom) house.add_room(living_room)

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:

FeatureAggregationComposition
DependencyThe contained object can exist without the container.The contained object cannot exist without the container.
OwnershipThe container does not own the contained object.The container owns the contained object.
LifetimeThe lifetime of the contained object is not controlled by the container.The lifetime of the contained object is controlled by the container.
ExampleUniversity 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.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About