《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也會死亡。

綜上,本文圖片下的文字的說法簡直就是毫無根據。一個人想吃飯,我們能斷定飯想被他吃麼?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章