KB100035

I've been getting a lot of questions about the distinction between aggregation and composition lately. Both deal with part-of relationships and are extremely important concepts to understand.

Let's start with a defition of aggregation from the UML 1.4 glossary:

Aggregation - A special form of an association that specifies a whole-part relationship between the aggregate (whole) and a component part.

Types and CharacteristicsThere are various types and characteristics of aggregation relationsips (whole-part relationships) that describe the relationship between the whole and part in more detail. Lets continue with a few more definitions related to aggregation sub-types and characteristics (the Composition definition is from the UML 1.4 glossary):

Composition / Composition Aggregation - A form of aggregation which requires that a part instance be included in at most one composite at a time, and that the composite object is responsible for the creation and destruction of the parts.

Shareable Aggregation - A form of aggregation in which the part can exist on it's own and is therefore shareable among multiple owner classes.

More On Aggregations ("has a" relationship)In an aggregation relationship, the part may be independent of the whole but the whole requires the part. An aggregation relationship is indicated in the UML with an unfilled diamond and a line as follows:

An Aggregation is an Association which denotes an "is part of" relationship. Unfortunately, the definition of this relationship is quite lax, so basically everyone is using his own interpretation. The only definitive (?) property is that in an instance graph, aggregations are not allowed to be circular - that is, an object can not be "a part of itself".

Composition (a strong type of "uses a" relationship)A composition relationship, also known as a composite aggregation, is a stronger form of aggregation where the part is created and destroyed with the whole. A composition relationship is indicated...

-In aggregation relationship, the ‘part’ object reference can be re-used & creation of ‘part’ object is not the responsibility of the ‘whole‘object usully. It would have been created somewhere else, and passed to the ‘whole’ object as a method argument.

-In composition, the life cycle of the ‘part’ is controlled by the ‘whole’.

So, if I am right then your pseudo code examples would need to get swapped within each other.

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.