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.
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.
We represent the enumeration in UML by a rectangle with two sections: the enumeration name and the data section.
<<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.
Here the name of the enumeration is Day. It consists of seven enumeration literals defined with some values for each one.
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.
Now let's understand how we can associate this enumeration with an attribute of 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.
We can define an enumeration inside a UML class using the following UML representation.
Now we can directly define the enumeration values for the status attribute in the Task class using a
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.