I would like to create an UML diagram with Java enumerations (enum), that have one or more attributes, but I am confused about how to do it.
For example an enum could be declared like this:
public enum Enumeration_type {
LITERAL_A("attr1_value", attr2_value, attr3_value),
LITERAL_B("attr1_value", attr2_value, attr3_value);
final String attr1;
final type_1 attr2 = initial_value_1;
final type_2 attr3;
Enumeration_type(String attr1, type_1 attr2, type_2 attr3) {
this.attr1_value = attr1;
this.attr2_value = attr2;
this.attr3_value = attr3;
}
}
Without the attributes, it is easy:
+--------------------+
| <<enumeration> |
| Enumeration_type |
+--------------------+
| LITERAL_A |
| LITERAL_B |
+--------------------+
But how do you model it with attributes elegantly? Should it be like this?
+-----------------------------------------------------+
| <<enumeration>> |
| Enumeration_type |
+-----------------------------------------------------+
| attr1: String |
| attr2: type_1 = initial_value_1 |
| attr2: type_2 |
+-----------------------------------------------------+
| LITERAL_A("attr1_value", attr2_value, attr3_value) |
| LITERAL_B("attr1_value", attr2_value, attr3_value) |
+-----------------------------------------------------+
I found only this example here, but that uses the String class attributes as enum names. I think, that should be different to usage of public enum
without specifying the enum names data types.
+-----------------------------------------+
| <<enumeration>> |
| CarType |
+-----------------------------------------+
| +sedan : String = SEDAN |
| +liftback : String = LIFTBACK |
| +stationWagon : String = STATION_WAGON |
+-----------------------------------------+
In UML models, enumerations are model elements in class diagrams that represent user-defined data types. Enumerations contain sets of named identifiers that represent the values of the enumeration. These values are called enumeration literals. You can add enumerations to depict discrete sets of values.
An enumerated attribute is an attribute that has a fixed set of possible values. For example the tag ul has the attribute type which could have the values: disc , square and circle .
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.
The keyword 'enum' is used to declare new enumeration types in C and C++. Following is an example of enum declaration. // The name of enumeration is "flag" and the constant // are the values of the flag.
I don't think you can model the attribute values for each of the enumeration literals in UML.
EA uses the following notation for an enumeration that has attributes:
You can either document the attributes values somewhere in the notes of each literal, or you could use an excel file or something similar to manage that data.
Often you are only required to provide the initial values at design-time as they might change at run-time. So there's not much use to keep those values in your model if you cannot trust them to be correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With