简体   繁体   English

类图中的HashMap(UML)

[英]HashMap in class diagram (UML)

I build UML 2.0 class diagram for my Java application. 我为我的Java应用程序构建了UML 2.0类图。 In my code I have attribute with HashMap datatype. 在我的代码中,我有HashMap数据类型的属性。 But, as I know, there is no HashMap datatype in UML standard. 但是,据我所知,UML标准中没有HashMap数据类型。 The question is - can I use HashMap as datatype for attribute of the class? 问题是 - 我可以使用HashMap作为类的属性的数据类型吗?

UPDATE UPDATE

maybe in diagram I just should point to java.util package? 也许在图表中我应该指向java.util包? and maybe place Map class in this package on the diagram? 并且可能将Map类放在图上的这个包中?

HashMap should not appear in your UML model anyway. 无论如何,HashMap不应该出现在你的UML模型中。 HashMap is just an implementation of a qualified association. HashMap只是一个合格关联的实现。 Probably it's even just a speed improved unqualified association. 可能它甚至只是速度提高了不合格的关联。 So if you had a Class A with a HashMap you would model a UML Class A, a UML Class B and a UML Association from A to B. You can add a qualifier to the association if it's qualified by a key which is not an attribute of B. If your HashMap key is the name of B (and B has that name as an attribute) you would simply omit the qualifier. 因此,如果你有一个带有HashMap的A类,你可以建模一个UML A类,一个UML B类和一个从A到B的UML关联。如果它由一个非属性的键限定,就可以为该关联添加一个限定符。如果您的HashMap键是B的名称(并且B将该名称作为属性),则只需省略限定符。

To denote the implementation of your Association (you want to implement it with a HashSet) you can add that as a keyword or create a Stereotype for it (more complex). 要表示您的Association的实现(您希望使用HashSet实现它),您可以将其添加为关键字或为其创建一个Stereotype(更复杂)。

Just use a normal class in UML and call it HashMap . 只需在UML中使用普通类并将其称为HashMap UML is language-agnostic and has no knowledge of Java's predefined classes. UML与语言无关,不了解Java的预定义类。 Or did I misunderstand your question? 还是我误解了你的问题?

HashMap may be the Java name for the concept, but every programming language has some kind of Hash<> or Map<> class, and something equivalent should be included in UML because many models include Hash or Map container attributes. HashMap可能是该概念的Java名称,但每种编程语言都有某种Hash<>Map<>类,并且UML中应包含等效的东西,因为许多模型包含Hash或Map容器​​属性。

Some tools support a <<map>> stereotype; 有些工具支持<<map>>原型; if you have that I would use it if you are mainly concerned about visual intuitiveness - but it's not possible to say what kind of key is implied. 如果你有这个我会使用它,如果你主要关注视觉直觉 - 但不可能说暗示了什么样的键。

The qualified association graphical UML device is non-intuitive and I suspect hard for tools to transform to anything sensible in a forward code generation. 合格的关联图形UML设备是非直观的,我怀疑工具很难转换为正向代码生成中的任何合理的东西。 I would avoid it. 我会避免它。

The other way to do it (which is what I usually) do is the following: 另一种方法(我通常这样做)如下:

  • create a Hash class with V and K as generic parameters. 使用V和K作为通用参数创建一个Hash类。 To do it correctly, K should really be constrained by a class such as 'Ordered' also lacking in UML (we always add this) 要正确地做到这一点,K应该真的受到诸如'Ordered'这样的类的限制,也缺少UML(我们总是添加这个)
  • for every use of Hash eg Hash<Thing, String> (be careful with the order - I use value first, key second), create a UML class called Hash<Thing,String> and an outgoing relation to Hash<> and then map the V and K to actual parameters Thing and String 对于Hash的每次使用,例如Hash<Thing, String> (小心顺序 - 我首先使用值,键秒),创建一个名为Hash<Thing,String>的UML类Hash<Thing,String>以及与Hash<>的传出关系,然后映射V和K到实际参数ThingString
  • then in the class that wants to use it, define a property eg things whose type is the Hash<Thing,String> type. 然后在想要使用它的类,定义一个属性例如things ,其类型是Hash<Thing,String>类型。

MagicDraw for example supports this. 例如MagicDraw支持这一点。

The downside of this is that you won't see an association link between the client class and the value type ( Thing in my example). 这样做的缺点是您不会在客户端类和值类型之间看到关联链接(在我的示例中为Thing )。 The upside is that if you publish your models as programmer specifications, which is what we do, programmers see the right thing in the class tables, as you can see in this example - translation_details attribute . 好处是,如果您将模型作为程序员规范发布,这就是我们所做的,程序员在类表中看到正确的东西,正如您在本例中所见 - translation_details attribute

The difficulty in doing this basic task in UML is just one of the numerous problems with UML, and why most developers I meet today don't use it other than for pictures on whiteboards or documentation. 在UML中执行此基本任务的难度只是UML的众多问题之一,以及为什么我今天遇到的大多数开发人员除了用于白板或文档上的图片之外不使用它。

HashMap is only one of many java classes. HashMap只是众多java类中的一种。
And you can use any java class, interface or primitive type in your UML 2.0 class diagramm. 您可以在UML 2.0类中使用任何java类,接口或基本类型。
Any datatype in the UML 2.0 java class diagram corresponds to some java class, interface or primitive. UML 2.0 java类图中的任何数据类型都对应于某些java类,接口或基元。

You are using the UML diagram for development of your own application. 您正在使用UML图来开发自己的应用程序。 So, feel free to extend the UML 2.0 standard for your convenience. 因此,请随意扩展UML 2.0标准以方便您使用。 Noone can blame you for this. 没有人可以责怪你。

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

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