uml class 地图属性的图表替代

Uml class diagram alternative of map attribute

通过搜索我发现在UML中,聚合(假设它使用得当)可以用来表示class中的属性。

例如,

(假设列可以独立)

那么,使用这样的例子,如果我想用映射替换属性:Column[]来表示列的名称,像下面这样使用关联class是否正确? (以防万一,我不愿意将列名作为属性放在列 class 中)

关联 classes 与简单的关联一起使用。它们具有 m-1-1-n 的多重性。共享聚合(如您所用)没有定义的语义(我建议不要使用它,除非您有特定领域并记录在案)。将预期的多重性放在关联的任一侧会更好。

一个关联 class 连接两个 class 实体,添加属性 and/or 操作。你的例子是 "unconventional",因为 Table/Column 有一个简单的关系,不需要关联 class。一个一般的例子是 Student/Lecture 关系,您可以在其中放置一个关联 class 来记录考试结果、时间等

是的,我认为这是一种有效的建模方式,您可以使用某种密钥字符串来识别此 TableColumn

使用 Map 是众多可能实现中的一种,因此它不是真正的 等于

使用关联建模的优点 Class 是您的模型保持在更抽象的功能级别,并省去了实现细节。

顺便说一句。对于 TableColumn 之间的关联,我会使用 composition 而不是 aggregation,因为有一个明显的两者之间具有很强的所有权关系和 life-cycle 依赖性。

如果你想用map替换属性Column[]来表示列名,又不愿意把列名放在Columnclass中作为属性,假设你想精确地遵循 UML specification 那么您将生成如下所示的模型:

Map<Key, Value>通常被理解为一个关联的容器,其中包含具有唯一键的键值对Map.Entry<Key, Value>。容器通过从Map<Key, Value>Map.Entry<Key, Value>.

的定向聚合建模

Map<Key, Value>Map.Entry<Key, Value> 模板 。 UML 规范的第 7.3.3.1 条说:

A template cannot be used in the same manner as a non-template Element of the same kind. The template Element can only be used to generate bound Elements or as part of the specification of another template.

根据第 7.3.3.3 条:

A TemplateBinding is a relationship between a TemplateableElement and a template that specifies the substitutions of actual ParameterableElements for the formal TemplateParameters of the template.

因此我们有两个绑定元素,它们与其模板具有 TemplateBinding(由 <> 关键字标记)关系:

  • ColumnNames 本质上是 Map<String, Column>
  • 的名称
  • ColumnName 本质上是 Map.Entry<String, Column
  • 的名称

根据 UML 规范的第 11.5.1 条:

An Association classifies a set of tuples representing links between typed instances. An AssociationClass is both an Association and a Class.

ColumnName 是表示 StringColumn class 实例之间的 link 的 AssociationClass。我们使用第 11.5.5 条中的符号,图 11.35 来表达这一点。

最后,TableColumnNames classes 之间的方向组合关联告诉我们,Table 的每个实例都拥有 ColumnNames 的一个实例,即一组列名。

请注意,虽然 ColumnNamesColumName classes 通常通过实现对最终用户隐藏,但它们仍然存在。

我用BoUML画了图