【UML】《Theach yourself uml in 24hours》——hour1

一、Hour1 介绍UML

1、为什么uml如此重要

书中说了很多uml为什么重要的原因,也举出了一些例子,但我觉得最好的一个原因是这个(上升到了思想层面):
作者认为学习的过程通常分为三个阶段:

  1. 第一阶段:你不知道你不知道什么(You don’t know what you don’t know)。这通常发生在你刚接触某个事物,这个阶段就是让你问问题,你也不知道问什么;
  2. 第二阶段:你知道了你不知道什么了(You do know what you don’t know)。这一阶段你知道了你所处领域有哪些东西,并且开始去弥补这些你不知道的东西;
  3. 第三阶段:你掌握了这一领域中必须的知识。

上述阶段可以总结为三个词:Modeling、Learning and knowledge
UML一个重要的能力就是让你直接进入第二个阶段,让你快速认知到你所接触的事物,并进而获取相关信息。

2、直接看一些uml的例子

uml是一门语言,与其先教语法,不如先看一些实际的例子更好理解。
以一个洗衣机系统举例:
(1)class icon
【UML】《Theach yourself uml in 24hours》——hour1
(2)instance icon 有两种
【UML】《Theach yourself uml in 24hours》——hour1
(3)Use Case Diagram 用例图
A use case is a description of a system’s behavior from a user’s standpoint.(从用户角度看系统行为的一种描述图)
【UML】《Theach yourself uml in 24hours》——hour1
(4)State Diagram 状态图
描述洗衣机状态的改变图
【UML】《Theach yourself uml in 24hours》——hour1
(5)Sequence diagram 时序图
class diagram or object diagram 只能表述静态信息,时序图可以表述实例之间随时间相互交互的动态信息。
What happens when you invoke the “wash clothes” use case?

  1. Soaking(浸泡),水通过水龙头进入滚筒(drum)
  2. 滚筒静止五分钟
  3. 浸泡结束后,水停止进入滚筒
  4. washing,滚筒转动15分钟
  5. washing结束后,滚筒排出水
  6. 滚筒停止转动
  7. Rinsing(冲洗),水重新进入滚筒;
  8. 滚筒再次转动
  9. 15分钟后水停止进入
  10. 冲洗结束,滚筒停止转动并排出水
  11. spinning(甩干),滚动转动五分钟
  12. 滚筒停止
  13. 洗衣结束
    通过上述过程,我们提取出三个类:timer、water pip、drum。它们都有各自的动作:

timer:

  • time the soaking
  • time the washing
  • time the rinsing
  • time the spinning

water pip

  • start a flow
  • stop a flow

drum 滚筒

  • store water
  • rotate back and force
  • rotate clockwise
  • stop rotate
  • pump water

最终得出时序图如下:
【UML】《Theach yourself uml in 24hours》——hour1
(6)Activity diagram 动作图
Activity diagram是发生在一个用例或实例内部的活动描述
【UML】《Theach yourself uml in 24hours》——hour1
(7)Communication diagram 通信图
目的与时序图类似,但是不同的是其不基于时间,而只是展示信息交流过程。
【UML】《Theach yourself uml in 24hours》——hour1

(8)Component diagram
现代软件的开发都是基于组件的,组件component的表示方法有两种:
uml1.x:
【UML】《Theach yourself uml in 24hours》——hour1

uml2.0
【UML】《Theach yourself uml in 24hours》——hour1
(9)Deployment daigram 部署图
用于描述基于计算机系统的结构信息
【UML】《Theach yourself uml in 24hours》——hour1
(10)其它
1. Notes
【UML】《Theach yourself uml in 24hours》——hour1
2. keywords and stereotypes
uml虽然提供了很多item,但是用户总是会有新的需求,而keywords and stereotypes可以用于创建新的item,比如interface,其实和class差不多,都有属性和方法,所以没必要创建一个新的icon,通过keywords and stereotypes描述如下图:
【UML】《Theach yourself uml in 24hours》——hour1
3. uml2.0中新增加的图

  • composite structure diagram
    【UML】《Theach yourself uml in 24hours》——hour1
  • Interaction Overview diagram
    【UML】《Theach yourself uml in 24hours》——hour1
  • Timing diagram
    时序图无法描述时间长短信息(duration),但是timing diagram可以。
    【UML】《Theach yourself uml in 24hours》——hour1
  • Package diagram
    uml1.x和uml2.0的包图发生了变化
    【UML】《Theach yourself uml in 24hours》——hour1

3、为什么UML有如此多的图?

因为通常一个系统有很多相关者(stakeholder),不同的利益相关者会从不同的角度看系统,比如上面的洗衣机,设计者和说明书撰写者的角度肯定不同.
Conscientious system design involves all the possible viewpoints,and each UML diagram gives you a way of incorporating a particular view.The objective is to communicate clearly with every type of stakeholder.
认真负责的系统设计涉及到所有可能的观点,并且每个UML图都提供了一种结合特定视图的方法视图。该目标是与每种类型的利益相关者进行清晰的沟通。