《Java疯狂讲义》中关联、组合和聚合的谬误

本文参考如下资料(建议阅读):

a). difference-aggregation-acquaintance-and-composition-as-used-by-gang-of-four

b). AssociationVsAggregationVsComposition

c). Design Patterns Elements of Reusable Object-Oriented Software

d). Java疯狂讲义(第三版)


1. 问题的由来

我看到的是这本书的第三版,在2.2.2中有关UML的类图的概念理解上,书中所用的图如下:


这副图上面的文字(书里面的排版)我觉得没有太大的问题(实际上我觉得还有一些表达欠妥的地方),摘录如下:

关联关系包含两种特例:聚合和组合,它们都有部分和整体的关系,但通常认为组合比聚合更加严格。当某个实体聚合成另一个实体时,该实体还可以同时是另一个实体的部分,例如,学生即可以是篮球俱乐部的成员,也可以是书法俱乐部的成员;当某个实体组合成另一个实体时,该实体则不能同时是一个实体的部分。聚合使用带空心菱形框的实线表示,组合则使用带实心菱形框的实线表示。


图片下片的文字就狗屁不通了,摘录如下:

Student和BasketBallClub存在聚合关系,即1个或多个Student实体可以聚合成一个BasketBallClub实体。而Arm(手臂)和Student之间存在组合关系,2个Arm实体组合成一个Student实体。


2. 背景知识

我们介绍一些背景知识热热身:

  1. 第一本讲述设计模式-可复用面向对象软件的基础(Design Patterns Elements of Reusable Object-Oriented Software)的书在1994年出版
  2. Java语言在1995年推出
  3. UML1.x在1997年推出


在Design Patterns by Gang of Four(参考资料c的别名) 1.6节中有一段叫做Relating Run-Time and Compile-Time Structures的段落:

Consider the distinction between object aggregation and acquaintance and how
differently they manifest themselves at compile- and run-times. Aggregation
implies that one object owns or is responsible for another object. Generally we
speak of an object having or being part of another object. Aggregation implies
that an aggregate object and its owner have identical lifetimes.

Acquaintance implies that an object merely knows of another object. Sometimes
acquaintance is called "association" or the "using" relationship. Acquainted
objects may request operations of each other, but they aren't responsible for
each other. Acquaintance is a weaker relationship than aggregation and suggests
much looser coupling between objects.

...

Ultimately, acquaintance and aggregation are determined more by intent than by
explicit language mechanisms. The distinction may be hard to see in the
compile-time structure, but it's significant. Aggregation relationships tend to
be fewer and more permanent than acquaintance. Acquaintances, in contrast, are
made and remade more frequently, sometimes existing only for the duration of an
operation. Acquaintances are more dynamic as well, making them more difficult
to discern in the source code.
大意是在讨论两个词语:aggregation和acquaintance。


而有意思的是随后的UML中却同样定义了一个aggregation的概念,然后就是association、composition。

为什么说这有意思呢?因为两者对同样的词语表达的概念实际上有所出入,UML中的aggregation实际上说的是GoF(参考资料c的别名)中的acquaintance,UML中的composition则是GoF中的aggregation。不过现今普遍采用的是UML中的概念了。


3. 为什么说是谬误

通过除了d的参考资料我们可以得知,association说的实际上是一种对象之间的所属与生命周期的关系,而不是本文图片下面表达出的("即1个或多个Student实体可以聚合成一个BasketBallClub实体;而Aram(手臂)和Student之间存在组合关系,2个Arm实体组合成一个Student实体")被聚合/组合对象构建出主体对象的方式。这中间的区别很微妙,语文没过200分的很难发现(呀,谁扔的砖头)。

Student和BasketBallClub是聚合关系(准确来说BasketBallClub聚合Student),可以得出的结论是BasketBallClub可以有0个或者多个Student,且BasketBallClub对Student的生命周期不负任何责任,同样BasketBallClub死亡也跟Student不会有一毛钱关系。

2个Arm和Student是组合关系(准确来说是Student组合Arm),可以得出的结论是Student必定会有2个Arm,且Student对这2个Arm的生命周期负有责任,如果Student死亡Arm也会死亡。

综上,本文图片下的文字的说法简直就是毫无根据。一个人想吃饭,我们能断定饭想被他吃么?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值