5

I am having a hard time understanding these when it comes to designing a class diagram, from what I know composition is a "has-a" relationship but Aggregation ?? I have no idea what is it. and one thing when will I know to use Composition in a class diagram? and when will I know to use Aggregation in a class diagram?? a example will be highly appreciated.

skaffman
  • 398,947
  • 96
  • 818
  • 769
user962206
  • 15,637
  • 61
  • 177
  • 270

2 Answers2

25

The main difference between an aggregation and a composition is the property of your "has-a" relationship. It' either strong or weak.

The aggregations "has-a" relationship is of "weak-type". Weak meaning the linked components of the aggregator may survive the aggregations life-cycle or may be accessed in some other way. A simple example would be a football club with its members. If the club is dissolved, you still got the members - which in fact could also be members of other clubs and thus are kept alive.

The composition "has-a" relationship is of "strong-type". Strong meaning that one can't exist without the other. The component's life-cycles are directly linked to the "parent". An example would be a house with rooms. If you decide to tear the house down, you will also lose your rooms.

Maybe a little abstract but I think that's the idea behind it.

Ashley
  • 3
  • 2
chrismoe
  • 266
  • 3
  • 2
0

See answers to previous questions here, here and here.

Personally I don't use Aggregation. The semantics are too weak to be useful. It causes more problems than it solves. There's only one place where it has well-defined and potentially useful properties that distinguish it from a simple binary association (see footnote).

Composition can be useful because it defines important properties on immutability and lifecycle management (see 1st link above). I've survived quite happily for many years without ever needing to use Aggregation.

hth.

Footnote. Aggregation can be applicable if you need to model a recursive tree relationship, e.g. a part decomposed recursively into sub-parts. Aggregation says there can be no circular relationships, i.e. a part can't be a sub-part of itself - directly or indirectly. However most people reading the model are unlikely to know that. -So you'd need to add a comment. Which means you'd be as well to stick with a binary association and avoid the confusion of using Aggregation.

Community
  • 1
  • 1
sfinnie
  • 9,854
  • 1
  • 38
  • 44