There is a class A
, an IB
interface, and a class B
that implements an IB
interface. Class A
has a field of type IB
. During program execution, the value of this field becomes an instance of class B
. How to draw a class diagram for this situation? Is there a “composition” relationship between A
and IB
, and is composition a relationship between A
and B
, or just a dependency?
Asked
Active
Viewed 258 times
3

Christophe
- 68,716
- 7
- 72
- 138

Anton Minchenko
- 421
- 5
- 11
1 Answers
3
It depends on the language and/or the semantics that you want to give to your construct.
In a language with reference based classes like java:
- the instance of
B
could always be shared and therefore continue to live after the death of the A object. This is in contradiction with the composition in UML. - an aggregation would allow B to be shared. But unfortunately, the aggregation semantics are not well defined in UML, and is therfore ambiguous.
- a normal association would be correct and unambiguous.
In a language with value based classes like C++:
- the composition would appropriately represent the joint lifecycle of the objects
- in your case however, IB would be polymorpic. Polymorphism would require a pointer. If you'd use a
unique_ptr<IB>
you would express a composition. Other pointers might allow sharing of objects, so the normal association would be a better representation.
But UML is not a programming language. It's a modelling language. So you should express in the model the semantic that you want. If IB objects are not expected to be shared, and should not outlive A, then composition appropriate shows this intent. If not, remain open.

Christophe
- 68,716
- 7
- 72
- 138
-
`the instance of B could always be shared and therefore continue to live after the death of the A object`. Wouldn't the garbage collector delete object `B` if only `A` owned the link to it? I don’t know how in Java, but in .Net it worked so. Based on this, is the composition here? What is the connection between class `A` and the interface? Composition or association? Or dependency? – Anton Minchenko Mar 04 '20 at 22:49
-
@AnthonyMinchenko yes, if only one A instance would have the handle for a B instance, the garbage collector would get rid of both. But the problem is that the langugage construct alone does not guarantee that there is no other A instance (or even an X instance) that refers to the same B instance. So translating the language construct with a composition would make assumptions that cannot be verified. This i why I insist on the design intent: If you want that a B instance is not shared, then you can use composition to express this intent. – Christophe Mar 04 '20 at 22:56
-
but if an object of class B is created only inside class A, is its field and there is no access to objects of other classes to it, then is this an obvious composition? – Anton Minchenko Mar 04 '20 at 23:01
-
@AnthonyMinchenko Let's say it the other way round: if I'd have an UML diagram with a comopsition, I'd certainly start impelmenting it as you've described. Infering from a language construct the design is not straight forward: if a B is created only inside an A, and if A has a [copy constructor](https://stackoverflow.com/a/869078/3723423) I might end up with several A's sharing the same B. – Christophe Mar 04 '20 at 23:05
-
Having carefully analyzed my code, I noticed that I was using the `Mediator` pattern (also known as: `Intermediary`, `Controller`) to reduce the connectivity of many classes to each other, due to the movement of these links into one mediating class. Thus, there is no rigid connection between classes `A` and `B`. Here goes composition `B` with the `Mediator` and the dependence of a on the mediator. – Anton Minchenko Mar 04 '20 at 23:27