【翻译】 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(类名)

    • 类的名字显示在第一部分
  2. 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.
    • 在代码中属性映射成为成员变量(数据成员)
  3. 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.
    • 例如,一个单元格与一个表达式有关。这个关系的本质是该表达式是该单元格的公式

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 Rightpublic(+)private(-)protected(#)Package(~)
Members of the same class(同类的成员)yesyesyesyes
Members of derived classes(派生类的成员)yesnoyesyes
Members of any other class(其他类的成员)yesnonoin 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 对我的帮助。 希望下次的翻译能够给大家带来更好的阅读体验,在此拜谢。

Last modification:September 4th, 2018 at 10:57 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment