Look at the following example:
The class named Fruits forms a composition with the class named Apple. That means that apples will be only instantiated when some fruit object is instantiated. Now my question is, if apples are only instantiated when fruits are instantiated how is it possible that a third class "Tasty Fruits" can access apple objects with an association relation if apple objects are inside fruits?
Wouldn't that association relation contradict the composition? I mean, in order to access apples, tasty fruits would need to access fruits as apples are inside fruits. I don't understand how and why this UML diagram is fine.
The composite aggregation means that Apples
components of a Fruit
composite have their lifecycle managed by the Fruit
that owns them.
This does not prevent Apples
to be associated directly with other classes. Apples
could even exist without being owned by Fruit
if the multiplicity on the fruit side would be 0..1
.
That being said, the diagram would be ok, with classes such as A, B, C. But it is very confusing, with the classes chosen : in real life an Apple
is a Fruit
, and a Fruit
instance (e.g. a given strawberry) is not composed of one or more apples. Similarly, a Tasty Fruit
is also a Fruit
and the association with Apple
is as weird as a the composition.
In other words, the issue is not the UML rules, but the semantics usually associated with the chosen classes.