【翻譯】 What is class diagram(什麼是類圖)?
寫在翻譯之前
這是一篇關於UML的英文部落格的翻譯,是我們的老師在教授我們UML類圖的時候推薦給我們的,為了學習UML順便學習英語,所以就想把它翻譯出來(翻譯為中英文對照版本)。文中可能有些地方不嚴謹或者謬誤的地方還望大家指出。下面是原文連結。 www.visual-paradigm.com/guide/uml-u…
文章正文
In software engineering, a class diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the relationships among objects.
在軟體工程中,類圖在統一建模語言(UML)中屬於靜態結構圖,它通過展示系統的類的屬性、操作(或方法)還有物件之間的關係的方式方式描述了系統的結構。
Purpose of Class Diagrams(類圖的目的)
- Shows static structure of classifiers in a system
- 顯示系統的中的分類器的靜態結構
- Diagram provides basic notation for other structure diagrams prescribed by UML
- 類圖為UML規定的其他的結構圖提供了符號
- Helpful for developers and other team members too
- 對開發人員是有幫助的,對其他的團隊成員同樣也有幫助
- Business Analysts can use class diagrams to model systems from business perspective
- 業務分析人員能使用類圖從業務角度對系統進行建模
A UML class diagram is made up of: 類圖的組成有:
- A set of classes and(PS: 原文就是這樣少了段話)
- A set of relationships between classes
- 類與類之間的一組關係
What is a Class(什麼是一個類)
A description of a group of objects all with similar roles in the system, which consists of: 在系統中有相似角色的一組物件的一個描述,其包含:
- Structural features (attributes) define what objects of the class "know"
- 結構特徵(特性), 定義了類的物件“知道”什麼
- Represent the state of an object of the class
- 代表類的一個物件的狀態
- Are descriptions of the structural or static features of a class
- 一個類的結構的和靜態的特性(特徵)的一些描述
- Behavioral features (operations) define what objects of the class "can do"
- 行為特性(操作)定義一個類的一些物件 “能做”什麼
- Define the way in which objects may interact
- 定義了類的物件能夠以什麼樣的方式互動
- Operations are descriptions of behavioral or dynamic features of a class
- 操作是一個類的行為的或動態特性的一些描述
Class Notation(類符號)
A class notation consists of three parts: 一個個類由以下三部分組成:
- Class Name(類名)
- 類的名字顯示在第一部分
- Class Attributes(屬性)
- Attributes are shown in the second partition.
- 類的屬性顯示在第二部分
- The attribute type is shown after the colon.
- 屬性的型別顯示在冒號的後面
- Attributes map onto member variables (data members) in code.
- 在程式碼中屬性對映成為成員變數(資料成員)
- Class Operations (Methods) (類的操作(方法))
- Operations are shown in the third partition. They are services the class provides.
- 操作/方法顯示在第三部分。他們是類提供的服務
- The return type of a method is shown after the colon at the end of the method signature.
- 返回型別顯示在位於方法簽名結尾的冒號的後面
- Operations map onto class methods in code
- 在程式碼中類的操作對映成為方法
The graphical representation of the class - MyClass as shown above: MyClass 類的圖形表示如上所示:
- MyClass has 3 attributes and 3 operations
- MyClass具有3個屬性和3個操作/方法
- Parameter p3 of op2 is of type int
- op2的引數p3 的型別是 int
- op2 returns a float
- op2 的返回值型別是 float
- op3 returns a pointer (denoted by a *) to Class6
- op3 返回了一個指向Class6的指標(由 a* 表示)
Class Relationships(類的關係)
A class may be involved in one or more relationships with other classes. A relationship can be one of the following types: (Refer to the figure on the right for the graphical representation of relationships).
一個類可能與一個或者多個其他的類產生關係。 一個關係能夠是一下的型別:(各個關係的影象化表示的方式參考右圖)。
PS: 因為Markdown的表格操作很噁心,所以下面的格式就沒用表格,主要是因為在表格中巢狀了圖片不好處理。
Inheritance (or Generalization) 繼承(泛化):
- Represents an "is-a" relationship.
- 表示了一個
is-a
的關係 - An abstract class name is shown in italics.
- 一個抽象類的類名被顯示為 斜體
- SubClass1 and SubClass2 are specializations of Super Class.
- SubClass1 和 SubClass2 是 SuperClass 的特殊化 (譯者注: 即SubClass1和SubClass2是特殊的Super Class)
- A solid line with a hollow arrowhead that point from the child to the parent class
- 一個從子類指向父類的帶中控箭頭的實線
Simple Association(簡單關聯):
- A structural link between two peer classes.
- 在兩個對等的類之間的一個結構的連線
- There is an association between Class1 and Class2
- Class1 和 Class2 之間存在著一個關聯(association)
- A solid line connecting two classes
- 通過一條實線連結兩個類
Aggregation(聚合)
A special type of association. It represents a "part of" relationship. 一個特殊的關聯關係。 其代表著"part of" 的關係
- Class2 is part of Class1.
- Class2 是Class1的一部分
- Many instances (denoted by the *) of Class2 can be associated with Class1.
- Class2的多個例項(由 * 表示) 能夠與Class1 關聯
- Objects of Class1 and Class2 have separate lifetimes.
- Class1 的物件和Class2 的物件有各自的生命週期
- A solid line with a unfilled diamond at the association end connected to the class of composite
- 一個在關聯的末端帶有空心菱形的實線連線到合成的類(譯者注: 空心菱形連線到被聚合的類)
Composition(組合)
A special type of aggregation where parts are destroyed when the whole is destroyed. 一種特殊的關聯關係,當整體被破壞的時候,部分也被破壞
- Objects of Class2 live and die with Class1.
- Class2的物件伴隨著Class1的物件的死亡而死亡
- Class2 cannot stand by itself.
- Class2 不能夠獨立存在
- A solid line with a filled diamond at the association connected to the class of composite
- 一個在關聯的末端帶有實心菱形的實線連線到合成的類(譯者注: 實心菱形連線到被組合的類)
Dependency(依賴)
- Exists between two classes if changes to the definition of one may cause changes to the other (but not the other way around).
- 如果改變一個類的定義可能引起另一個類的改變(反之則不行),則兩個類之間存在依賴關係
- Class1 depends on Class2
- Class1 依賴於 Class2
- A dashed line with an open arrow
- 一個帶有開放箭頭的虛線
Relationship Names(關係的名稱)
- Names of relationships are written in the middle of the association line.
- 關係的名稱被寫在連線線的中間
- Good relation names make sense when you read them out loud:
- 一個好的關係的名稱是有意義的,當你大聲讀出他們的時候
- "Every spreadsheet contains some number of cells",
- "每一個電子表格包含一些單元格"
- "an expression evaluates to a value"
- 一個表示式計算出一個值
- They often have a small arrowhead to show the direction in which direction to read the relationship, e.g., expressions evaluate to values, but values do not evaluate to expressions.
- 他們經常有個小箭頭來顯示去讀他們之間的關係時的方向, 例如: 一個表示式計算出一個值而不是一個值計算出一個表示式。
Relationship - Roles(關係-角色)
- A role is a directional purpose of an association.
- 角色是一個關係的定向的目的
- Roles are written at the ends of an association line and describe the purpose played by that class in the relationship.
- 角色被寫在連線線的末尾並用來描述該類在關係中扮演的目的
- E.g., A cell is related to an expression. The nature of the relationship is that the expression is the formula of the cell.
- 例如,一個單元格與一個表示式有關。這個關係的本質是該表示式是該單元格的公式
Navigability(導航性)
PS:這個標題是實在是不知道怎麼翻譯是好
The arrows indicate whether, given one instance participating in a relationship, it is possible to determine the instances of the other class that are related to it. 一個箭頭指示,給一個關係的類的例項,來斷定另一個與之相關的類的例項是否是可能的。
The diagram above suggests that 上面的圖表明
- Given a spreadsheet, we can locate all of the cells that it contains, but that
- 給一個電子表格我們能夠定位到它所包含的所有的單元格,但是
- we cannot determine from a cell in what spreadsheet it is contained.
- 我們不能夠從一個單元格斷定他被哪個電子表格所包含
- Given a cell, we can obtain the related expression and value, but
- 給一個單元格我們能夠獲得與之相關的表示式和值,但是
- given a value (or expression) we cannot find the cell of which those are attributes.
- 給一個值(或表示式),我們不能找到其中有這些屬性的單元格
Visibility of Class attributes and Operations(類的屬性和方法的可見性)
In object-oriented design, there is a notation of visibility for attributes and operations. UML identifies four types of visibility: public, protected, private, and package. 在物件導向設計中, 有一些控制屬性和操作/方法的可見性修飾符。 UML確定了四種可見性修飾符: public、protected、private、*package .
The +, -, # and ~ symbols before an attribute and operation name in a class denote the visibility of the attribute and operation.
在類中的屬性和方法名之前的 +
-
#
和 ~
符號,代表著屬性和方法的可見性。
+
denotes public attributes or operations+
代表公開的屬性和方法-
denotes private attributes or operations-
代表私有的屬性和方法#
denotes protected attributes or operations#
代表受保護的屬性和方法~
denotes package attributes or operations~
代表著包級的屬性和方法(譯者注: 只有在同一包內才能訪問的屬性和方法,僅供大家參考)
Class Visibility Example(類可見性的例子)
In the example above: 上方的例子中:
- attribute1 and op1 of MyClassName are public
- MyClass類的 attributes1 和 op1 是public的
- attribute3 and op3 are protected.
- attribute3 和 op3 是 protected
- attribute2 and op2 are private.
- attribute2 和 op2 是 private
Access for each of these visibility types is shown below for members of different classes. 對於不同的類的成員的,每一種可見性型別的訪問許可權,在下方顯示:
Access Right | public(+) | private(-) | protected(#) | Package(~) |
---|---|---|---|---|
Members of the same class(同類的成員) | yes | yes | yes | yes |
Members of derived classes(派生類的成員) | yes | no | yes | yes |
Members of any other class(其他類的成員) | yes | no | no | in smae package(在同一包下的可以訪問) |
Multiplicity(多重性)
How many objects of each class take part in the relationships and multiplicity can be expressed as: 每個類的多少物件參與類之間的關係,多重性可以表示為一下:
- Exactly one - 1
- 恰好是1 -
1
- Zero or one - 0..1
- 0或1
0..1
- Many - 0..* or *
- 多 -
0..* 或者 *
- One or more - 1..*
- 一或多 -
1..*
- Exact Number - e.g. 3..4 or 6
- 確定的數 - 例如
3..4
或6
- Or a complex relationship - e.g. 0..1, 3..4, 6.* would mean any number of objects other than 2 or 5
- 或是是一個複雜的關係 - 例如
0--1,3..4,6.*
,意味著除了2和5之外的任意數量的物件
Multiplicity Example(多重性的例子)
- Requirement: A Student can take many Courses and many Students can be enrolled in one Course.
- 要求: 一個學生可以選擇多門課程並且多個學生可以選修一個課程。
- In the example below, the class diagram (on the left), describes the statement of the requirement above for the static model while the object diagram (on the right) shows the snapshot (an instance of the class diagram) of the course enrollment for the courses Software Engineering and Database Management respectively)
- 在下面的例子中, 類圖(位於左邊),描述了上述要求的靜態模型的宣告,然而物件圖(右邊) 顯示了對於
Software Engineering
和Database Management
課程的快照(該類圖的一個例項)。
Aggregation Example - Computer and parts(聚合的例子- 電腦和電腦元件)
- An aggregation is a special case of association denoting a "consists-of" hierarchy
- 聚合關係是關聯關係的一種特殊的情況,表示一個
consists-of(包含)
的層級結構 - The aggregate is the parent class, the components are the children classes
- 聚合是父類,元件是子類
Inheritance Example - Cell Taxonomy(繼承的例子-單元格分類)
- Inheritance is another special case of an association denoting a "kind-of" hierarchy
- 繼承是另一種特殊的關聯關係 表示一種 “kind-of(歸類)” 的層級結構
- Inheritance simplifies the analysis model by introducing a taxonomy
- 繼承通過引入分類法(taxonomy),簡化了分析模型
- The children classes inherit the attributes and operations of the parent class.
- 子類繼承父類的屬性和方法
Class Diagram - Diagram Tool Example(類圖-Diagram Tool Example)
A class diagram may also have notes attached to classes or relationships. Notes are shown in grey. 一個類圖也可能有附加到類或者關係上的註釋(notes)。 註釋一灰色顯示。
In the example above: 在上面的例子中
We can interpret the meaning of the above class diagram by reading through the points as following. 通過下面的要點我們可以理解上面的類圖的含義.
- Shape is an abstract class. It is shown in Italics.
- Shape類是一個抽象類,它以斜體顯示
- Shape is a superclass. Circle, Rectangle and Polygon are derived from Shape. In other words, a Circle is-a Shape. This is a generalization / inheritance relationship.
- Shape是一個超類。 Circle, Rectangle 和 Polygon(多邊形) 派生自 Shape。 換句話說 Circle
is-a
Shape。這是一種泛化/繼承。 - There is an association between DialogBox and DataController.
- 在DialogBox和 DataController之間存在一個關聯關係
- Shape is part-of Window. This is an aggregation relationship. Shape can exist without Window.
- Shape是
"part-of"
Window。 這是一種聚合關係。 Shape可以脫離Window獨立存在。 - Point is part-of Circle. This is a composition relationship. Point cannot exist without a Circle.
- Point 是 "part-of" Circle. 這是一個組合關係。 Point脫離Circle以後不能獨立存在。
- Window is dependent on Event. However, Event is not dependent on Window.
- Window 依賴於 Event。無關怎麼說, Event沒有依賴於Window
- The attributes of Circle are radius and center. This is an entity class.
- Circle的屬性是
center(圓心)
和radius(半徑)
。 這是一個實體類。 - The method names of Circle are area(), circum(), setCenter() and setRadius().
- Circle有這些方法,名稱分別是:
area()
circum()
setCenter()
和setRadius()
- The parameter radius in Circle is an in parameter of type float.
- Circle中的
radius(半徑)
引數是一個 float 型別的引數 - The method area() of class Circle returns a value of type double.
- Circle中的
area()
方法的返回值型別是 double 型別 - The attributes and method names of Rectangle are hidden. Some other classes in the diagram also have their attributes and method names hidden.
- Rectangle的屬性和方法名被隱藏了。在該類圖中也有一些類具有他們的隱藏的屬性和方法
Dealing with Complex System - Multiple or Single Class Diagram?(處理複雜的系統 - 多個或單個類圖?)
Inevitably, if you are modeling a large system or a large business area, there will be numerous entities you must consider. Should we use multiple or a single class diagram for modeling the problem? The answer is: 不可避免的,如果你正在建模一個大的系統或者大的業務領域,在這時你必須考慮大量的實體。那麼我們應該使用多個類圖還是單個類圖對問題進行建模?答案是:
- Instead of modeling every entity and its relationships on a single class diagram, it is better to use multiple class diagrams.
- 與其將所有的實體和他們之間的關係都建模在單個類圖中,使用多個類圖是一個更好的選擇。
- Dividing a system into multiple class diagrams makes the system easier to understand, especially if each diagram is a graphical representation of a specific part of the system.
- 將一個系統拆分到多個類圖中使得該系統更易於理解,尤其是每一類圖是該系統的一個特定的模組的圖形化表示的時候。
Perspectives of Class Diagram in Software Development Lifecycle
We can use class diagrams in different development phases of a software development lifecycle and typically by modeling class diagrams in three different perspectives (levels of detail) progressively as we move forward: 我們能夠在軟體開發生命週期的不同的開發階段使用類圖, 通常在我們不斷推進軟體開發週期的的過程中,以三種不同的視角(粒度的級別)來構建類圖:
Conceptual perspective: The diagrams are interpreted as describing things in the real world. Thus, if you take the conceptual perspective you draw a diagram that represents the concepts in the domain under study. These concepts will naturally relate to the classes that implement them. The conceptual perspective is considered language-independent.
概念視角(conceptual perspective): 該類圖被理解為描述真實世界中的事物的圖。因此,如果你持概念的視角的話,你畫出來的類圖是代表所研究的領域中的概念的類圖。這些概念將自然地與他們的實現類相關聯。概念視角被視為是獨立於語言的。
Specification perspective: The diagrams are interpreted as describing software abstractions or components with specifications and interfaces but with no commitment to a particular implementation. Thus, if you take the specification perspective we are looking at the interfaces of the software not the implementation.
規範視角(Specification perspective): 該類圖被理解為描述具有規範和結構的軟體抽象或元件但是對特定的實現沒有承諾的圖。 因此,如果你持規範的角度來畫圖,我們所關心的是軟體的介面而不是實現。
Implementation perspective: The diagrams are interpreted as describing software implementations in a particular technology and language. Thus, if you take the implementation perspective we are looking at the software implementation.
實現視角(Implementation perspective): 該類圖被理解為以特定的技術和語言來實現軟體的圖。因此,如果你持實現的視角來畫圖,我們所關心的是軟體的實現。
結語
首先不得不承認,我的翻譯的確很糟糕,比較晦澀難懂。 同時再翻譯這篇部落格的時候我也認識到了,翻譯所帶來的語意的流失是非常嚴重的,如果有能力的話,還是比較推薦大家看英文。通過翻譯這篇部落格也學到了不少的知識,不管是英語上的還是專業上的,說到翻譯,必須得感謝我的英語老師--Frankie Gao
對我的幫助。 希望下次的翻譯能夠給大家帶來更好的閱讀體驗,在此拜謝。