如何在UML类图中描述包含的映射?
我有一个 MyServer 类,其中包含一个 Map,其键是 MyClientType 对象,其值是 MyClient 对象。 我想在类图中描述这种关系,但我找不到一种干净的方法来做到这一点。
I have a MyServer class that contains a Map whose keys are MyClientType objects and whose values are MyClient objects. I'd like to depict this relationship in a class diagram but I can't figure out a clean way to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
克鲁的答案是最好的,但它仍然只是暗示了一张地图。
我认为这取决于你的图表所处的抽象级别。 如果它相对较高,我会同意黑猩猩的反应。 如果它相对较低并且您确实需要显示地图,有意显示与实现相关的细节,我会采用以下内容:
然后如何在代码中实现地图是完全不相关的(
T1toT2
run-时间对象实际上可能不会出现)。正如 Gabreil 所提到的,这也可以使用关联类进行建模。
当然,只有当您确实确实需要显示或指定地图时才重要。
Kru's answer is the best, but it still only hints at a Map.
I would argue it depends on the level of abstraction at which your diagram sits. If it's relatively high, I'd go with chimp's response. If it's relatively low and you really need to show a map, intentionally showing implementation-related detail, I'd go with the following:
How the map is then implemented in code is totally irrelevant (
T1toT2
run-time objects might not actually come to be).As mentioned by Gabreil, this could be also be modelled using an association class
Of course is only matters if you really really need to show or specify a map.
应该不是像上面那样很简单吗?
Should it not be quite simple like above?
首先,我和其他一些人认为,UML 应该包含一些基本的集合类型,就像在一些早期版本中所做的那样。 例如,它们可以取自 OCL...
“EMF 方式”似乎是正确的,但恕我直言,它过于重视 to 类型,恕我直言,这确实不重要,所以我会对它进行建模就像一个关联类一样。 这将使您能够捕获所有地图特定约束(例如多重性),这些约束可以使用常规类捕获,但不会使该类与其他类一样重要。
First of all I and some others think, UML should contain some basic collection types as it did in some earlier versions. They could be taken for example from the OCL...
The "EMF way" seems right, however it gives imho too much importance to the to type, which is really unimportant imho, so I would model it just as an association class. This will enable you to capture all map specific constraints (as e.g. multiplicity) which can be captured using regular class, but won't make that class as important as the other ones.
与 Mark 的解决方案的不同之处在于,服务器与容器之间存在多种关系。 这也是 Eclipse 建模框架 (EMF) 建议实现地图的方式。
您还可以添加一些更特定于 UML 的内容,例如指定键必须是唯一的(通过构造型)。
The difference to Mark's solution is that the server has a many-relation to the containers. That's also how the Eclipse Modeling Framework (EMF) proposes to implement maps.
You might also throw in some more UML-specific things, like specifying that the keys have to be unique (through stereotypes).
我只会在 MyClient 端显示从 MyServer 到 MyClient 的关联,其重数为 0..*。 其他一切都是实现细节,可以留给程序员。
I would just show an association from MyServer to MyClient with a multiplicity of 0..* at the MyClient end. Everything else is implementation detail and can be left to the programmer.
您可以使用合格的关联:
请参阅: http://etutorials.org/Programming/UML/Chapter+6.+Class+Diagrams+Advanced+Concepts/Qualified+Associations/ (因为很难使用 ASCII 进行绘制)
另请注意,限定关联会更改多重性:
顶部说明了从服务器到 0-n 值的关联。 相比之下,限定关联表示任何给定的键将仅与一个值关联,并且您不能拥有一个缺少值的键。
You can use a qualified association:
See: http://etutorials.org/Programming/UML/Chapter+6.+Class+Diagrams+Advanced+Concepts/Qualified+Associations/ (cause it is hard to draw using ASCII)
Note also that a qualified association changes the multiplicity:
The top illustrates an association from a server to 0-n values. By contrast, the qualified association says that any given key will be associated with only one value, and that you can't have a key an absent value.