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

来源:互联网 发布:手机淘宝账号怎么查看 编辑:程序博客网 时间:2024/06/09 14:28

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

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 howdifferently they manifest themselves at compile- and run-times. Aggregationimplies that one object owns or is responsible for another object. Generally wespeak of an object having or being part of another object. Aggregation impliesthat an aggregate object and its owner have identical lifetimes.Acquaintance implies that an object merely knows of another object. Sometimesacquaintance is called "association" or the "using" relationship. Acquaintedobjects may request operations of each other, but they aren't responsible foreach other. Acquaintance is a weaker relationship than aggregation and suggestsmuch looser coupling between objects....Ultimately, acquaintance and aggregation are determined more by intent than byexplicit language mechanisms. The distinction may be hard to see in thecompile-time structure, but it's significant. Aggregation relationships tend tobe fewer and more permanent than acquaintance. Acquaintances, in contrast, aremade and remade more frequently, sometimes existing only for the duration of anoperation. Acquaintances are more dynamic as well, making them more difficultto 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
原创粉丝点击