4me

If the planet was going to break up–all right. But while I was alive I would go on living at the best speed I could manage. – “Future Imperfect”

Aggregation v.s. Composition (a review on UML)

Posted by tcle on May 20, 2008

Aggregation

Aggregation is a kind of association that specifies a whole/part relationship between the aggregate (whole) and component part. This relationship between the aggregate and component is a weak “has a” relationship as the component may survive the aggregate object. The component object may be accessed through other objects without going through the aggregate object. The aggregate object does not take part in the lifecycle of the component object, meaning the component object may outlive the aggregate object. The state of the component object still forms part of the aggregate object.

An example of aggregation is a History-Class object contains zero or more of Student objects. The state of each Student object has an influence on the state of the History-Class object. If the History-Class object is destroyed, the Student objects may continue to exist.

The UML diagram above illustrates the aggregation association line with an open diamond on the aggregate side.

Composition

Composition is a kind of association very similar to aggregation except where the composite object has sole responsibility for the disposition of the component parts. The relationship between the composite and the component is a strong “has a” relationship, as the composite object takes ownership of the component. This means the composite is responsible for the creation and destruction of the component parts. An object may only be part of one composite. If the composite object is destroyed, all the component parts must be destroyed, or the reference and responsibility of the component part must be handed over to another object. Composition enforces encapsulation as the component parts usually are members of the composite object.

An example of composition is a House object contains zero or more Room objects. The state of each Room object has an influence on the House object. If the House object is destroyed, the Room objects will also be destroyed.

The UML diagram above illustrates the composition association line with a solid diamond on the composite side.

Source: http://www.javadesign.info/DesignConcepts/OOConcepts/aggregationcomposition

14 Responses to “Aggregation v.s. Composition (a review on UML)”

  1. Jonathon said

    Thanks, this explained the concept perfectly. I’d been reading UML Distilled by Martin Fowler and even though it’s a good book it really did not explain the differences as clearly.

    or maybe I’m just slow 😛

  2. root said

    loud and clear!thanks!

  3. Vishnu Parandhaman said

    Good explanation. Thanks for it.

  4. Naman said

    Very clear and unambiguous explanation. Thanks

  5. saboor said

    nice 🙂

  6. Inernal Gear said

    Thanks, this is a very good, succinct explanation that was lacking from my textbook and my professor…

  7. Root said

    Clear and good explanation.Thx

  8. Peter said

    Thank you!

  9. PUK said

    Thank you – this cleared up the difference for me.

  10. Younes said

    perfect…you make my day

  11. Tay Mon said

    Very good explanation…Thanks.

  12. sivaram said

    fine & easy to understand

  13. sivaram said

    fine $ Easy To Understand

  14. Rohit Suthar, Mumbai said

    It’s a nice explation.
    Thanks!

Leave a reply to PUK Cancel reply