0

I want to model an XML schema that has the following composition relationships:
A can contain B, and in another case B can contain A.

Using a bi-directional composition is not an option here as it is not allowed by UML and does not reflect the two different cases of inclusions.

Is using two compositions, as the followings is considered a valid UML representation?

enter image description here

1
  • Tricky one. At first glance I thought: obviously wrong. But following the below discussion...
    – user188153
    Commented Aug 17, 2019 at 20:56

2 Answers 2

1

Yes, this is a valid class diagram. UML does not have any restrictions on the number of associations between two classes, no matter the types of the associations.

You only specified multiplicities on one side of the association. This means that the multiplicities on the diamond side of the associations are undefined (in old versions of UML, an absent multiplicity implied a multiplicity of 1, but this is no longer the case). For completeness sake, you could add them, but be careful, because if they were both equal to 1, you would get an infinite chain of instances of A and B, which is not what you want. I guess the multiplicities should both be 0..1, but I do not have enough information to be sure.

5
  • Nope. Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (parts). The diagram is clearly a contradiction to the spec.
    – user188153
    Commented Aug 17, 2019 at 20:40
  • The diagram does not contradict this at all. Maybe the A's that contain B's are completely different instances than the A's contained in B's. Commented Aug 17, 2019 at 20:47
  • Hmm. There you might have a point :-/
    – user188153
    Commented Aug 17, 2019 at 20:48
  • +1 exactly! A and B can have different instances with different relationships. In my case '0..1' multiplicity is the correct one (thanks for the extra tip). But I don't see how '1' would lead to an infinite chain, given the fact of different instances of A's and B's involved in the two different associations. Commented Aug 18, 2019 at 8:14
  • A 1 on the diamond side of both assocations means that every instance of B is contained in one instance of A and every instance of A is contained in one instance of B. Commented Aug 19, 2019 at 7:26
0

No. That's just the same as using one line with two diamonds on either end. The semantics of each one "owning" the other is simply not possible (and therefore forbidden as per UML spec). If A kills B it kills itself since when B dies it has to kill A. Just go with a simple association here.

P. 128 of UML 2.5:

composite | Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (parts).

Thinking twice over your "one case and another" you might add a constraint {xor} connecting the two connectors.


P.S. www.admiraalit.nl might as well be right. The UML spec is not talking about instances specifically and does not detail cases where you have distinct non-recursive compositions (like he says a:A *- b:B *- a1:A etc.). Might be a documentation issue?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.