0

In some class diagram of several design patterns, like decorator pattern for instance, I see an aggregation (between decorator-component) but, in the code, instead having a collection, there is a single variable declared of an interface type.

Why it is an aggregation instead of an association? Thank you very much

Alberto
  • 339
  • 4
  • 12
  • You would have to show the diagram you are referring to and also state why you think a collection is needed since a decorator decorates a single component. IMO, there should be composition between decorator and component since they most likely have the same lifecycle, unless you allow to swap the inner component of a decorator. – plalx Mar 20 '17 at 02:55

1 Answers1

0

Aggregation is just a form of association. It is commonly defined as a whole-part relationship and this leads to the assumption that aggregation is a one-to-many relationship, which is not necessarily true. Any association or dependency can be a one-to-many relationship, but can also be a one-to-one relationship. Aggregation is no different.

The difference with aggregation is more colloquial, and since many people will assume it means one-to-many, you may choose to use it that way; but there is nothing inherently incorrect in a one-to-one aggregation.

You can see some slightly differing views in these references.

Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83