Class Diagram

Figure 4.30 Elements of the class diagram
关联,多重性; 聚合aggregation。

In class diagrams, as shown in Figure 4.30, we work with the following elements:

Class

A class represents a relevant concept from the domain, a set of persons, objects, or ideas that are depicted in the IT system:

Examples of classes are passengers, planes, or tickets.

Attribute

An attribute of a class represents a characteristic of a class that is of interest for the user of the IT system:

Characteristics of interest of a passenger, for example, are name and age.

Generalization

Generalization is a relationship between two classes: a general class and a special class:

Refer to Generalization, Specialization, and Inheritance.

Association

An association represents a relationship between two classes:

An association indicates that objects of one class have a relationship with objects of another class, in which this connection has a specifically defined meaning (for example, "is flown with").

Multiplicity

A multiplicity allows for statements about the number of objects that are involved in an association:

Also see Figure 4.32.

Aggregation

An aggregation is a special case of an association (see above) meaning "consists of":

The diamond documents this meaning; a caption is unnecessary.

Reading Class Diagrams

Figure 4.31 shows a class diagram from our case study with the classes customer, ticket, and coupon, their attributes, and their associations:

Figure 4.31 Class diagram with associations

Looking at the class diagram in Figure 4.31, you can read the association between the classes customer and ticket as follows:

  • One (this sentence always begins with "one") object of the first class has an association with a number of objects of the second class.

The appropriate values from the diagram have to be inserted into this first abstract formulation, which can be universally applied. The name of one class is customer (1); the name of the other class is ticket (4). The name of the association is owns (2):

  • A customer (1) owns (2) * (3) ticket (4).

If the asterisk is exchanged with its meaning, a regular English sentence is created:

  • A customer (1) owns (2) zero, one or several (3) ticket(s) (4).

Since associations usually are not directional, meaning usually go both directions, our association also has a meaning in the other direction:

  • A ticket (4) is owned by (2) exactly one (5) customer (1).

The small triangle next to the name of the association (2) indicates in which direction the name of the association holds true. We can read all the associations in the class diagram in this way.

The specification of the number of objects of the second class (you always start with one object of the first class) is called the multiplicity. The course of action should always be according to the same pattern:

First, a statement of the lower limit (minimum number) followed by two periods (..) and a statement of the upper limit (maximum number).

Figure 4.32 shows the most common possibilities:

Figure 4.32 Multiplicities

However, in UML it is also possible to insert any values as the lower and upper limits, e.g., 2 .. 4 or 6 .. *.

The association's name is necessary for understanding the domain meaning of the association. In contrast to the association itself, which applies to both directions, the name of the association applies to only one direction, which is indicated by a black triangle. If the association is not labeled, its meaning has to be derived from the domain context, or it takes on a general meaning such as has or belongs to. In case of doubt it is better to label associations too much than too little. Many diagrams that we have encountered in our practical experience were incomprehensible because associations were not labeled.

Associations can also be viewed as the implementation of static business rules (see Static and Dynamic Business Rules). Statements such as "a ticket belongs to exactly one customer" are documented in the class diagram by associations.

Roles are another possible way in UML to give relationships between classes a domain meaning. In this way, we can state what role an object of one class plays for the objects of another class:

Figure 4.33 Class diagram with roles

Looking at the class diagram in Figure 4.33, we can read the left association with roles between the classes flight number and airport as follows:

  • An airport (1) is a start (location(2) for one or more (3) flightnumbers (4).

There is another association between the two classes flight number and airport:

  • An airport (1) is a destination (5) for one or more flight numbers (4).

These two associations also have inversions, even though roles are only stated for one direction:

  • flight number (4) has as start (location) (2) exactly one (7) airport (1).
  • A flight number (4) has as destination (5) exactly one (8) airport (1).

This records that a certain flight number has a departure airport and a destination airport. An example of a flight number is LX317, a daily flight of the Swiss airline Crossair from London to Zurich.

Figure 4.34 Class diagram with Aggregation

Among the many domain meanings that an association can have there is one that can be signified with UML by its own symbol: the whole-part relationship or aggregation. This type of relationship is always used when objects of one class are a part of objects of another class.

In the class diagram in Figure 4.34 aggregation is used on the left side (the white diamond), which can be read as follows:

  • A ticket (1) consists of (2) 1 to 4 (3) coupons (4).

or the other way around:

  • A coupon (4) is part of (2) exactly one (5) ticket (1).

The example without a diamond, but with a name for the association, has exactly the same meaning! The last missing element of UML that we use to model class diagrams is generalization/specialization, which serves to depict the relationship between a superclass and a subclass. The generalization/specialization in Figure 4.35 can be read from top to bottom or bottom to top. If you begin at the top, you find a class Freight (1) with the attributes: Identification, Weight, and ID-number (2). This class has two specializations, Piece of Luggage (3) and Piece of Cargo (4). The class Piece of Cargo has an additional attribute: Degree of Hazardousness (5).

If you begin at the bottom you will find the classes Piece of Luggage (3) and Piece of Cargo(4). These have a superclass, the class Freight (1), which contains the shared attributes (and functions) of the subclasses.

Figure 4.35 Class diagram with generalization/specialization

[概念理解] UML类建模的更多相关文章

  1. UML类建模(强烈推荐-思路很清晰)

    UML类建模(强烈推荐-思路很清晰) 2016年10月23日 15:17:47 mbshqqb 阅读数:2315 标签: uml面向对象设计模式 更多 个人分类: 面向对象程序设计   UML的构造快 ...

  2. UML类图关系大全

    UML类图关系大全 1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法.在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设 ...

  3. UML类图关系大全【转】

    UML类图关系大全 1.关联 双向关联:C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设 ...

  4. Java 大黑话讲解设计模式 -- UML类图

    目录 1.啥是UML类图? 2.UML类图有啥用? 3.正式理解UML类图 4.使用idea画第一个UML类图 5.类之间的关系图[必须牢记] 6.类之间的关系 6.1.依赖 6.2.泛化 6.3.实 ...

  5. UML类图关系全面剖析

    UML的类图关系分为: 关联.聚合/组合.依赖.泛化(继承).而其中关联又分为双向关联.单向关联.自身关联:下面就让我们一起来看看这些关系究竟是什么,以及它们的区别在哪里. 1.关联 双向关联:C1- ...

  6. UML类图关系-转

    1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设计模式内的类关系来 ...

  7. UML类图关系大全-转

    1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设计模式内的类关系来 ...

  8. UML类图关系表示

    UML 之 C++类图关系全面剖析 分类: 软件设计与架构2008-10-16 08:52 5165人阅读 评论(3) 收藏 举报 umlc++borderclasscblog UML的类图关系分为: ...

  9. 我对uml类图关系的理解

    uml类图的关系: 泛化关系也就是继承. 实现关系就是一个类实现另外一个接口. 依赖关系就是一个类使用了另外一个类,是一种使用关系,在这个类的某个服务中需要另外一个类来协助. 关联关系就是一类拥有另外 ...

随机推荐

  1. 区间DP入门

    所为区间DP,主要是把一个大区间拆分成几个小区间,先求小区间的最优值,然后合并起来求大区间的最优值. 区间DP最关键的就是满足最优子结构以及无后效性!! 例如像是石子合并和括号匹配这两类比较经典的模型 ...

  2. 【JavaEE-面试总结】(未完,待续···)

    目录: 一.Java基础 二.JavaEE基础 三.JavaEE进阶 四.数据库 五.数据结构&算法 六.高级(服务器) 一.Java基础 1.1 面向对象(封装.继承.多态) 访问权限修饰符 ...

  3. 微信小程序的坑之wx.miniProgram.postMessage

    工作中有个需求是小程序的网页在关闭的时候,需要回传给小程序一个参数 查阅小程序官方文档,有这样一个接口 wx.miniProgram.postMessage ,可以用来从网页向小程序发送消息,然后通过 ...

  4. Ubuntu16.04进入无限登录状态的解决办法

    具体来说就是,输入密码之后又到了登录界面,无限循环(也许可能不能输入密码,这种状态我没有测试) 此方案仅适用于安装过NVIDIA显卡驱动的系统并且在登录界面会发现分辨率变了 如果你没有安装过NVIDI ...

  5. Vue编写轮播组件引入better-scroll插件无法正常循环轮播

    临近过年还是发个博客表示一下自己的存在感,这段时间公司突然说想搞小程序,想到这无比巨大的坑就只能掩面而泣,于是乎这段时间在学习小程序开发.关于标题所说的是有老铁问的,我也跟着网上的代码码了一遍然后发现 ...

  6. Java面向对象重要关键字

    面向对象过程接触的重要关键字:extends   super   this   final   static  extends 关键字用作继承功能,可以申明一个类是从另外一个类继承而来的,一般形式如下 ...

  7. spring的IOC底层原理

    我们调用一个类的方法,首先是User user=new  User(),对象调用这个方法,user.add(),这种方法有一个缺陷就是代码的耦合度太高,比如你的servlet调用User类里的方法,需 ...

  8. Leetcode 227.基本计算器II

    基本计算器II 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  . 整数除法仅保留整数部分. 示例 1: 输入: " ...

  9. Bone Collector II(01背包kth)

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  10. 【MVC 2】MVC+EF框架结构实例:注册ID号验证

    导读:本篇博客,将通过一个实例,详细介绍MVC+EF的应用.原理性的东西或者说是进一步的解耦和,请看博客: [框架结构 3]MVC+EF实体框架-原理解析.在这里,仅用MVC框架和一个EF生成的Mod ...