How to *really* write UML cardinalities?
Asked Answered
G

3

32

I would like to know once and for all how to write UML cardinalities, since I very often had to debate about them (so proofs and sources are very welcome :)

If I want to explain that a Mother can have several Children but a Child has one and only one Mother, should I write:

Mother * ---------- 1 Child

Or

Mother 1 ---------- * Child

?

Guthrun answered 18/7, 2013 at 13:45 Comment(1)
If you are already familiar with the Merise notation, well, UML does it just the other way round for cardinalities. Hence your first example would be correct in a Merise diagram.Linear
L
34

the second one Mother 1 ----------------- 1..* Child

You would find many example in the UML specification for all figure related to the Abstract Syntax...

Lucchesi answered 18/7, 2013 at 14:1 Comment(0)
G
26

Of course Red Beard is right, the correct answer is the second one.

As for a tip for remembering this, I advise to think in english: You say "A child has ONE mother", and in this sentence like in UML, ONE is written next to Mother. Fairly simple.

Gree answered 18/7, 2013 at 19:2 Comment(1)
Nice way to remember this :+1:Mcgurn
G
2

Many people have this question when they start using UML, especially when they come from another notation where the names are always read clockwise, regardless of which end of the line they're on. That's really confusing!

Red Beard is correct, although the UML spec does not explicitly state where association-end information (i.e., name and multiplicity) is written, it implies it in several places. For example, Figures 7.11 (showing attributes) and 7.12 (showing unidirectional associations with association ends next to the arrowheads) are equivalent property notations; thus, the multiplicity does indeed go next to the property's type.

One way I learned to remember which end has which multiplicity is to imagine a unidirectional graph of instances and write the number next to the arrowheads that point at the target.

BTW, you should use descriptive association end names. These often turn into attribute names in Java, element names in XSD, and so on. For example, in Java, the Mother class might have a "children" attribute of type "Set<Child>". If you don't name them, you'll often get undesirable default names.

Gleam answered 18/7, 2013 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.