[翻译]UML Association(关联) vs Aggregation(聚合) vs Composition(组合)

最近在看 《架构整洁之道》 ,书中举一些例子时会有 UML 类图,个人对于 UML 类图记得不是很清晰,所以理解起来不是很方便,准备再总结下。总结时发现对于 组合/聚合/关联 之间的差异有点不太理解,查了一些相关说明的文章,发现这篇写的比较好,但是没有中文,所以自己翻译了一下,哪块翻译的有问题欢迎在评论区批评指正。

ps 也有结合 mermaid 的使用写了一篇UML类图的总结,有兴趣的可以看下 UML Mermaid .

为加强记忆,对于关键词,中文和英文会同时列出

原文地址:visual-paradigm.com

Consider the differences and similarities between the classes of the following objects: pets, dogs, tails, owners.

考虑以下对象的类的相同,不同:宠物尾巴主人

img

We see the following relationships:

  • owners feed pets, pets please owners (association)
  • a tail is a part of both dogs and cats (aggregation / composition)
  • a cat is a kind of pet (inheritance / generalization)

我们可以看到以下关系

  • 主人喂养宠物,宠物逗主人开心(Association(关联))
  • 尾巴是猫和狗的一部分(Aggregation(聚合)/Composition(组合))
  • 猫是宠物的一种(Inheritance(继承)/Generalization(泛化))

The figure below shows the three types of association connectors: association, aggregation, and composition. We will go over them in this UML guide.

下图显示了三种关联关系:Association(关联),Aggregation(聚合),Composition(组合)。我们可以在这篇指南中复习UML

img
img
img

The figure below shows a generalization. We will talk about it later on in this UML guide.

下图展示了Generalization(泛化)关系。我们将在之后的UML指南中讨论它。
ps. 这两段感觉没什么意义,看下边的就好

img

Association 关联

If two classes in a model need to communicate with each other, there must be a link between them, and that can be represented by an association (connector).

如果一个模型中的两个类需要相互通信,则他们之间必然存在连接,并且表现为关联(association)。

Association can be represented by a line between these classes with an arrow indicating the navigation direction. In case an arrow is on both sides, the association is known as a bidirectional association.

关联(Association)可以由这些类之间线表示,箭头表示导航方向。如果两端都有箭头,这个关联就是双向关联

We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association. The example indicates that a Student has one or more Instructors:

我们可以通过给这条线添加多重性修饰表示关联的多重性。下边的例子表时一个学生有一个或多个导师

A single student can associate with multiple teachers:

一个学生可以关联多个老师

img

The example indicates that every Instructor has one or more Students:

每个导师可以有一个或多个学生

img

We can also indicate the behavior of an object in an association (i.e., the role of an object) using role names.

我们也可以使用角色名称表示关联中对象的行为(比如 对象角色)

img

Association(关联) vs Aggregation(聚合) vs Composition (组合)

The question “What is the difference between association, aggregation, and composition” has been frequently asked lately.

“关联(Association),聚合(Aggregation)和组合(Composition)有什么区别?”最近经常被问到

Aggregation and Composition are subsets of association meaning they are specific cases of association. In both aggregation and composition object of one class “owns” object of another class. But there is a subtle difference:

  • Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.
  • Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don’t exist separate to a House.

聚合(Aggregation)组合(Composition) 是关联的子集,他们是特殊的关联例子。在聚合(Aggregation)和组合(Composition)中,一个类的对象“拥有”另一个类的对象。但有一个微妙的区别:

  • 聚合(Aggregation) 意味着子类可以独立于父类存在,比如:班级(父类)和学生(子类)。删除班级后学生仍然存在。
  • 组合(Composition) 意味着子类不能独立于父类存在,比如:房子(父类)和房间(子类)。没有房子,房间也不存在。

Composition Example 组合示例

We should be more specific and use the composition link in cases where in addition to the part-of relationship between Class A and Class B - there’s a strong lifecycle dependency between the two, meaning that when Class A is deleted then Class B is also deleted as a result

我们应该更具体一些,并在以下情况下使用组合(composition)链接:类A和类B除了部分关系之外,有很强的生命周期依赖性,这意味着当类A被删除时,类B也会被删除

img

Aggregation Example 聚合示例

It’s important to note that the aggregation link doesn’t state in any way that Class A owns Class B nor that there’s a parent-child relationship (when parent deleted all its child’s are being deleted as a result) between the two. Actually, quite the opposite! The aggregation link is usually used to stress the point that Class A instance is not the exclusive container of Class B instance, as in fact the same Class B instance has another container/s.

值得注意的是,聚合链接没有以任何方式声明类A拥有类B,也没有声明两者之间存在父子关系(父子关系:当父类删除时,其所有子类都将因此被删除)。事实上,恰恰相反!聚合链接通常用于强调类A实例不是类B实例的独占容器,实际上同一个类B实例拥有另一个容器/s。

img

Summing it up -

To sum it up association is a very generic term used to represent when one class used the functionalities provided by another class. We say it’s a composition if one parent class object owns another child class object and that child class object cannot meaningfully exist without the parent class object. If it can then it is called Aggregation.

总结一下

总结起来,关联(Association)是一个非常通用的术语,用来表示一个类使用了另一个类提供的功能。如果一个父类对象拥有另一个子类对象,并且子类对象在没有父类对象的前提下不能有意义的存在,我们称其为组合(Composition),如果可以(独立存在)则被称为聚合(Aggregation)。

Generalization(泛化) vs Specialization(特化)

Generalization is a mechanism for combining similar classes of objects into a single, more general class. Generalization identifies commonalities among a set of entities. The commonality may be of attributes, behavior, or both. In other words, a superclass has the most general attributes, operations, and relationships that may be shared with subclasses. A subclass may have more specialized attributes and operations.

泛化(Generalization) 是一种用于将类似对象类转化为一个更一般的类的机制。泛化(Generalization)表示一组实体之间的共性。共性可以是属性,行为,或者都有。换句话说,超类有最通用的属性、操作和以及可以与子类共享的关系。子类可以有更专门的属性和操作。

Specialization is the reverse process of Generalization means creating new sub-classes from an existing class.

For Example, a Bank Account is of two types - Savings Account and Credit Card Account. Savings Account and Credit Card Account inherit the common/ generalized properties like Account Number, Account Balance, etc. from a Bank Account and also have their specialized properties like unsettled payment etc.

专门化(Specialization) 是泛化(Generalization)的反向过程,即从现有的类创建新的子类。

例如,银行账户有两种类型——储蓄账户和信用卡账户。储蓄账户和信用卡账户从银行账户中继承了常见的/广义的属性,比如账号、账户余额等,也有它们特有的属性,比如未结算的付款等。

img

Generalization(泛化) vs Inheritance(继承)

Generalization is the term that we use to denote abstraction of common properties into a base class in UML. The UML diagram’s Generalization association is also known as Inheritance. When we implement Generalization in a programming language, it is often called Inheritance instead. Generalization and inheritance are the same. The terminology just differs depending on the context where it is being used.

泛化(Generalization) 是我们用来在UML中表示将公共属性抽象为基类的术语。UML类图的泛化关联也称为继承(Inheritance)。当我们在编程语言中实现泛化时,它通常被称为继承。泛化和继承是相同的。这些术语只是取决于使用的上下文而有所不同。

[翻译]UML Association(关联) vs Aggregation(聚合) vs Composition(组合)

https://blog.jichao.top/2020/10/31/Translate-Association-Aggregation-Composition/

作者

Alec.ji

发布于

2020-10-31

更新于

2021-11-01

许可协议

评论