Aggregation

Comprehensive study notes, diagrams, and exam preparation for Aggregation.

Aggregation

Definition

Aggregation is a special form of association in object-oriented programming where one class represents a whole and another class represents a part, but the part can exist independently of the whole.

It is commonly described as a weak ownership or shared ownership relationship. The whole object does not fully control the lifecycle of the part object. In UML, aggregation is usually represented by a hollow diamond at the whole/aggregate side.

Example:

  • A School has Teachers
  • A Team has Players
  • A Library has Books

In all these cases, the parts may continue to exist even if the whole no longer exists.


Main Content

1. Aggregation as a "Has-A" Relationship

  • Aggregation expresses that one object contains or uses another object, forming a has-a relationship rather than an inheritance relationship.
  • It is used when a class needs to collaborate with other classes, but those classes are not strongly dependent on each other for existence.

A simple example is a Car and Engine relationship in some designs, or more clearly, a Department and Professors relationship. A department has professors, but professors can still exist even if they are removed from one department or transferred to another.

In programming terms, aggregation is often implemented by storing references to objects of another class. The aggregate object may access the part objects, but it does not necessarily create or destroy them.

Example in everyday life:

  • A University has Students
  • Students can exist before joining the university and can continue after leaving it

This makes aggregation useful for representing real-world systems where membership or inclusion is temporary or flexible.

2. Independent Lifecycle of Objects

  • In aggregation, the lifecycle of the part object is not controlled by the whole object.
  • The part can survive independently, be shared by multiple objects, or be reassigned from one aggregate to another.

This is the key difference between aggregation and composition. In composition, if the whole is destroyed, the parts are also usually destroyed. In aggregation, the parts remain alive even if the whole disappears.

For example:

  • A Player may belong to a Team
  • If the team is disbanded, the player still exists and may join another team
  • Therefore, the player has an independent lifecycle

This concept is very important in software design because it prevents unnecessary coupling between classes and allows objects to be reused across different contexts.

3. UML Representation and Implementation

  • In UML class diagrams, aggregation is shown using a hollow diamond at the side of the whole object.
  • The diamond is connected to the class that owns or contains the other class.

Example structure:

School o---- Teacher

Here:

  • School is the whole
  • Teacher is the part
  • The hollow diamond o indicates aggregation

Implementation often involves one class holding a collection or reference to another class.

Example in pseudocode:

class Department:
    teachers: list of Teacher

This means:

  • A Department can have multiple Teacher objects
  • The teachers exist independently
  • Teachers may be assigned to different departments over time

Aggregation can be one-to-one, one-to-many, or many-to-many, depending on the model:

  • One Library to many Books
  • One Department to many Teachers
  • Many Teams may use the same Coach in certain systems

Working / Process

  1. The whole object is created to represent the broader entity, such as a school, team, or department.
  2. The part objects are created separately and then linked to the whole object by reference or association.
  3. The whole object uses the part objects, but the part objects continue to exist independently even if the whole object is removed.

Advantages / Applications

  • Promotes loose coupling between classes, making systems easier to modify and extend
  • Supports reuse of objects because parts can be shared across different wholes
  • Models real-world relationships more accurately when parts do not depend on the lifetime of the whole

Aggregation is widely used in:

  • School management systems
  • Library management systems
  • Company and employee models
  • Sports team systems
  • University and course registration systems

For example:

  • In a School Management System, a school may aggregate teachers and students
  • In a Library System, a library may aggregate books and members
  • In a Company System, a department may aggregate employees

This relationship helps designers represent entities in a natural way while keeping the software flexible.


Summary

  • Aggregation is a weak has-a relationship between objects
  • The part object can exist independently of the whole
  • Important terms to remember: aggregation, whole, part, has-a relationship, hollow diamond, independent lifecycle