Aggregation and Its Implementation

Learn about aggregation, its importance, and its implementation using a real-life example.

What is aggregation?

In contrast to composition, an aggregation represents a part-whole relationship that is not responsible for the existence and lifespan of its parts. While the parts are contained within the whole, the relationship is unidirectional, and the parts can belong to multiple objects simultaneously. Unlike a composition, an aggregation does not create or destroy its parts when it is created or destroyed.

Real-life example

Let’s assume a university management system adheres to the following rules:

  • Departments are composed in the university, meaning they are created and destroyed by the university.
  • Teachers are also composed in the university, meaning they are created and destroyed by the university.
  • Teachers are aggregated in departments, as they can join multiple departments simultaneously.

Here’s a visual representation of the relationship that we need to create:

Get hands-on with 1200+ tech skills courses.