📜  UML关联vs聚合vs组合

📅  最后修改于: 2021-01-11 13:12:11             🧑  作者: Mango

UML关联,聚合与组合

在UML图中,关系用于链接几件事。它是结构,行为或分组事物之间的联系。以下是下面列出的标准UML关系:

  • 协会
  • 相依性
  • 概括
  • 实现

协会

关联关系是一种结构关系,其中在系统内链接了不同的对象。它表现出代表活动的对象之间的二进制关系。它描述了对象之间的关系,例如一个老师,可以与多个老师关联。

它由类之间的一条线表示,后跟一个导航方向的箭头,当箭头在两侧时,则称为双向关联。我们可以通过在表示关联的行上添加装饰来指定关联的多重性。

例:

1)一个老师有多个学生。

2)一个学生可以与许多老师联系。

组成和聚集是关联的两个子集。在这两种情况下,一个类的对象都归另一类的对象所拥有。唯一的区别是,子项在组成上并不独立于其父项而存在,而在聚合中,子项不依赖于其父项,即独立。聚合是关联的一种特殊形式,组合是聚合的一种特殊形式。

聚合

聚合是关联的子集,是不同事物的集合。它代表有关系。它比关联更具体。它描述了整体或部分关系。这是一个二进制关联,即,它仅涉及两个类。这是一种孩子独立于父母的关系。

例如:

在这里,我们考虑一个汽车和一个车轮的例子。没有车轮,汽车就无法行驶。但是车轮可以与自行车,踏板车,自行车或任何其他车辆独立使用。车轮对象可以不存在汽车对象而存在,这证明是一种聚集关系。

组成

组成是聚合的一部分,它描绘了整个部分的关系。它描述了组合(父)及其部分(子项)之间的依赖关系,这意味着如果丢弃该组合,则其部分也会被删除。它存在于相似对象之间。

从下面的示例中可以看到,构图关联关系将Person类与Brain类,Heart类和Legs类联系起来。如果该人被破坏,大脑,心脏和腿也将被丢弃。

关联vs.聚合vs.组成

Association Aggregation Composition
Association relationship is represented using an arrow. Aggregation relationship is represented by a straight line with an empty diamond at one end. The composition relationship is represented by a straight line with a black diamond at one end.
In UML, it can exist between two or more classes. It is a part of the association relationship. It is a part of the aggregation relationship.
It incorporates one-to-one, one-to-many, many-to-one, and many-to-many association between the classes. It exhibits a kind of weak relationship. It exhibits a strong type of relationship.
It can associate one more objects together. In an aggregation relationship, the associated objects exist independently within the scope of the system. In a composition relationship, the associated objects cannot exist independently within the scope of the system.
In this, objects are linked together. In this, the linked objects are independent of each other. Here the linked objects are dependent on each other.
It may or may not affect the other associated element if one element is deleted. Deleting one element in the aggregation relationship does not affect other associated elements. It affects the other element if one of its associated element is deleted.
Example: A tutor can associate with multiple students, or one student can associate with multiple teachers. Example: A car needs a wheel for its proper functioning, but it may not require the same wheel. It may function with another wheel as well. Example: If a file is placed in a folder and that is folder is deleted. The file residing inside that folder will also get deleted at the time of folder deletion.