Enumeration in UML class diagrams

Enumeration is a useful concept in programming that refers to creating datasets or lists of related data values. Enumerations are helpful in programming to make our code readable and understandable by classifying similar data in a single enumeration.

Using enumeration in UML class diagram

UML is a language that helps us make diagrams to visualize our software systems to understand their behaviors and relationships. A class diagram in UML helps us understand the relationship between classes, their properties, methods, and connections.

Note: To learn more about a class diagram, you can refer to this Answer.

Enumeration representation

We represent the enumeration in UML by a rectangle with two sections: the enumeration name and the data section. 

Enumeration in a class diagram
Enumeration in a class diagram
  • <<enumeration>>: The keyword mandatory to use in the enumeration to make it different from the other classes.

  • EnumerationName: The name of the enumeration.

  • value1, value2, value3: The distinct data values of the enumeration also known as enumeration literals.

Note: An enumeration is considered meaningful if it consists of a minimum of two enumeration literals.

We can define values for the enumeration literals in an enumeration as well.

Enumeration with defined values in a class diagram
Enumeration with defined values in a class diagram
  • Here the name of the enumeration is Day. It consists of seven enumeration literals defined with some values for each one.

Example

Let's consider an example of an enumeration for storing the types of a task based on its current status. The name of the enumeration is TaskStatus, consisting of enumeration literals: Pending, InProgress, and Completed.

Example of an enumeration
Example of an enumeration

Now let's understand how we can associate this enumeration with an attribute of a class.

Association of an enumeration with a class
Association of an enumeration with a class

In the above diagram, we have a class Task, containing three public attributes: name, dueDate, and status. The status attribute can have any one of the values present in the enumeration TaskStatus. We connect our enumeration with the Task class using an association.

Defining enumeration inside a UML class

We can define an enumeration inside a UML class using the following UML representation.

Defining enumeration in a class
Defining enumeration in a class

Now we can directly define the enumeration values for the status attribute in the Task class using a containmentIt represents how one class contains or is composed of instances of another class. relationship.

Defining direct enumeration values in a class
Defining direct enumeration values in a class

Conclusion

The enumeration in UML class diagrams offers a powerful tool for representing distinct values within a class structure. With enumerations, UML class diagrams become clearer, helping us design and build our software systems efficiently.

Copyright ©2024 Educative, Inc. All rights reserved