天天看點

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

關鍵字: uml

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.regex ,java.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.javaeye.com/blog/186875

繼續閱讀