Aggregation versus Composition

From CSSEMediaWiki
Jump to: navigation, search
Aggregation versus Composition

Wikipedia provides us the university example to illustrate the difference between aggregation and composition.

Association (represented by a solid line, with an arrow if unidirectional) occurs when one object instance causes another to perform an action on its behalf. Associations are typically implemented by references.

Aggregation (represented by the hollow diamond) occurs in the case where the destruction of one object does not end the existence of the other. An Aggregation is an Association which denotes an "is part of" relationship. In this example, when the university closes the professors continue to exist.

Composition (represented by the solid diamond) occurs inside objects when the relationship is such that if the super-object is destroyed the sub-object also ceases to exist. A Composition adds a lifetime responsibility to Aggregation. In this example, if the university is closed the departments also close.

It is important to grasp the full implications of the Composition "lifetime responsibility" relationship. In order for the 'whole' to be responsible for the 'parts' lifetime, it must be able to control/limit access to the part by other objects; after all, if there are several objects sharing access to the part, the whole cannot unilaterally destroy it. This generally means that the whole object is the only object that sends messages to the part, access to the part is not shared.

Martin Fowler's advice

"Composition is a good way of showing properties that own by value, properties to value objects, or properties that have a strong and somewhat exclusive ownership of particular other components. Aggregation is strictly meaningless; as a result, I recomment that you ignore it in your own diagrams. If you see it in other people's diagrams, you'll need to dig deeper to find out what they mean by it. Different authors and teams use it for different purposes" -- Martin Fowler, UML Distilled: A Brief Guide to the Standard Object Modeling Language, 3rd ed., Addison-Wesley.

Personal tools