Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 39 ยท Class and Object Diagrams

Aggregation, Composition and Dependency

Understand the whole-part relationships of UML: aggregation, composition, and the weaker dependency, with diamonds, lifetimes, examples and code that shows the difference.

An association is neutral: two classes are linked. Often there is more to say. A car has an engine; a university has departments; a method uses a logger. UML offers three refinements, each with its own symbol: aggregation, composition and dependency. They differ mainly in how tightly the lifetimes of the two sides are coupled.

Aggregation is drawn as an association with a hollow diamond at the whole's end. It says: the whole has parts, but the parts can exist independently.

classDiagram
    class Team {
        +String name
    }
    class Player {
        +String name
    }
    Team o-- Player : members

A Team has Players. If the team disbands, the players still exist and may join another team.

Aggregation is deliberately vague. The UML specification gives it no extra semantics beyond a plain association, and many modelers skip it altogether. Use it when the "has" idea matters for readers; do not spend time agonizing over it.

Composition is drawn with a filled diamond at the whole's end. It means the part belongs to exactly one whole and lives and dies with it: deleting the whole deletes the parts.

classDiagram
    class House {
        +String address
    }
    class Room {
        +String name
    }
    House "1" *-- "1..*" Room : contains

A Room does not exist outside its House. Destroy the House and its Rooms go too.

Properties of composition:

  • The multiplicity on the whole's end is 1 (or 0..1): a part has only one owner at a time.
  • The whole is responsible for creating and destroying its parts.
  • Parts are not shared. If two wholes need the same object, it is aggregation or plain association.
Aggregation Composition
Symbol hollow diamond filled diamond
Part outlives the whole yes no
Part shared between wholes possible never
Typical example Team and Players Order and its Order Lines

A useful test: "If I delete the whole, should the part still make sense?" A deleted Order makes its OrderLines meaningless (composition). A deleted Playlist does not erase the songs (aggregation or plain association).

A dependency is a dashed line with an open arrow from the client to the supplier. It means: if the supplier changes, the client may have to change. There is no stored reference; the usage is momentary.

classDiagram
    class ReportPrinter {
        +print(Report r)
    }
    class Report
    class Logger {
        +log(String msg)
    }
    ReportPrinter ..> Report : uses
    ReportPrinter ..> Logger : uses

Typical sources of dependency:

  • A method parameter or return type of another class.
  • A local variable of that type.
  • A call to a static method of another class.
  • An object created inside a method (stereotype <<create>>).

Dependencies can carry stereotypes: <<use>>, <<create>>, <<instantiate>>, <<import>>.

Ranking the relationships

From strongest coupling to weakest:

  1. Generalization: child is bound to the parent's internals.
  2. Composition: parts cannot exist without the whole.
  3. Aggregation: parts exist independently.
  4. Association: a stored link between objects.
  5. Dependency: a momentary use.

A good design uses the weakest relationship that tells the truth. Fewer and weaker links mean a system that is easier to change.

Whole and part at a glance

Aggregation Hollow diamond. Parts live on.
Composition Filled diamond. Parts die too.
Dependency Dashed arrow. Just uses.

Test it by deleting the whole.

1 / 2

Seeing the difference in code

Python Composition: the whole creates and owns its parts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Aggregation: parts are passed in and outlive the whole

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The code difference is where the parts come from. In composition the whole constructs them. In aggregation they are passed in and merely referenced.

Python Dependency: used inside a method, never stored

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Diamonds on the wrong end. The diamond sits at the whole, not the part.
  • Composition everywhere. Overusing the filled diamond claims lifetime control you may not have.
  • Treating dependency as association. If there is no stored reference, use the dashed arrow.
  • Confusing aggregation with inheritance. "Has a" is not "is a".
  • Cycles of dependency. If package A depends on B and B on A, redesign; this is a code smell in any diagram.

When unsure between aggregation and plain association, choose plain association. Many experienced modelers do, because the hollow diamond adds little precise meaning.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where is the diamond drawn?

    The diamond touches the class that represents the whole.
  2. 2. Which relationship means the part is destroyed when the whole is destroyed?

    Composition (filled diamond) ties the lifetime of parts to the whole.
  3. 3. A method takes a Logger as a parameter and does not store it. Which relationship fits?

    A momentary use with no stored reference is a dependency (dashed arrow).
  4. 4. Which pair is the best example of composition?

    Order lines make no sense without their order, and belong to exactly one order.
  5. 5. Which is the weakest relationship?

    Dependency is the loosest coupling: a momentary use.

Summary

  • Aggregation (hollow diamond): has-a, parts exist independently, very loose meaning.
  • Composition (filled diamond): parts belong to one whole and die with it.
  • Dependency (dashed open arrow): the client merely uses the supplier.
  • In code, composition constructs parts; aggregation receives them; dependency uses them briefly.
  • Use the weakest relationship that tells the truth.

Next: Object Diagrams