UML Enumeration (in UML Class Diagram)

An enum allows you to define an entity with a list of literals in the UML class diagram. UML Enumeration values can be used by class attributes.

What is Enumeration in UML Class Diagram?

A UML enumeration represents a complete list of all values that a given type may acquire. An enumeration is used as a type of attribute, operation, and operation parameters. Enums may be used in a flag mode to support bitwise combinations of particular values.

An enumeration is depicted as a rectangular box with a name and a line-separated list of items. The enumeration element is decorated with the «enumeration» stereotype. The stereotype caption allows us easy to differ from other element types (e.g. classes).

Benefits of Using Enumerations

Enumerations (or enums, as they are commonly known) play a crucial role in enhancing the clarity, reliability, and maintainability of software models and code. Integrating enumerations into UML class diagrams and subsequently into the software development process offers several key benefits:

  • Improved Code Readability and Maintainability: The descriptive approach of enums makes it easier for developers to understand the intent of the code, reducing the cognitive load and making maintenance tasks less error-prone.
  • Type Safety: Enums in UML contribute to type safety by defining a fixed set of constants. This allows developers to restrict the values that an object can take, which in turn, significantly reduces errors.
  • Grouping Related Constants: Enums provide a way to group related constants together under a single type. This grouping not only helps in organizing the code better but also in implementing logic that relies on related sets of values, such as handling different user roles, product categories, or command types in an application.
  • Reducing Magic Numbers: By replacing numeric literals or strings (often referred to as "magic numbers" or "magic strings") with enums, the code becomes less prone to errors and more descriptive. Enums ensure that only valid values are used, and they make the code easier to refactor since the dependency is on the enum type rather than on specific literal values.
  • Support for Bitwise Operations: In scenarios where multiple conditions may apply simultaneously, enums (especially when designed to support flag mode) can be used with bitwise operations to efficiently combine or check for multiple states at once.

Enum Values

An enumeration is represented by its values. A meaningful enumeration should contain two and more values.

UML Enumeration Literal

An item of enumeration is also called enumeration literal. It is represented by its name. Optionally, a numeric or other type value for the literal may be defined.

How to Add Values to Enum

In the diagram editor, you can add other enumeration items using the context bar. First, create a new enumeration or select an existing one. Then click on the Add Item button in the context bar (usually placed below the selected enumeration). A new item will be added to the enumeration and you can enter its name.

Enumeration Relationship

An enumeration connects to a class using a UML association relationship. The relationship says whether an attribute, operation, or operation parameter within a connected classifier uses the enumeration as its type. Association from a classifier to an enumeration may be omitted. UML enumeration can be used simply as a data type for attributes, operations, and parameters without an association line in the diagram.

An enumeration associated with a class (UML Class Diagram)
An enumeration associated with a class (UML Class Diagram)

UML Enum Association

An enum may be associated with a class or a class field (attribute, operation) in our UML tool. If you drag (using the right mouse button) the enumeration and drop it over an attribute, it will be set as its type.

Enumeration in UML Class

An enum can be defined within a UML class. This may be modeled using containment relationships in the UML class diagram.

An enumeration nested in a class (UML Class Diagram)
An enumeration nested in a class (UML Class Diagram)

Enumeration Items with Defined Values

Specific values can be assigned to enumeration items. The value of the enumeration item is displayed after the item name and the equals sign =. You can enter the specific value to an enumeration item very easily - just edit the item in the diagram editor and append = value after the item name.

Enumeration Items with Values
Enumeration Items with Values

Another way is to open the Properties dialog (right-click on the desired enumeration and select Properties from the context menu) and switch to the Items tab. It offers a tabular editor with two columns - Name and Value. You can edit the particular enumeration items there.

Enumeration Items Editor in Properties Dialog
Enumeration Items Editor in Properties Dialog

Enumeration Example

The following example shows how to represent an enum in class diagrams. Check out our UML class diagram example with an enumeration element:

Example of an enumeration in a class diagram
Example of an enumeration in a class diagram

Create Enumeration in Software Ideas Modeler

The UML tool Software Ideas Modeler allows you to create and use enumerations in your diagrams intuitively.

To create and use enumerations in your UML class diagrams, follow these steps:

  • Add Enumeration: In the diagram editor, create a new enumeration or select an existing one.
  • Add Enumeration Values: Click on the "Add Item" button in the context bar to add new enumeration values. Enter the value names.
  • Establish Enumeration Relationship: Connect the enumeration to a class using a UML association relationship.
  • Set Enumeration as Type: Drag the enumeration and drop it over an attribute to set it as the attribute's type.
  • Nest Enumeration in a Class: Define an enumeration within a UML class using containment relationships.

Comments

Keith 2 January 2022 6:47:39

Cool

I am a CS major and just want to say this helped me a bunch thank you. God bless

Dusan Rodina - softwareideas.net 3 January 2022 10:04:16

RE: Cool

Thank you for the positive feedback.

Noura 18 February 2022 15:46:20

Good

The explanation is clear and simple to understand. Thank you

Dusan Rodina - softwareideas.net 18 February 2022 15:50:15

RE: Good

You're welcome!

Abdou 26 October 2023 3:29:58

GOOD

Thank you so much the examples are very helpful!!

Dusan Rodina - softwareideas.net 26 October 2023 7:17:20

RE: GOOD

You are welcome!

lars 19 May 2024 14:03:12

How to do it?

When is an enum either association, aggregation or composition?

For me a enum is a part of a class in the whole lifespan of the class - hense I would depicture it as Composition. However in your examples on this page you only depict them as associations. I find it very confusing.

Dusan Rodina - softwareideas.net 21 May 2024 8:54:44

RE: How to do it?

I believe that in most cases, an Association or a Dependency is more suitable because Enums are global definitions that can usually exist independently of the class lifespan.

New Comment

Comment