UML類圖中5中關係的辨析(修訂)

 

   Figure 1. UML offers five different types of class relationship

  要理解這5中關係就不能簡單的把它們和具體的語言實現相關聯,UML介於問題域和解域之間,因而兼有這兩者的概念。雖然類圖是靜態的,但類之間的這5種關係卻是動靜兼有的(也就是編譯時和運行時混合的)。

    問題域、解域混合,編譯時、運行時混合是這5種關係的特點。


用詞
問題域
解域
編譯時
運行時
Dependency uses a
短暫的或者對非業務類的(如工具類)依賴
作用域在方法內部的reference(可能是方法參數或方法內部聲明的 reference 作用域在方法內部的reference(可能是方法參數或方法內部聲明的 reference 短暫的
Association has a
相對固定的,對業務類的依賴
類屬性 類屬性 持續一定時間的
Aggregation owns but may share
owns but may share 類屬性 類屬性 生命線可能相關聯
Composition is part of
is part of 類屬性 類屬性 生命線總是關聯
Generalization is a type of
is a type of 繼承
繼承




Dependency依賴:uses a

 

    Dependency表示一個類uses或者知道另一個類。一方的改動將引起另一方的改動。這是一種典型的臨時關係,代表了類之間的一種短暫的交互。

    針對“一方的改動將引起另一方的改動”這句話來講,所有強於dependency的關係都蘊含/隱含者dependency關係 。且這個意義常用來表示package之間的依賴關係。(package之間只有依賴和繼承2種關係)

    因爲是一種短暫的關係,所以依賴類指向目標類的reference的作用域一般在一個方法的內部(這有可能是方法的傳入參數或者在方法內部聲明的reference),而不是 類的屬性。因爲屬性代表一種相對持久的關係,而方法內部的對象,只有在方法被調用時(短暫的)才與依賴類(方法所在的類)發生關係。比如window和 event之間,只有用戶事件時兩者纔會發生關聯,當沒有事件時兩者並不一起工作,他們之間的合作關係是短時間的。我們也不會把event聲明爲 windows的一個屬性,而一般在windows的方法裏進行處理。

    Dependency 還經常用於表示一種對通用模塊(如java.util.regexjava.math )而非業務模塊的依賴關係。這顯然是一種問題域(而不是解域或技術角度)的思維角度。



Association關聯:has a

    Associations表示類之間的一種持續一段時間的合作關係,但被關聯的2者生命線(順序圖的概念)不被綁定(也就是說如果一個類被銷燬,另一個類不一定被銷燬)。

    在具體實現時往往表示爲類的屬性。但也不是所有的屬性都是Associations,像int/bool這類的屬性就不易表示爲Associations。

    關聯是可以有導航的,就是可以有方向,用帶箭頭的實線表示。



Aggregation聚合: owns but may share
    Aggregation 表示一種owns(擁有)關係,並且 被關聯的2者生命線可能 被關聯。

    在UML規範中對這種關係的定義非常模糊,充斥這也許、可能這類字眼。《UML Distilled》中甚至說“聚合是完全沒有意義的。因此,我建議,你在自己的的圖中略去聚合。”

    我個人也認爲Aggregation 是較難判斷的一類關係,很難確切定義它。它的上面association和下面composition的定義都很明確,大概可理解爲介於兩者之間,但偏composition。

Composition組合:is part of

    Composition用於表示“整體-部分”關係。這種關係是“非共享”的,也就是說,在任何時間,“部分”只能包含在一個“整體”中。二者的生命線總是相連的,如果整體被銷燬了,部分也就不存在了。

    這裏的“部分”不一定是多個。

    這裏要提醒一下,這裏的“非共享”關係指的是實例,而不是類。就是說一個類的不同實例可以屬於不同的擁有者,但一個特定的實例只能有一個擁有者。



Generalization 泛化(Otherwise Known as Inheritance): is a type of

 

以上5類關係依賴性由弱到強。

比較常用的是association/composition/generalization

 

 

 

以下是相關概念的英文表述:

Dependency between classes means that one class uses, or has knowledge of, another class. It is typically a transient relationship, meaning a dependent class briefly interacts with the target class but typically doesn't retain a relationship with it for any real length of time.

A dependency implies only that objects of a class can work together.
The dependency relationship is often used when you have a class that is providing a set of general-purpose utility functions, such as in Java's regular expression (java.util.regex ) and mathematics (java.math ) packages. Classes depend on the java.util.regex and java.math classes to use the utilities that those classes offer.

 

Associations are stronger than dependencies and typically indicate that one class retains a relationship to another class over an extended period of time. The lifelines of two objects linked by associations are probably not tied together (meaning one can be destroyed without necessarily destroying the other).
Association means that a class will actually contain a reference to an object, or objects, of the other class in the form of an attribute.

 

Aggregation is a stronger version of association. Unlike association, aggregation typically implies ownership and may imply a relationship between lifelines.

 

Composition is used to capture a whole-part relationship. The "part" piece of the relationship can be involved in only one composition relationship at any given time. The lifetime of instances involved in composition relationships is almost always linked; if the larger, owning instance is destroyed, it almost always destroys the part piece.

 

參考文獻

1.O'Reilly.Learning.UML.2.0.Apr.2006

2.O'Reilly.UML.2.0.in.a.Nutshell.Jun.2005

3.UML Distilled 3rd edition

4.道法自然:面向對象實踐指南

 

 

ps:本想收藏,可是想到收藏和記錄總是有些不太一樣,所以重新作爲文章發出.

原文地址:http://mahuangyihao.iteye.com/blog/186875

發佈了29 篇原創文章 · 獲贊 0 · 訪問量 627
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章