2
votes

Is there a way in a UML class diagram to indicate how many instances of a given class will be present in your system?

I know you can indicate the multiplicity of a relationship between classes:

Dog * ----------- 1 Yard 1 ----------- * Tree

But is there a common way to visually depict that there is exactly say, five instances of Yard in the model?

2

2 Answers

2
votes

You cannot express this directly with UML bit with OCL (Object Constraint Language). It would be an invariant of Yard like

context Yard inv: Yard.allInstances()->size()=5

OCL is a language designed to express formal constraints when modelling with UML.

0
votes

According to this explanation on UML multiplicities, it is perfectly valid to use any natural numbers for the number of elements. Also, if the lower bound is equal to the upper bound, you can describe them by using just one number (e.g. 1..1 is equivalent to 1)

So you can for example have:

Yard 1 ----------- 5 Tree