Suppose that there is one container C that can contain objects of type A and B. C cannot be empty and can only contain 1 object of each type. That means that C contains one of the following:
To map this problem in UML class diagram I tried these...
... but none of them map the problem since that in the left diagram the container C can be empty and in the right diagram the container C can contain two objects of type A (or B).
So my question is: how is it possible to map this problem?
The solution to your problem is indeed using UML constraints as explained by Javier. But in this particular case, there is a predefined UML constraint called {or}
that does exactly what you want. The "or" constraint is predefined in UML and can be graphically written between associations. Note how 0 is removed from the multiplicities because {or}
means that one of the aggregations may not be present at all.
P.S 1: For a similar example on {xor}
see page 302 and 303 of UML 2.0 Toolkit by OMG Press Book.
P.S 2: As far as I know, the notation you are using that branches an aggregation is not correct UML notation (or at least has a different meaning). Joining together multiple relationships is an alternate notation applicable to generalization.
P.S 3: Of course you could use OCL or natural language for your constraint if you find it more descriptive.