Understanding UML composition better
Posted
by
Prog
on Programmers
See other posts from Programmers
or by Prog
Published on 2014-05-26T17:49:33Z
Indexed on
2014/05/26
22:00 UTC
Read the original article
Hit count: 472
The technical difference between Composition and Aggregation in UML (and sometimes in programming too) is that with Composition, the lifetime of the objects composing the composite (e.g. an engine and a steering wheel in a car) is dependent on the composite object. While with Aggregation, the lifetime of the objects making up the composite is independent of the composite.
However I'm not sure about something related to composition in UML.
Say ClassA is composed of an object of ClassB:
class ClassA{
ClassB bInstance;
public ClassA(){
bInstance = new ClassB();
}
}
This is an example of composition, because bInstance
is dependent on the lifetime of it's enclosing object.
However, regarding UML notation - I'm not sure if I would notate the relationship between ClassA and ClassB with a filled diamond (composition) or a white diamond (aggregation).
This is because while the lifetime of some ClassB
instances is dependent of ClassA
instances - there could be ClassB
instances anywhere else in the program - not only within ClassA
instances.
The question is: if ClassA
objects are composed of ClassB
objects - but other ClassB
objects are free to be used anywhere else in the program: Should the relationship between ClassA
and ClassB
be notated as aggregation or as composition?
© Programmers or respective owner