Tricky thing to model

I’d like to model a class which aggregates three optional member classes, whereby at least one member shall exist.

How can I do that?


According to the UML you can apply constraints to one or more model elements, including associations. This is modelled using a dashed line between elements. In your case you could show a dashed line joining all the aggregation lines. Usually the constraint is shown in “{}” and can be expressed in human readable form, e.g. english, or machine readable form, e.g. OCL. Constraints can be named but, according to the spec, are commonly unnamed.

There’s a standard association constraint defined in the UML that may apply. It is the {xor} constraint and seems to fit the bill. Presumably it means that one of the associations exist but not more than one. I read that as exactly one of the associations shall exist. Alternatively you can express it in natural language.

Now the tricky part, I have yet to find a tool that allows you to do this properly but Visual Paradigm has a constraint you can apply across associations. I could only get it to apply one across two associations and the help is all screwed up so I can’t search for how to do it across more than two. The VP people might be able to help.

Nah, use generalization. The aggregating class includes 1…3 of class X which is a superclass of the 3 aggregated classes.

I hate putting English words on UML diagrams (except to name things of course). The major point for me is to get away from describing things with ‘English’ text. UML should be independent of any spoken language.

Les. :roll:

Let me assume the possible allowed combination is 0-0-1, 0-1-1, 1-1-1 for the objects of 3 aggregated classes. Then, you put 3 aggregation symbols with multiplicity of 0…1. Additional OCL should be added between 3 aggregation symbols to ensure at least one object is included.