问题描述
我了解聚合和合成之间的区别,但是我在关联方面有些挣扎.我目前的理解是,彼此使用"时,类之间存在关联,例如,在方法调用期间将一个对象传递给另一个对象.另请参阅:
I understand the difference between aggregation and composition but I am struggling a bit with association. My current understanding is that an association exists between classes when ‘they use each other’, for example, one object is passed to the other during a method call. See also:
http://www.codeproject.com/Articles/330447/Understanding-Association -聚合与合成
两个对象都独立存在,与聚合相反,没有一个对象是另一个的容器类.这是否意味着两个对象都必须具有其他对象的副本(例如1:m关系),或者关联如何存储".任何反馈将不胜感激.
Both objects exist independently and, in contrast to aggregation, no object is a container class of the other. Does this mean that both objects MUST have a copy of the other(s) (e.g. 1:m relationship) or how else is the association ‘stored’. Any feedback would be very much appreciated.
推荐答案
来自UML上层结构2.4.1:
From the UML Superstructure 2.4.1:
仅此而已.而且非常模糊因此,也很难理解.我定义的(在我所教的课程中)是一个从依赖到构成的链接层次结构,其中:
Nothing more, nothing less. and very vague. Because of this, it is also very hard to understand. What I defined (In a course I teach) is a hierarchy of links from dependency to composition where:
- 从
A
到B
的依赖关系意味着A
使用B
但间接使用(例如,通过接收它的实例并将其转发到其他对象). - 从
A
到B
的关联意味着A
直接使用B
(例如,通过调用方法) - 从
A
到B
的聚合意味着(cemally)B
是A
的一部分,但是B
可以共享,并且如果删除A
,则不会删除B
.请注意,这没有说明组成部分"的实现方式. - 从
A
到B
的组合类似于聚合,其中B
无法共享,并且如果删除A
,其所有聚合(B
s)也将被删除.
- Dependency from
A
toB
means thatA
usesB
but indirectly (say by receiving instances of it and forwarding them to other objects). - Association from
A
toB
means thatA
usesB
directly, (for example by calling methods) - Aggregation from
A
toB
means thatB
is part ofA
(semantically) butB
can be shared and ifA
is deleted,B
is not deleted. Note that this says nothing about how the "is part" is implemented. - Composition from
A
toB
is like Aggregation, whereB
cannot be shared and ifA
is deleted, all of its aggregates (B
s) are deleted also.
这篇关于关联与聚合之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!