Aggregation vs Composition vs Association vs Direct Association

The whole point of OOP is that your code replicates real world objects, making your code readable and maintainable.

1. Association

Association is: Class A uses Class B.

Example:

  • Employee uses Bus/train Services for transportation.
  • Computer uses keyboard as input device

And in In UML diagram Association is denoted by a normal arrow head.

2. Aggregation

Class A contains Class B, or Class A has an instance of Class B.

An aggregation is used when life of object is independent of container object. But still container object owns the aggregated object.

So if we delete class A that doesn't mean that class B will also be deleted. E.g. none, or many, teachers can belong to one or many departments.

The relationship between Teachers and Departments is aggregation.

3. Composition

Class A owns Class B.

E.g. Body consists of Arm, Head, Legs. BankAccount consists of Balance and TransactionHistory.

So if class A gets deleted then also class B will get deleted.


Please note that there are different interpretations of the "association" definitions. My views below are heavily based on what you would read in Oracle Certification books and study guides.

Temporary association

A usage inside a method, its signature or as a return value. It's not really a reference to a specific object.

Example: I park my Car in a Garage.

Temporary Association UML

Composition association

A so-called "STRONG relationship": The instantiation of the linked object is often hard coded inside the constructor of the object. It cannot be set from outside the object. (Composition cannot be a many-to-many relationship.)

Example: A House is composed of Stones.

Composition UML

Direct association

This is a "WEAK relationships". The objects can live independent and there are usually setters or other ways to inject the dependent objects.

Example: A Car can have Passengers.

Direct Association UML

Aggregation association

Very similar to a Direct association. It's also a "WEAK relationship" with independent objects. However here the associated objects are a crucial part of the containing object.

Example: A Car should have Tires.

Aggregation UML

Note: Both Direct associations and Aggregation associations are often generalized as "Associations". The difference is rather subtle.


Direct association has nothing in common with the other three. It does not belong to UML at all, it is the IBM requirements modelling term.

As for others,

Association A->B is a child of Dependency. Association means, that A (or its instance) has some easy way to get to instance of B. For example, a.x.y.b. Or by function, or by some local variable. Or by a direct reference or pointer, or something else (there are many languages in the world). As you see, there is no strict border between dependency and association.

One of attributes of Association is Aggregation, it can have values: None, shared (often incorrectly called aggregation), and composition.

If A (or instance) has some (or one) instances of B so, that destroying of association means the destroying of B instances, it is the composition.

If you or a tool author had decided, that some has-a relationship, that is weaker that composition, needs to be specially shown, you can use shared aggregation. Usually it is some collections of references to B in A.

There are some more interesting attributes of associations. Look here if you are interested.