Association vs. Aggregation [duplicate]

There are four kinds of Class relationships

  1. Association: uses a
    Ex:a Class Man uses a Class Pen ( Pen is still there when man die )
  2. Aggregation: has a
    Ex:a Class Man has a Class Car ( Car is still there when Man die )
  3. Composition: owns a
    Ex:a Class Man owns a Class Heart ( When Man die, Heart die )
  4. Inheritance: is a
    Ex:a Class Man is a Class Human ( Man is a Human )

A relationship between classes of objects

Inheritance>Composition>Aggregation>Association


Association means two classes have some kind of relationship, could be anything really.

Composition and aggregation are two kinds of associations. The easiest way to distinguish them is thinking about "how hard" the relationship is. Think about what happens when you delete the owner object.

Aggregation, the aggregated object continues to live. (Think order <-> product, the product continues to live).

Composition, the aggregated object dies with the owner. (Think paragraphs <-> document, the paragraphs die with the document).

An aggregation can be argued to be meaningless since there isn't really much difference between drawing a line with an non-filled arrow (association), and a line with a non-filled diamond (aggregation). The relations are very similar. Line with filled diamond (composition) is however very different.


This is a very arguable question. As Martin explains in the answer, the Order aggregates the Product. And this can be considered true. Grady Booch in his "Object-Oriented Analysis and Design" brings a similar example for association - a sale is associated with products in that sale, and vice versa. And a sale doesn't aggregate products. So all examples should be domain-specific, because from another point of view the association may become more specific. Another example is the composition of Documents using Paragraphs.

So everything in this field strongly depends on the context. This is the OOP.

You can try to apply your knowledge to a particular project you are going to design. I would recommend you to read Grady Booch's book, if you haven't done it yet. Lots of books have been written since it, but it is still the Bible of OO*.