2010年10月13日 星期三

[OO 小學堂] UML 類別圖介紹

前言 :
本文介紹UML的類別圖。類別圖是從類別觀點看系統的結構;物件圖則顯示系統結構在某個時間的實例。二種圖式有一部份是大同小異、並互通有無。以下段落依序介紹類別圖的圖示與示範。另一篇將介紹物件圖。類別圖描述系統的基本結構,並且模型建構符合當前的物件導向程式語言模型,例如Java;於是,類別圖非常適合當做在系統分析階段對程式人員解說的文件。

類別 : (Class)

主要用四個長方形從上向下層疊、其中以文字標明各段內容,依序表示類別名稱、屬性、操作方法、以及責任。屬性和操作方法可以用 + 或 - 等前綴符號表示存取能力是公開或私有,另外,或者可用 # 標示保護屬性、以及用 ~ 標示包裝 (即若干個類別的集合)。屬性的主體包含屬性名稱、和屬性的資料型態或類別。操作方法的主體包含方法名稱、括弧標示的參數項目、和方法的資料型態或類別。操作方法的參數要寫上參數名稱、和參數的資料型態或類別。如果操作方法有許多參數,要用逗點(,)分隔。第四個長方形要寫物件的責任,並且第四個長方形可以按需要而省略。

有時為了省略細節、而使表達較簡單又清楚,可以將類別屬性和操作方法的描述文字省略。或者,也可以把第二個長方形和第三個長方形省略,只用一個帶有類別名稱的長方形表示類別。


關聯 : (Association)

在一個類別和另一個類別之間拉出連接線、並在上方標示關聯詞(動詞)、二端視需求可標示參與數目和介面名稱,表示二個類別之間的關聯。例如,一或多個「數字」「標記」一種「物品」。參與數目 (multiplicity or cardinality) 可能標示為數字、表示多項的代用符號(*) 、或是像 "0..1" 的數字區段。可以在關聯詞旁邊以實心三角形標示方向。


彙總 : (Aggregation)

在連接線的一端加上空心菱形,表示一項類別(菱形端)對另一項類別有「整體與部份」的關聯。彙總的意思是 has-a ,通常是強調一項類別(菱形端)的包含了另一項類別的集合:例如,Man 有 身分證, 健保卡...等 (IDCard)。


組合 : (Composition)

在連接線的一端加上實心菱形,表示一項類別(菱形端)除了對另一項類別有「整體與部份」的關聯之外、而且是由另一項類別組成。組合的關聯力比彙總強,Composition 表示法與 Aggregation 的差異在於變成實 diamond,其他完全一樣,跟 Aggregation 在意義的差異在於 Composition 強調『同生共死』, 以下圖來解釋, 當 System 物件死亡時,Component 物件也要跟著死亡,但 aggregation 是『生死有命』,當 System 物件死亡時,Component 物件並不特別去處理。


一般化/特殊化 : (Generalization/specialization)

在連接線的一端加上空心三角形,表示一項類別 (三角形端) 是另一項類別的一般樣式;相對地,另一項類別是該類別的特殊樣式。一般化的意思是 is-a ,也是繼承的關係:例如,Bus 是一種車子。Bus 和 Taxi 是 Car 的子型 (subtype),而 Car 是母型(supertype) 。
可注意,一般化也可以用在使用案例圖,用來處理案例或參與者的從屬層次。


實現 : (Realization)

用帶箭頭的虛線從一項類別拉到另一項類別、並在上方標示用雙角括號 « » 包含的關聯詞 (動詞),表示一項類別實現了另一項類別的行為:例如,工程師與經理等角色都需要吃飯, 走路 etc . 故在 IPeople 的介面定義了所有Human being 都會有的行為。


依賴 : (Dependency)
從一項類別拉出一條帶箭頭的虛線到另一項類別、不帶標示文字,表示一項類別依賴另一項類別提供的功能。


補充說明 :
@. Association, Aggregation 與 Composition 的差別.
@. 本文轉載自 這裡
@. UML Tutorial - Class Diagrams
Class diagrams are widely used to describe the types of objects in a system and their relationships. Class diagrams model class structure and contents using design elements such as classes, packages and objects.2 Class diagrams describe three different perspectives when designing a system, conceptual, specification, and implementation.1 These perspectives become evident as the diagram is created and help solidify the design. This example is only meant as an introduction to the UML and class diagrams. If you would like to learn more see the Resources page for more detailed resources on UML.

@. www.developer.com : The UML Class Diagram Par1
So, what is a class diagram? Imagine you were given a task of drawing a family tree. The steps you would take would be:
- Identify the main members of the family
- Determine how they are related to each other
- Identify the characteristics of each family member
- Find relations among family members
- Decide the inheritance of personal traits and characters
A class diagram is similar to a family tree. A class diagram consists of a group of classes and interfaces reflecting important entities of the business domain of the system being modeled, and the relationships between these classes and interfaces. The classes and interfaces in the diagram represent the members of a family tree and the relationships between the classes are analogous to relationships between members in a family tree. Interestingly, classes in a class diagram are interconnected in a hierarchical fashion, like a set of parent classes (the grand patriarch or matriarch of the family, as the case may be) and related child classes under the parent classes.
This message was edited 18 times. Last update was at 01/09/2016 08:44:27

2 則留言:

  1. You can find more UML diagram examples in creately diagram community. there are 1000s of diagram examples and templates to be used freely.

    回覆刪除

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...