《Java瘋狂講義》中關聯、組合和彙總的謬誤

來源:互聯網
上載者:User

標籤:call   ...   -o   img   data   general   mon   ast   csdn   

本文參考例如以下資料(建議閱讀):

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的類圖的概念理解上,書中所用的圖例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

這副圖上面的文字(書裡面的排版)我認為沒有太大的問題(實際上我認為另一些表達欠妥的地方)。摘錄例如以下:

關聯關係包括兩種特例:彙總和組合,它們都有部分和總體的關係,但通常覺得組合比彙總更加嚴格。

當某個實體彙總成還有一個實體時。該實體還能夠同一時候是還有一個實體的部分,比如。學生即能夠是籃球俱樂部的成員。也能夠是書法俱樂部的成員;當某個實體組合成還有一個實體時。該實體則不能同一時候是一個實體的部分。

彙總使用帶空心菱形框的實線表示,組合則使用帶實心菱形框的實線表示。


圖片下片的文字就狗屁不通了,摘錄例如以下:

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

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

《Java瘋狂講義》中關聯、組合和彙總的謬誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.