Object-Oriented Programming

UML Association & Aggregation

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Properties: Attributes & Association (review)

  • Another notation for property (alternative to attribute)
  • Use attributes for properties that are data types, e.g., primitive types, or whatever is considered a data type in your design
  • Use associations for class types, especially if the methods are of interest at the design level
  • Is String a class or a datatype?


DataType Class
Primitives Objects
Methods are familiar Methods are not familiar
Operators do not change Operators can change
New developers know what you mean New developers have only a vague idea
Tend to be scalar (single value) Tend to be complex types
Equivalent values ⇨ same identity Two distinct objects can have the same value
Probably not a base for generalization (inheritance) Maybe a base for generalization (inheritance)

UML Class Relationships

Relationship Description
Dependency uses a
Generalization is a
Association has a
Aggregation (Association) has a
Composition (Association) has a


  • One class has a(n) element of another class


  • Target belongs only to the source
  • Source (e.g., University) controls the lifetime of the target (e.g., College)

Composition Code

  • Code: fields/data members of
    • direct type
    • containers
    • pointers/references to objects created (and destroyed) internally (discussed later)


  • part of relationship
  • Special kind of association with roles and multiplicities often used
  • Targets may be shared, e.g., a member can belong to many different clubs
  • The source (e.g., Club) doesn’t control the lifetime of the target (e.g., Member)
  • Code: Fields (data members) are pointers/references to this type, and the objects are created externally

Aggregation: C++ References

Aggregation: C++ References Analysis

  • Advantages:
    • Simpler syntax than pointers
    • Sure (?) that the object exists
  • Disadvantages
    • Can only be changed in member initialization list
    • Cannot be directly part of a container (workaround: std::reference_wrapper)
    • References to local objects whose lifetime may disappear

Aggregation: C++ Pointers

Aggregation: C++ Pointers Analysis

  • Advantages
    • Much more flexible, can be changed at any time
  • Disadvantages
    • More complex syntax
    • May be nullptr so have to check before use
    • More difficult to manage lifetime, Who deletes?

Aggregation: std::shared_ptr<>

  • Similar to std::unique_ptr<>, but allows multiple clients
  • As last client is deleted, then the pointer is deleted
  • Easier to manage lifetime, as pointer is shared
  • Suggest as first choice

Composition Can Occur Even With:

  • References:
  • Pointers:
  • Smart Pointers:

Composition with References

Composition with Pointers

Composition with Smart Pointers

Composition vs Aggregation

  • Prefer Composition to Aggregation as lifetime is clearer

  • Aggregation has particular roles in more complex relationships, especially with polymorphic data members

  • Two are often confused and terms used interchangeably