简体   繁体   English

在UML类图中对与Java枚举的关联进行建模

[英]Modelling associations to Java Enums in UML Class diagrams

I am confused about the usage of associations with Java Enums in UML Class diagrams. 我对UML类图中与Java枚举的关联使用感到困惑。 Currently I am modelling a system containing a Car class that has some properties. 目前,我正在建模一个包含具有某些属性的Car类的系统。 Some are also Enums, just like FuelKind or CarBrand : 有些也是Enums,就像FuelKindCarBrand一样

在此处输入图片说明

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 . 数据库中将有很多汽车( Car是JPA实体),我想问自己,哪一个是FuelKindCarBrand的最佳关联和基数。

My thoughts until now: 直到现在我的想法:

  • Enum instances are instantiated on their own, so technically they do not depend on Car . 枚举实例是自己实例化的,因此从技术上讲,它们不依赖于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 . 一辆汽车有一个CarBrand和一个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. 这在连接器的枚举端( 而不是类端)上显示为带有属性名称(fuelKind)的文本标签。

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. 因此,您只需将其分配给Car类中的属性即可。 You can indicate with a dependency that you are using a specific enum in a class. 您可以通过依赖性指示您正在类中使用特定的枚举。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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