UML中Association(关联), Aggregation(聚合), Composition(组合)和Dependency(依赖)辨析

本文详细解析了UML中的四种重要关系:关联、依赖、聚合和组合。关联是类之间的结构关系,分为双向和单向等类型;依赖则表示类之间的临时性使用关系;聚合强调部分与整体的关系,但部分可以独立存在;组合是更强的聚合形式,部分的生命周期依赖于整体。了解这些关系对于理解UML模型和设计高质量软件至关重要。

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

Association, Aggregation, Composition and Dependency 

 

Association vs. Dependency

Association

Association的定义:

描述两个类之间的结构关系:一个类的实例连接到另一个类的实例

An association is a structural relationship between classes that specifies that objects

of one class are connected to objects of another class

#严格的说,An association represents a structural relationship that connects two classifiers

 通常也可以用class泛指其他3类。

Classifiers: classinterfacedata typecomponent

 

Association有5种类型:

1. Bi-directional:

2. Uni-directional associations:

3. Association class:

4. aggregationBasic aggregation:

5. Composition (Composition aggregation)

Reflexive associations

Association举例:

 

Bi-directional:

Association class:

 

Dependency

Dependency的定义:(From UML Spec)

A dependency signifies a supplier/client relationship between model elements where the modification of the supplier may impact the client model elements. A dependency implies the semantics of the client is not complete without the supplier. The presence of dependency relationships in a model does not have any runtime semantics implications; it is all given in terms of the model-elements that participate in the relationship, not in terms of their instances (runtime).

 

Dependency is a model-level relationship and not a run-time relationship—describing the need to investigate the model definition of the client element for possible changes if the model definition of the supplier element is changed.

 

 Dependency的类型:

Type of dependency

Keyword or Stereotype

Description

Abstraction

«abstraction», «derive», «refine», or «trace»

Relates two model elements, or sets of model elements, that represent the same concept at different levels of abstraction, or from different viewpoints.

Binding

«bind»

Connects template arguments to template parameters to create model elements from templates.

Realization

«realize»

Indicates that the client model element is an implementation of the supplier model element, and the supplier model element is the specification.

Substitution

«substitute»

Indicates that the client model element takes the place of the supplier. The client model element must conform to the contract or interface that the supplier model element establishes.

Usage

«use», «call», «create», «instantiate», or «send»

Indicates that one model element requires another model element for its full implementation or operation.

 

Dependency的图例:

Notes: instantiate stereotype

A stereotyped usage dependency among classifiers indicating that operations on the client create instances of the supplier

 

Dependency的适用场合(Guideline: when to use Dependency)

Model a Dependency When The Relationship is Transitory

Connect two classes to indicate that the connection between them is at a higher level of abstraction than an association relationship. The dependency relationship indicates that the client class performs one of the following functions:

¡         Temporarily uses a supplier class that has global scope

¡         Temporarily uses a supplier class as a parameter for one of its operations

¡         Temporarily uses a supplier class as a local variable for one of its operations

¡         Sends a message to a supplier class

 

 

Composition vs. Aggregation

Composition:部分的生命周期依赖于整体的生命周期,也就是说部分不能脱离整体而存在。

整体的个数不会超过1,也就是说“部分”是不共享的。这是Composition的关键特性。而Aggregation的“部分”是可以共享的。在“拥有”关系上,CompositionAggregation更强,更“自私”。

在早期UML Draft中也叫aggregation-by-value,在语义上等价于类的属性。

代码举例:

public class Engine
{
 . . .
}

publicclassCar

{

   Engine e = new Engine();

   .......

}

 

Aggregation:用来表示拥有或者整体/部分的关系,但是部分的存在可以不依赖整体。

可以视为“aggregation-by-reference

代码举例:

public class Address
{
 . . .
}

publicclassPerson

{

    private Address address;

    public Person(Address address)

    {

        this.address = address;

    }

    . . .

}

 

Address address =new Address();
Person personA = new Person(address);

Person personB =new Person(address); // AB两人合住在相同的地址

 

Composition can be used to model by-value aggregation, which is semantically equivalent to an attribute. In fact, composition was originally called aggregation-by-value in an earlier UML draft, with “normal” aggregation being thought of as aggregation-by-reference.

 

 

参考资源:

http://faq.javaranch.com/java/AssociationVsAggregationVsComposition

http://www.jguru.com/faq/view.jsp?EID=51520

http://www.atomicobject.com/pages/Aggregationaggregation by value 

http://www.c-sharpcorner.com/UploadFile/pcurnow/compagg07272007062838AM/compagg.aspx

 http://publib.boulder.ibm.com/infocenter/rtnlhelp/v6r0m0/index.jsp?topic=/com.ibm.xtools.modeler.doc/topics/cdepend.html

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值