UML Multiplicity

UML Multiplicity Categories
|
Mandatory |
Optional |
Single Value |
[1] |
[0..1] |
Multivalued |
[1..*] [2..4] |
[*] [0..*] [0..2] |
Mandatory & Optional Multivalued
Optional Single Value

Optional Single Value: C++ Pointer

Solution: Flag
- For each optional, have to maintain another flag variable
- Have to remember the relationship between
name
and hasName
- C++17 feature, similar to
boost::optional
- Can be used on any value type (non-pointer)
- Allows for lazy loading/allocation
- Complete replacement of having to use pointers for optional single-value attributes
Use Case: Represent Optional Types
Takeaway
- Use
std::optional
where you are mapping from an Optional type in UML
- Use
std::optional
in all cases where pointers are used to represent optional values