Association vs. Aggregation [duplicate]
There are four kinds of Class relationships
- Association: uses a
Ex:a ClassMan
uses a ClassPen
( Pen is still there when man die ) - Aggregation: has a
Ex:a ClassMan
has a ClassCar
( Car is still there when Man die ) - Composition: owns a
Ex:a ClassMan
owns a ClassHeart
( When Man die, Heart die ) - Inheritance: is a
Ex:a ClassMan
is a ClassHuman
( 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*.