Shame on me, but I have to admit that yesterday I had doubts about what is aggregation and composition. As everyone of us would do I started googling and found some of very useful and helpful explanations.

Two examples to start with

For example, we have table in room, we say that room aggregates table. Table can live without parent object (room).

Example of composition would be college composing faculties. Faculty cannot live without college.

Challenge

When I talked about this two examples with my friend he challenged following verification:
“If child cannot live without parent we have composition and when child can live without parent we have aggregation.” He gave me example of queue build upon list, he stated that this always was an example of composition to him. In this case queue has field _innerList (or something like that), but indeed being child list can be moved out of queue and conceptually we can operate with different lists in our system. So the rule of child-parent doesn’t apply. Even more it is applicable vise verse – queue (parent) cannot live without list (child).


So I found following to be the best way to distinguish:

If during lifetime of parent its child element(s) should exist to have consistent system, then this is COMPOSITION.
If above statement is not satisfied, but at the same time parent can contain child element(s), then this is AGGREGATION.

Relational/UML point of view

If you would read this wiki  page. You will also find that aggregation is more general term then composition. They explain this from a bit another point, point of relations.

Above relation on the picture shows composition and is marked as filled diamond, below relation shows aggregation as more generalization of composition, without restriction to have child elements (Monday our pound is full of ducks and Tuesday there is no ducks at all).

I would love to hear from you thoughts on how we can help others distinguish this more quicker.