简体   繁体   中英

Modelling associations to Java Enums in UML Class diagrams

I am confused about the usage of associations with Java Enums in UML Class diagrams. Currently I am modelling a system containing a Car class that has some properties. Some are also Enums, just like FuelKind or CarBrand :

在此处输入图片说明

There are going to be a lot of cars in the database ( Car is a JPA entity) and I'm asking myself which is the best association and cardinality to FuelKind and CarBrand .

My thoughts until now:

  • Enum instances are instantiated on their own, so technically they do not depend on Car . Also non-technically those enums contain all available 'choices', so there is no strict dependency. So it can not be a Composition Aggregation between them.
  • One Car has one CarBrand and one FuelKind . But from the other side: Does one brand or fuel kind have one car or multiple cars? Logically multiple cars can be from Mercedes or driving on petrol. But technically a enum instance has no link to one or multiple cars.

Do you have some suggestions and thoughts on how to solve this problem?

You're doing it almost right. A directed association is the correct relationship for this situation.

To clarify that the association refers to a specific attribute in the class, use a target role. This is displayed as a text label with the name of the attribute (fuelKind) at the enumeration end ( not the class end) of the connector.

If you had two attributes of the same enum type you would then have to draw two separate associations.

However, since you are using the type names in the attributes you don't actually need to draw any relationships at all. But if you do, a directed association with a target role is semantically equivalent to naming the type in the attribute. Which you choose is a matter of style, and you can do both if it helps clarify things.

As to a relationship going the other way, the directed nature of the association means that the relationship is one-way. In other words, drawing it the way you've done does not indicate a relationship from the enum types back to the class. And for enum types there normally shouldn't be, any more than there are such relationships for integers or strings.

This is not correct. An enumeration is a datatype, not a class. So you just assign it to your attribute in the Car class. You can indicate with a dependency that you are using a specific enum in a class.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM