Design Concepts  «Prev  Next»
Lesson 6The concept of Aggregation:
ObjectiveDefine aggregation as it applies to object modeling.

Define Aggregation in Object Modeling

Aggregation is a key concept in object modeling and represents a "has-a" relationship between objects. It is a type of association where one object (the whole) contains or is composed of other objects (the parts), but the lifecycle of the parts is independent of the whole.
  1. "Has-a" Relationship:
    • Indicates ownership or a whole-part relationship. For example, a Library has Books, but Books can exist independently of the Library.
  2. Independent Lifecycle:
    • The part objects can exist independently of the whole object. For example, even if a Library object is destroyed, the Book objects it contains still exist.
  3. Weaker Coupling:
    • Aggregation represents a weaker relationship compared to composition. While the whole aggregates the parts, it does not imply exclusive control over their lifecycle.
  4. Implemented as References:
    • In programming, aggregation is typically implemented by having the whole object hold references to the part objects.

Example in Object Modeling:
Consider a scenario where a `Team` is associated with multiple `Players`. Even if the `Team` object is destroyed, the `Players` still exist and can be part of another team.
UML Representation:
  • Aggregation is represented as a hollow diamond at the end of the association line near the whole object.

Example in Java Code:
// Part class
class Player {
    private String name;

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

// Whole class
class Team {
    private String teamName;
    private List<Player> players;

    public Team(String teamName) {
        this.teamName = teamName;
        this.players = new ArrayList<>();
    }

    // Add a player to the team
    public void addPlayer(Player player) {
        players.add(player);
    }

    // Display team details
    public void displayTeam() {
        System.out.println("Team: " + teamName);
        System.out.println("Players:");
        for (Player player : players) {
            System.out.println("- " + player.getName());
        }
    }
}

// Main class to demonstrate aggregation
public class Main {
    public static void main(String[] args) {
        Player player1 = new Player("Alice");
        Player player2 = new Player("Bob");

        Team team = new Team("Dream Team");
        team.addPlayer(player1);
        team.addPlayer(player2);

        team.displayTeam();
    }
}

Key Points:
  1. Player Objects Exist Independently:
    • The Player objects can exist without being part of a Team.
  2. Team Aggregates Players:
    • The Team object holds references to the Player objects but does not control their lifecycle.

Aggregation vs. Composition:
Aspect Aggregation Composition
Relationship Whole-part with independent lifecycle Whole-part with dependent lifecycle
Coupling Weaker coupling Stronger coupling
Lifespan Part can exist without the whole Part depends on the whole for existence
Example Library and Books Car and Engine

Aggregation is a fundamental concept in object-oriented design, enabling developers to model real-world relationships in a way that enhances modularity and reusability.

Working with Aggregates

Aggregation describes the assembly of objects to create a new object where the whole (the assembly) is greater than the sum of the parts. One the part objects are assembled into an aggregate, they are essentially hidden. The part objects become the implementation of the aggregate. To use the aggregate, the client objects must access the interface of the aggregate object. The part objects are inaccessible.
  • Propagation:
    When the aggregate interface is invoked, the aggregate propagates the instruction, that is, it sends instructions to its parts to implement the requested function. The aggregate has no behavior of its own other than to coordinate the behavior of its parts. For example, a car is made up of an engine, transmission, fuel system, and other parts. When drivers want to move the car forward, they use the interface of the car, that is, the ignition switch, the gas pedal, the brake pedal, and so on, rather than access the parts directly. For example, a press of the gas pedal is implemented when the fuel system pumps gas, the engine turns over, and the transmission responds to the speed changes. Aggregation is not reflected in code. The implementation looks like any other association. Use aggregation to show intent. The intent of aggregation is to hide the makeup of a complex object by requiring client objects to talk to the aggregate.

Aggregation versus Composition

Aggregation defines an object in terms of the parts assembled to make it. When you want the parts to be available for use even when the aggregate is dissembled, you are using weak aggregation[1]. If the parts exist only as long as they are part of the aggregate, you are using strong aggregation[2] or composition.
The diagram below describes an example of both weak and strong aggregation (composition):
Weak Aggregation
Weak Aggregation

Difference between Weak and Strong Aggregation

The distinction between weak aggregation and strong aggregation in object modeling lies in the degree of dependency between the whole (aggregating object) and its parts. Both represent "whole-part" relationships, but they differ in their lifecycle and the strength of the connection.
Weak Aggregation
  • Definition: Represents a "has-a" relationship where the part can exist independently of the whole.
  • Lifecycle: The part does not depend on the whole for its existence.
  • Coupling: Loosely coupled relationship.
  • Implementation: Often represented as aggregation in Unified Modeling Language (UML) with a hollow diamond.

Example:
  • A Library has Books. If the Library is deleted, the Books can still exist independently.
  • A Team has Players. If the Team ceases to exist, the Players may still belong to other teams.

Key Characteristics:
  1. The part can be shared with multiple wholes.
  2. Changes in the whole may not affect the existence of the part.

UML Representation: Represented by a hollow diamond near the whole in UML diagrams.
Strong Aggregation (Composition)
  • Definition: Represents a "part-of" relationship where the part cannot exist without the whole.
  • Lifecycle: The lifecycle of the part is tied to the whole. If the whole is destroyed, the parts are also destroyed.
  • Coupling: Tightly coupled relationship.
  • Implementation: Often represented as composition in UML with a filled diamond.
  • Example:
    • A Car has an Engine. If the Car is destroyed, the Engine is also destroyed.
    • A House has Rooms. If the House is demolished, the Rooms cease to exist.

Key Characteristics:
UML Representation:Represented by a filled diamond near the whole in UML diagrams.
Comparison Table
Aspect Weak Aggregation Strong Aggregation (Composition)
Definition "Has-a" relationship "Part-of" relationship
Lifecycle Part exists independently of the whole Part depends on the whole for its lifecycle
Coupling Loosely coupled Tightly coupled
Sharing Parts can belong to multiple wholes Parts are exclusive to one whole
Example Library and Books Car and Engine
UML Symbol Hollow diamond Filled diamond

Understanding these concepts is vital in designing object-oriented systems that accurately represent real-world relationships while maintaining flexibility and modularity.
In strong aggregation a part entity cannot exist without the containing whole entity in the whole-part relationship. For example, university has multiple departments. This a composition relationship between the university and department entity. A department cannot exist on its own with out the university.

Modeling Composition

Composition is used for aggregations where the life span of the member object depends on the life span of the aggregate. The aggregate not only has control over the behavior of the member, but also has control over the creation and destruction of the member. In other words, the member object cannot exist apart from the aggregate. This greater responsibility is why composition is sometimes referred to as strong aggregation. Composition is a subset of aggregation, just as aggregation is a subset of association.
1) Weak Aggregation
Weak aggregation: Players may participate in many teams at the same time.
Weak aggregation: Players may participate in many teams at the same time. For example, they can play for a city league and a company team. When a team is disbanded the players still exist and can be reassigned to other teams.

2) Strong Aggregation or Composition
Strong aggregation: Chapters belong to a specific book. Without the book the chapters have no context and lose their meaning.
Strong aggregation or Composition: Chapters belong to a specific book. Without the book the chapters have no context and lose their meaning. If the book is deleted the chapters are deleted along with it. It does no good to keep them since they cannot be reassigned or reused in another book.

Aggregation and composition can represent an infinite number of layers of detail.
However, remember to include only the level of detail needed to support the abstraction required by the problem domain.

[1]Weak aggregation: Players may participate in many teams at the same time. For example, they can play for a city league and a company team
[2]Strong aggregation or Composition: Chapters belong to a specific book. Without the book the chapters have no context and lose their meaning.

SEMrush Software