Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Aggregation
LLD

Aggregation

Learn about weak whole-part relationships where component objects can exist independently.

The Problem It Distinguishes

A team has players. Disband the team — do the players die? Obviously not; they sign elsewhere. But a house’s rooms do cease existing with the house. Both are “whole–part” relationships, yet their lifecycle ownership differs fundamentally — and that difference determines deletion logic, copy semantics, and database cascades. Aggregation is the weak variant: whole holds parts, but parts exist independently.

        Team                          Player
      ┌──────┐   hollow diamond       ┌────────┐
      │ Team │◇──────────────────────│ Player │
      └──────┘  (shared, independent) └────────┘
            team disbands → players live on,
            possibly joining another team

The hollow diamond sits on the whole end. Its emptiness is the notation for “holds a reference but does not own the lifetime.”

Java Mechanics

class Team {
    private final List<Player> players;
    // parts created OUTSIDE and handed in — team doesn't manufacture them
    Team(List<Player> players) { this.players = new ArrayList<>(players); }
    void disband() {
        players.clear();     // release references only;
    }                        // Player objects remain valid elsewhere
}

The tell is in construction: aggregation receives its parts (constructor injection); composition would create them internally (this.room = new Room()).

Where It Appears

  • Department ↔ Employees: reorganize departments, employees persist.
  • Playlist ↔ Songs: delete playlist, songs stay in library.
  • Cache ↔ cached objects: cache eviction never implies object destruction — owners elsewhere still hold them.
  • Course ↔ Students: a course enrollment ends; students continue.

The Shared-State Hazard

Because parts are shared, mutation is visible to every holder:

Player p = new Player("Rohit");
teams.get(0).add(p);
teams.get(1).add(p);          // same instance in two teams
p.setInjured(true);           // BOTH teams observe it instantly

This is correct when shared truth is intended (injury status), a bug when independence was assumed. Value-object copies or defensive duplication avoid unintended coupling.

Aggregation vs Composition vs Association

AssociationAggregationComposition
Phraseknows-ahas-apart-of
Lifetime linknonepart outlives wholepart dies with whole
Part creationanywhereexternal, injectedinternal to whole
UMLplain linehollow diamond ◇filled diamond ◆

Interview Signals

  • The discriminating question is always lifecycle: “if I delete the container, must the contents die?” Yes → composition. No → aggregation.
  • Copy semantics follow: aggregating structures copy shallowly (references shared) by design; composing structures need deep copies.
  • Database translation: aggregation ≈ FK without ON DELETE CASCADE; composition ≈ cascade delete.

My Private Notes

Notes are auto-saved locally to this device.