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.dom
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: 在系統中有類似角色的一組對象的一個描述,其包含:ui
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: 一個個類由如下三部分組成:lua
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).設計
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.
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)
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.
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.
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.
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.