什么是 UML 中的一元关联

What is a Unary association in UML

当我用谷歌搜索了解一元关联时,我得到了以下两个解释:

第一个是:

A unary relationship is when both participants in the relationship are the same entity. For Example: Subjects may be prerequisites for other subjects, or one employee manages many Employees.

第二个是:

Class B knows about ClassA.

Class A does not know about ClassB.

Now lets look at the following example:

You can see the Person and Address relationship below. We call this relationship as has-a relationship since person has a address. So Person knows the address but address does not know anything about person

我是不是误会了什么?

共同语言

一个关联的arity就是关联了多少个class。这是一个模棱两可的概念,因为有些人理解不同的 classes,而其他人理解实例。

当应用于 unary 时,第一种解释意味着自反关联(或 self-association,即与 iteself 关联的 class,而第二种解释将意味着 class 没有关联(不是很有用:任何 class 都可以与任何其他关联)。

UML 透视图

幸运的是,UML 规范比通用语言要精确得多:

An Association specifies a semantic relationship that can occur between typed instances. It has at least two memberEnds represented by Properties, each of which has the type of the end. More than one end of the Association may have the same type.

所以在 UML 中没有“一元关联”。它是二元、三元或 n-ary(规范中使用的术语)。对于两端具有相同class的二进制关联,UML中没有专门的术语。但是 或 self-association 是比一元更受欢迎的术语。

E/R建模

术语“一元”在 entity-relationship modeling, to describe a relation in a relational database. Relations correspond more or less to an association in UML, and entities to classes, but there are some subtle semantic differences. E/R has its foundation in the set theory 的上下文中很流行。如果一个关系是在相同的实体之间,这意味着实际上只涉及一个集合。这可能是一元在这种情况下更受欢迎的原因。

Merriam-webster 将一元定义为

having, consisting of, or acting on a single element, item, or component

所以第一个解释是正确的,因为这种类型的关联作用于单个 class。然而,术语 一元 未在 UML 中使用,可能会造成混淆。
UML 使用术语 binary 表示关联有两端,并且 ternaryn-ary 表示关联有多个端点。 你说的一元关联其实是对自身的二元关联,也叫自反关联。

不要与单向关联混淆,后者是一种只能导航(有箭头)到一侧的关联。