Object-Oriented Programming

UML Multiplicity

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

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

C++ Pointer Use

3 states of char*

std::string Record

2 States of std::string

Solution: Flag

  • For each optional, have to maintain another flag variable
  • Have to remember the relationship between name and hasName

Solution: std::optional

  • 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

Scalar Usage

Use Case: Represent Optional Types

Use Case: Error Return

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