UML中Composition和Aggregation的差異

其實這在我之前Interview某HxC公司被打槍的時候有被問到,當時小弟才疏學淺(其實現在也沒充實到哪裡去),這幾天突然又想起這個問題,google了一堆網頁,寫的都不知所云,wiki裡面說:
Object composition is a way to combine simple objects or data types into more complex ones.
Aggregation differs from ordinary composition in that it does not imply ownership. 
媽咧... 誰知道這是在說什麼,其實簡單說,Composition就是在constructor裡面建立的物件,Aggregation就是用參數傳進去的物件。這應該是全世界最簡單的解釋了吧?

這是Composition:
class Penis{
...
}
class Man {
 Man() {
   Penis bigLP = new Penis();
   ...
 }
}
這是Aggregation:
class Man {
  Cloth myCloth;
  Man(Cloth cloth) {
    this.myCloth = cloth;
  }
}

這世界上還有比這更簡單的解釋我輸你 XD

留言

這個網誌中的熱門文章

使用Mac的Preview來合併兩張圖片

重拾起書本

Mac OS X 10.6.3 上的黑體-繁