【翻譯】 What is class diagram(什麼是類圖)?

【翻譯】 What is class diagram(什麼是類圖)?

寫在翻譯以前

這是一篇關於UML的英文博客的翻譯,是咱們的老師在教授咱們UML類圖的時候推薦給咱們的,爲了學習UML順便學習英語,因此就想把它翻譯出來(翻譯爲中英文對照版本)。文中可能有些地方不嚴謹或者謬誤的地方還望你們指出。下面是原文連接。 www.visual-paradigm.com/guide/uml-u…express

文章正文

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

在軟件工程中,類圖在統一建模語言(UML)中屬於靜態結構圖,它經過展現系統的類的屬性、操做(或方法)還有對象之間的關係的方式方式描述了系統的結構。ide

01-class-diagram-in-uml-diagram-hierarchy

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: 在系統中有類似角色的一組對象的一個描述,其包含: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

  1. Class Name(類名)
  • 類的名字顯示在第一部分
  1. 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.
  • 在代碼中屬性映射成爲成員變量(數據成員)
  1. 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
  • 在代碼中類的操做映射成爲方法

02-simple-class

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的表格操做很噁心,因此下面的格式就沒用表格,主要是由於在表格中嵌套了圖片很差處理。code

Inheritance (or Generalization) 繼承(泛化):

03-inheritance

  • 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(簡單關聯):

04-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(聚合)

05-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(組合)

06-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(依賴)

07-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.
  • 他們常常有個小箭頭來顯示去讀他們之間的關係時的方向, 例如: 一個表達式計算出一個值而不是一個值計算出一個表達式。

08-relationship-name

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肯定了四種可見性修飾符: publicprotectedprivate、*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(類可見性的例子)

02-simple-class

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..46
  • 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 EngineeringDatabase Management 課程的快照(該類圖的一個實例)。

09-class-diagram-to-object-diagram

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
  • 聚合是父類,組件是子類

10-aggregation-example

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.
  • 子類繼承父類的屬性和方法

11-inheritance-example

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)。 註釋一灰色顯示。

12-uml-class-diagram-example

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 對個人幫助。 但願下次的翻譯可以給你們帶來更好的閱讀體驗,在此拜謝。

相關文章
相關標籤/搜索