首先看association,下圖是個典型的assoication class diagram。
association的表示法是實線加上箭頭,有兩個重點,navigability和multiplicity,navigability就是箭頭指的方向,指別的class的,表示負責維護association關係,在此class中有data member存著被指class的reference(pointer),multiplicity則是上面的數字,離自己class比較遠的,表示對方class和自己class之間的關係,如上圖,1個School可以有多個Student,而1個Student只能有一個School,而一個Student可以選1到6個Course,1個Course可被1到多個Student選。
association常見的問題是,到底箭頭該怎麼指才對?一般來說,若是一對多的關係,是由一指向多,因為通堂一個class會有個array或vector儲存多個物件,但這並非絕對,只能說通常如此,如上圖的Student對Course是一對多,且Course對Student也是一對多,但他選擇了由Course負責association關係,所以完全看設計需要,實務上,建議如UML for Java Programmer中文版p.3-15那樣,加上stereotype,詳細的敘述是屬於哪一種association,將來我會再專文介紹。
若以C++表示,association的程式碼如下
class A {
private:
B* itsB;
};
再來看aggregation
aggregation的表示法由空diamond和箭頭表示(選的這張圖是比較舊的UML格式,所以沒有箭頭),空diamond表示whole,箭頭表示part。若以英文表示,就是has a的關係,上圖是典型一對多的表示法。
若以C++表示,aggregation的程式碼如下
class Node {
private:
vector<Node*> itsNodes;
};
最後是composition,表示法與aggregation的差異在於變成實diamond,其他完全一樣,跟aggregation在意義的差異在於composition強調『同生共死』,當System物件死亡時,Component物件也要跟著死亡,但aggregation是『生死有命』,當System物件死亡時,Component物件並不特別去處理。
若以C++表示,composition的程式碼如下
class Car {
public:
virtual ~Car() {delete itsCarb;}
private:
Carburetor* itsCarb
};
由以上程式可知,composition須由destructor去處理,而aggregation則不必。
在C#、Java這類有garbage collection的語言,composition幾乎不會用到,但在C++,只要用到pointer,就得自己去delete,所以在composition對於C++就很重要。
原文:http://www.cnblogs.com/oomusou/archive/2007/02/20/652899.html
Association represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.
Aggregation [...] is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).
Composition [...] is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.
In terms of the strength of the Bond between classes in terms of strength of the association we can arrive at the below
Association < Aggregation < Composition
明白了吧