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


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

寫在翻譯之前

這是一篇關於UML的英文博客的翻譯,是我們的老師在教授我們UML類圖的時候推薦給我們的,為了學習UML順便學習英語,所以就想把它翻譯出來(翻譯為中英文對照版本)。文中可能有些地方不嚴謹或者謬誤的地方還望大家指出。下面是原文鏈接。
https://www.visual-paradigm.com/guide/uml-unified-modeling-language/what-is-class-diagram/?tdsourcetag=s_pctim_aiomsg

文章正文

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)中屬於靜態結構圖,它通過展示系統的類的屬性、操作(或方法)還有對象之間的關系的方式方式描述了系統的結構。

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

  • 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:
一個個類由以下三部分組成:

  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的表格操作很惡心,所以下面的格式就沒用表格,主要是因為在表格中嵌套了圖片不好處理。

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.
    • 例如,一個單元格與一個表達式有關。這個關系的本質是該表達式是該單元格的公式

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 對我的幫助。 希望下次的翻譯能夠給大家帶來更好的閱讀體驗,在此拜謝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM