UML Multiplicity and Collections
Multiplicity in UML allows to specify cardinality - i.e. number of elements - of some collection of elements.
Multiplicity element defines some collection of elements, and includes both multiplicity as well as specification of order and uniqueness of the collection elements.
Note, that [UML 2.4.1 Specification] does not separate multiplicity from multiplicity element, mingling and using these two interchangeably and causing some confusion in understanding the specification.
Multiplicity
Multiplicity is a definition of cardinality - i.e. number of elements - of some collection of elements by providing an inclusive interval of non-negative integers to specify the allowable number of instances of described element. Multiplicity interval has some lower bound and (possibly infinite) upper bound:
multiplicity-range ::=
[ lower-bound '..' ] upper-bound
lower-bound ::=
natural-value-specification
upper-bound ::= natural-value-specification | '*'
Lower and upper bounds could be natural constants or constant expressions evaluated to natural (non negative) number. Upper bound could be also specified as asterisk '*' which denotes unlimited number of elements. Upper bound should be greater than or equal to the lower bound.
Note, that [UML 2.4.1 Specification] is not very consistent describing multiplicity. For example, it says that the lower bound must be a non-negative integer literal, and later - that it may be specified by value specification, such as (side-effect free, constant) expression. Another issue is that asterisk as upper bound in one place means unlimited (and not infinity) number of elements while in the other you can read the (possibly infinite) upper bound. You can feel the difference in the two sentences: "Doctor may have unlimited number of patients." and "Doctor may have infinite number of patients."
Some typical examples of multiplicity:
Multiplicity | Option | Cardinality |
---|---|---|
0..0 | 0 | Collection must be empty |
0..1 | No instances or one instance | |
1..1 | 1 | Exactly one instance |
0..* | * | Zero or more instances |
1..* | At least one instance | |
5..5 | 5 | Exactly 5 instances |
m..n | At least m but no more than n instances |
If the multiplicity is associated with an element whose notation is a text string (such as a class attribute), the multiplicity range is placed within square brackets as part of that text string.
Multiplicity of Players for Soccer Team class
If the multiplicity is associated with an element that appears as a symbol (such as use case or class), the multiplicity range is displayed without square brackets.
Two or more Player actors are required
to initiate Play Game use case.
Multiplicity Element
Multiplicity element defines some collection of elements, and includes both multiplicity as well as a specification of order and uniqueness of the collection elements.
Some subclasses of multiplicity element are structural feature, operation, parameter, pin.
Collection properties could be described with the following non-normative syntax rules:
collection-type ::= multiplicity-range [ '{' collection-options '}' ]
Collection options specify whether the values in an instantiation of the element should be unique and/or ordered:
collection-options ::=
order-designator [ ','
uniqueness-designator ] |
uniqueness-designator [ ','
order-designator ]
order-designator ::= 'ordered' | 'unordered'
uniqueness-designator ::= 'unique' | 'nonunique'
Collection Type | isOrdered | isUnique |
---|---|---|
Multiset, bag | false | false |
Sequence, array | true | false |
Set | false | true |
Ordered set | true | true |
If multiplicity element is multivalued and specified as ordered, then the collection of values in an instantiation of this element is sequentially ordered. By default, collections are not ordered.
If multiplicity element is multivalued and specified as unique, then each value in the collection of values in an instantiation of this element must be unique. By default, each value in collection is unique.
Data Source could have a Logger and has ordered pool
of min to max Connections. Each Connection is unique (by default)
Customer has none to many purchases.
Purchases are in specific order and each one is unique (by default)