UML中的association,aggregation以及composition

本文深入解析UML中关联(Association)、聚合(Aggregation)与组合(Composition)的概念,探讨它们之间的区别及应用场景。从类之间的基本关系出发,阐述了如何通过UML图示表达这些关系,为软件设计提供清晰的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎访问原文链接UML


# UML

UML 我们平时经常会用到,但有一些概念一直搞不清,在这里梳理一下。

基本概念

在这里使用微软文档中的图例进行说明

uml

ShapeElementDescription
1ClassA definition of objects that share given structural or behavioral characteristics. For more information, see Properties of types on UML class diagrams.
1ClassifierThe general name for a class, interface, or enumeration. Components, use cases, and actors are also classifiers.
2Collapse/ Expand controlIf you cannot see the details of a classifier, click the expander at upper-left of the classifier. You might also have to click the [+] on each segment.
3AttributeA typed value attached to each instance of a classifier. To add an attribute, click the Attributes section and then press ENTER. Type the signature of the attribute. For more information, see Properties of attributes on UML class diagrams.
4OperationA method or function that can be performed by instances of a classifier. To add an operation, click the Operations section and then press ENTER. Type the signature of the operation. For more information, see Properties of operations on UML class diagrams.
5AssociationA relationship between the members of two classifiers. For more information, see Properties of associations on UML class diagrams.
5aAggregationAn association representing a shared ownership relationship. The Aggregation property of the owner role is set to Shared.
5bCompositionAn Association representing a whole-part relationship. The Aggregation property of the owner role is set to Composite.
6Association NameThe name of an association. The name can be left empty.
7Role NameThe name of a role, that is, one end of an association. Can be used to refer to the associated object. In the previous illustration, for any Order O, O.ChosenMenu is its associated Menu. Each role has its own properties, listed under the properties of the association.
8MultiplicityIndicates how many of the objects at this end can be linked to each object at the other. In the example, each Order must be linked to exactly one Menu. * means that there is no upper limit to the number of links that can be made.
9GeneralizationThe specific classifier inherits part of its definition from the general classifier. The general classifier is at the arrow end of the connector. Attributes, associations, and operations are inherited by the specific classifier. Use the Inheritance tool to create a generalization between two classifiers.

其中重点记录一下Association,Aggregation和Composition

Association

An association represents a family of links. A binary association (with two ends) is normally represented as a line. An association can link any number of classes. An association with three links is called a ternary association. An association can be named, and the ends of an association can be adorned with role names, ownership indicators, multiplicity, visibility, and other properties.
There are four different types of association: bi-directional, uni-directional, aggregation (includes composition aggregation) and reflexive. Bi-directional and uni-directional associations are the most common ones.
For instance, a flight class is associated with a plane class bi-directionally. Association represents the static relationship shared among the objects of two classes.

Association简单理解就是代表两个类之间的关系。

Aggregation

Aggregation is a variant of the “has a” association relationship; aggregation is more specific than association. It is an association that represents a part-whole or part-of relationship. As shown in the image, a Professor ‘has a’ class to teach. As a type of association, an aggregation can be named and have the same adornments that an association can. However, an aggregation may not involve more than two classes; it must be a binary association. Furthermore, there is hardly a difference between aggregations and associations during implementation, and the diagram may skip aggregation relations altogether.[7]

Aggregation can occur when a class is a collection or container of other classes, but the contained classes do not have a strong lifecycle dependency on the container. The contents of the container still exist when the container is destroyed.

In UML, it is graphically represented as a hollow diamond shape on the containing class with a single line that connects it to the contained class. The aggregate is semantically an extended object that is treated as a unit in many operations, although physically it is made of several lesser objects.

Example: Library and Students. Here the student can exist without library, the relation between student and library is aggregation.

Aggregation.是 “has a”的关系,比association更加具体,它代表了part-whole或者part-of的关系,比如一个教授“has a”一个班级来上课。

当一个类是其他类的容器时,aggregation的关系就产生了,但是被包含的类与包含的类之间没有强的生命周期依赖,容器中的东西依然存在即使容器以及不在了。

Composition

composition关系中的两个类有强生命周期依赖,也即意味着一个类被删了,另一个类也随之被删了

Differences between Composition and Aggregation[edit]

  • Composition relationship

    1. When attempting to represent real-world whole-part relationships, e.g. an engine is a part of a car.

    2. When the container is destroyed, the contents are also destroyed, e.g. a university and its departments.

  • Aggregation relationship

    1. When representing a software or database relationship, e.g. car model engine ENG01 is part of a car model CM01, as the engine, ENG01, may be also part of a different car model.[8]

    2. When the container is destroyed, the contents are usually not destroyed, e.g. a professor has students; when the professor dies the students do not die along with them.

Reference

  1. Types of UML Diagrams
  2. UML Class Diagrams: Reference
  3. UML Association vs Aggregation vs Composition
  4. Class diagram
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值