Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Design Pattern & UML  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Aggregation 与 Composition 的区别?
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-03 10:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如题,大家指点


"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-03 11:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
查了查感觉Aggregation 可以转换成 Association ,估计很难辨别, 类图中好象都不用 Aggregation 关系???

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


图F
聚合(Aggregation)

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

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


jameszhang edited on 2004-11-03 11:28

"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-11-03 12:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-03 13:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
lifetime 理解为 "生命周期"??

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


jameszhang edited on 2004-11-03 13:35

"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
wes109

以梦为马

CJSDN高级会员


发贴: 857
积分: 60
于 2004-11-03 15:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Aggregation: 聚合
Composition: 组合

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

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

引用floater的例子

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

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

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




作者 Re:Aggregation 与 Composition 的区别? [Re:wes109]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-03 16:09 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
wes109 wrote:
Aggregation: 聚合
Composition: 组合

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

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

引用floater的例子

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

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

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

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



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
wes109

以梦为马

CJSDN高级会员


发贴: 857
积分: 60
于 2004-11-03 16:24 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我觉得这不是代码上的区别

只是概念上的区别




作者 Re:Aggregation 与 Composition 的区别? [Re:wes109]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-04 08:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
wes109 wrote:
我觉得这不是代码上的区别

只是概念上的区别

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



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-11-04 11:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:Aggregation 与 Composition 的区别? [Re:floater]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-04 11:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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??



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-11-04 11:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
yep.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
supertom





发贴: 7
积分: 1
于 2004-11-18 09:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list



作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
WeiterWay





发贴: 25
积分: 0
于 2004-11-24 10:42 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Composition 是一种特殊形式的Aggregation (如上面各位讨论的,除了lifetime区别). 在UML语法里Composition用实心箭头表示,Aggregation用空心箭头表示。我个人理解,Composition关系中的component可以看作是heavy-weight property. 它是一个实体组成部分但是又不能独立于容器主体。


作者 Re:Aggregation 与 Composition 的区别? [Re:jameszhang]
mluo





发贴: 1
积分: 0
于 2004-11-30 04:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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。




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

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