Topic: Aggregation 与 Composition 的区别?

  Print this page

1.Aggregation 与 Composition 的区别? Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-03 10:08

如题,大家指点

2.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-03 11:22

查了查感觉Aggregation 可以转换成 Association ,估计很难辨别, 类图中好象都不用 Aggregation 关系???

从代码上没什么区别!!!!????


图F
聚合(Aggregation)

聚合(图F)是关联的一种形式,代表两个类之间的整体/局部关系。聚合暗示着整体在概念上处于比局部更高的一个级别,而关联暗示两个类在概念上位于相同的级别。聚合也转换成Java中的一个实例作用域变量。

关联和聚合的区别纯粹是概念上的,而且严格反映在语义上。聚合还暗示着实例图中不存在回路。换言之,只能是一种单向关系。

3.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-11-03 12:48

google'd this, the diff is the lifetime.

http://www.visualcase.com/kbase/associations.htm

The association link indicates that two classes have a relationship: a student attends a school; a student takes courses.

Each association link has two ends which are called roles. Each role has a name, a multiplicity, a navigability and a type.

Composition indicates that one class belongs to the other. A polygon is made up of several points. If the polygon is destroyed, so are the points.

Aggregation is similar to composition, but is a less rigorous way of grouping things. An order is made up of several products, but a product continues to exist even if the order is destroyed.

4.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-03 13:31

lifetime 理解为 "生命周期"??

floater 老大能举几个代码的例子吗?

5.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: wes109
Posted on: 2004-11-03 15:21

Aggregation: 聚合
Composition: 组合

它们都代表一种关联(association)关系
聚合较松散,而组合较紧密

我觉得可以理解为生命周期

引用floater的例子

1、组合关系。
几个点(point)连在一起构成了一个多边形(polygon)。如果多边形不存在了,构成多边形的点也就不存在了,它们是“同生死,共患难”。。。。

就像数据库中的两个表,user(可能保存用户名、密码、用户类型等)和userinfo(可能保存地址、身份证号码等),如果要删除表user,必然同时要删除userinfo

2、聚合关系
一个订单,上面包含几个产品信息。如果订单被取消或删除了,产品还是要存在的。它们是聚合关系。

6.Re:Aggregation 与 Composition 的区别? [Re: wes109] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-03 16:09

wes109 wrote:
Aggregation: 聚合
Composition: 组合

它们都代表一种关联(association)关系
聚合较松散,而组合较紧密

我觉得可以理解为生命周期

引用floater的例子

1、组合关系。
几个点(point)连在一起构成了一个多边形(polygon)。如果多边形不存在了,构成多边形的点也就不存在了,它们是“同生死,共患难”。。。。

就像数据库中的两个表,user(可能保存用户名、密码、用户类型等)和userinfo(可能保存地址、身份证号码等),如果要删除韚ser,必然同时要删除userinfo

2、聚合关系
一个订单,上面包含几个产品信息。如果订单被取消或删除了,产品还是要存在的。它们是聚合关系。

概念很清楚了,但有JAVA代码的例子吗? 感觉这种关系用在数据库设计上比较合适

7.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: wes109
Posted on: 2004-11-03 16:24

我觉得这不是代码上的区别

只是概念上的区别

8.Re:Aggregation 与 Composition 的区别? [Re: wes109] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-04 08:08

wes109 wrote:
我觉得这不是代码上的区别

只是概念上的区别

那就是可以理解为代码上没有区别. 因为设计完成 要指导编码所以应该清楚之间的代码是否有区别

9.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-11-04 11:10

normally, there should not have any getter/setter for the composition, so the member will have the same lifetime as the object containing it. Otherwise, if other objects hold the reference of the member, then the member just can't have the same lifetime as the object.

This is where we are talking about objects, not just classes.

10.Re:Aggregation 与 Composition 的区别? [Re: floater] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-04 11:32

floater wrote:
normally, there should not have any getter/setter for the composition, so the member will have the same lifetime as the object containing it. Otherwise, if other objects hold the reference of the member, then the member just can't have the same lifetime as the object.

This is where we are talking about objects, not just classes.

有点理解了, 这里的objects 是 类的 instances??

11.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-11-04 11:56

yep.

12.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: supertom
Posted on: 2004-11-18 09:18


13.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-11-24 10:42

Composition 是一种特殊形式的Aggregation (如上面各位讨论的,除了lifetime区别). 在UML语法里Composition用实心箭头表示,Aggregation用空心箭头表示。我个人理解,Composition关系中的component可以看作是heavy-weight property. 它是一个实体组成部分但是又不能独立于容器主体。

14.Re:Aggregation 与 Composition 的区别? [Re: jameszhang] Copy to clipboard
Posted by: mluo
Posted on: 2004-11-30 04:10

Composition实际上就是Aggregation的一种。

而Aggregation就是一种part-of的关系,A是B的一部分,就象points是ploygon的一部分,当ploygon变化的时候,那些点也就相应的变化; 发动机是汽车的一部分,汽车要更快,发动机就要更换(可能比喻不太好)。所以前面有个回复的解释composition用多边形与点的例子是错的,但后面"同生死"的概念是正确的,也是最重要的一点。

Compostion则属于Aggregation的一类,但表现得更strong。同样拿Ploygon来做例子,你有一个composited object,用来对这个ploygon进行着色等具体行为,当这个ploygon都没了,相应的这个composited part也更着消失掉。在UML里面有两种画图方式,一种是按照楼上几位画,还有一种是将这个composited object画在polygon class里面,这样理解起来更容易些,因为更能体现"同生死"的特性。

形象的解释你可以参考UML distilled,而完整定义则查看UML user guide。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923