2012年9月18日 星期二

UML中的關聯


class之間的關係有以下幾種
1. Association / 關聯 / knows a
X知道Y的存在
X可能以pointer(指標)或reference(參考)知道Y的存在
在概念模型階段方向性(箭頭)通常不太有意義可以省略

2. Dependency / 相依 / use a
A dependency exists between two elements if changes to the definition of one element (the supplier) may cause changes to the other (the client).
X相依Y
Y若改變 X可能會受到影響 但X的改變不會影響到Y
X可能有某個function可以call Y的function
舉個例子:
有一個 class Circle,提供 drawCircle 的功能,然 drawCircle 內部是使用 java 的 graphic 來實作繪圖。則我們可以說:Circle use a graphic. 也就是 Circle 相依於 graphic.

3. Composition / 組合 / has a
car 組合 wheel
Composition是一種整體完全擁有部分
如車子(car)擁有4個輪子(wheel)
當車子(car)消滅時4個輪子(wheel)同時也會消滅

4. Aggregation / 聚合
car 聚合 passenger
Aggregation是一種"擁有性"比較弱的關係
如:
車子(car)裡面有4個人(passenger)
當車子(car)消滅時4個人(passenger)不會消滅

Composition跟Aggregation在意義的差異在於:
Composition是『同生共死』,當System物件死亡時,Component物件也要跟著死亡。Aggregation是『生死有命』,當System物件死亡時,Component物件並不特別去處理。
在C#、Java這類有garbage collection的語言,composition幾乎不會用到,但在C++,只要用到pointer,就得自己去delete,所以在composition對於C++就很重要。


再舉個例子,(from wiki)
a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.

5. Inheritance / 繼承 (UML以繼承表示泛型關係)
X繼承Z,Y繼承Z,此時Z和X,Y之間就存在著泛型關係。
Z是共同化(抽象化),X/Y則是特性化(具體化)
如:
BMW / Benz 繼承Car,則Car和BMW / Benx就存在泛型關係

沒有留言:

張貼留言