简析association,aggregation,composition

转 贴: http://blog.csdn.net/booknut/archive/2009/03/11/3980936.aspx

UML中对象的几种关系:

association,aggregation和composition 都好像是 "has- a"关系,

其中细微区别相关资料提供以下一些解释:

association 关联

School与Student关联

 

http://ootips.org/uml-hasa.html 中如此解释:

 

Association represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.

 

关联从面向对象来说,指的是“A 对象向B对象发生消息的能力”,箭头的方向便是消息发送的方向;所以一般实行是A对象拥有B对象的引用作为成员变量,当然也有可能是方法的参数或局部变量。

 

[Example:]

|A|----------->|B|

class A
{
  private:
    B* itsB;
};


http://www.visualcase.com/kbase/associations.htm 中有关于箭头的解释

 

Sometimes a role has an arrow indicating its navigability.  This shows which class has the responsibility for maintaining the relationship between the classes.  In the example above, the school class has a responsibility for knowing which students attend.  Therefore, you don't have to explicitly define a getStudents operation in the school class (although you may) since it is already implicitly defined by the navigability of the association.

 

可以解释为,拥有箭头的角色(对象)(指向对方)有维护两个角色之间关系的责任,也就是说上面的图中School有维护这种关系的责任。

其实这两种解释方法是一致的,拥有另外一个对象的引用才能在维护两个角色关系中起主动作用,也是拥有对象的引用才能向该对象发生消息。

 

Aggregation 聚合

 System include Component

 

http://ootips.org/uml-hasa.html 中如此解释:

 

Aggregation  is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).

 

 表示整体与部分的关系,跟关联其实是一样的,唯一不同的地方就是不能循环的聚合关系,即 System可以包含Component的聚合,但是不能同时存在Component包含System的聚合。在实现上不能在System包含Component引用集合同时,Component又包含System引用集合。

[Example:]

|Node|<>-------->|Node|

class Node
{
  private:
    vector<Node*> itsNodes;
};

The fact that this is aggregation means that the instances of Node cannot form a cycle. Thus, this is a Tree of Nodes not a graph of Nodes.

 

Composition  组合

 

[...] is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.

[Example:]

|Car|<#>-------->|Carburetor|

class Car
{
  public:
    virtual ~Car() {delete itsCarb;}
  private:
    Carburetor* itsCarb
}; 

 

Composition 组成,跟Aggregation聚合几乎是完全一样的,除了生命周期。Composition中的“part(部分)”的存在和消亡是有整体控制的,“整体”负责“部分”的创建和销毁。

 

http://www.visualcase.com/kbase/associations.htm  中如此描述:

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.

一个“订单”中包含有多个产品,但是当订单被销毁时候“产品”依然可以存在。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值