unity 学习软件工程技术干货

21分钟入门UML

2015-11-24  本文已影响16145人  MountainKing

架构师进阶必备技能

定义

UML是一种开放的方法,用于说明、可视化、构建和编写一个正在开发的、面向对象的、软件密集系统的制品的开放方法。

模型

  1. 功能模型:从用户的角度展示系统的功能,包括用例图。
  2. 对象模型:采用对象,属性,操作,关联等概念展示系统的结构和基础,包括类别图、对象图。
  3. 动态模型:展现系统的内部行为。包括序列图,活动图,状态图。

教程

实现UML的工具有很多,首先排除所有绘图软件,因为这些软件绘制成的图片无法用GIT来进行版本控制,也很难集成在markdown里。我选择PlantUML(类似于HTML的标记性语言)来实现UML,采用gravizo来渲染PlantUML,可集成在markdown。

比如:

![Alt text](http://g.gravizo.com/g?
    a -> b: hello;
    b -> a: hi;
)

将上面的代码复制到md文件,就可以生成下面这张图片。

![Alt text](http://g.gravizo.com/g?
a -> b: hello;
b -> a: hi;
)

  1. 时序图
    通过描述对象之间发送消息的时间顺序显示多个对象之间的动态协作。
    ->表示消息传递,-->表示异步消息传递,note [left | right]对消息进行说明。
a --> b: how are you;
note right: greeting;
a -> a: i am thinking;
b -> a: fine;

![Alt text](http://g.gravizo.com/g?
a --> b: how are you;
note right: greeting;
a -> a: talk to myself;
b -> a: fine;
)

  1. 用例图
    参与者与用例的交互。下图是饭店的用例图。
left to right direction;
skinparam packageStyle rect;
actor customer;
actor chef;
rectangle restaurant{
customer -> (eat food);
customer -> (pay for food);
chef -> (cook food);
}

![Alt text](http://g.gravizo.com/g?
left to right direction;
skinparam packageStyle rect;
actor customer;
actor chef;
rectangle restaurant{
customer -> (eat food);
customer -> (pay for food);
chef -> (cook food);
}
)

  1. 活动图
    我一直是把活动图当流程图来用,描述程序的处理过程。下图描述的是一个经典的程序员笑话。
(*) --> "buy 10 apples";
if "is there watermelon " then;
-->[true] "buy a apple";
-right-> (*);
else;
->[false] "Something else";
-->(*);
endif;

![Alt text](http://g.gravizo.com/g?
() --> "buy 10 apples";
if "is there watermelon " then;
-->[true] "buy a apple";
-right-> (
);
else;
->[false] "Something else";
-->(*);
endif;
)

  1. 组件图
    表示组件是如何互相组织以构建更大的组件或是软件系统。下图是Web项目的组件图。
HTTP - [web server];
[web server] - [app server];
database "mysql" {;
[database];
};
[app server] - [database];

![Alt text](http://g.gravizo.com/g?
HTTP - [web server];
[web server] - [app server];
database "mysql" {;
[database];
};
[app server] - [database];
)

  1. 状态图
    描述一个对象在其生存期间的动态行为。下图是线程的状态图。
[*] -> ready : start;
ready -> running : get cpu;
running -> ready : lost cpu;
running -down-> block : io, sleep, locked;
block -up-> ready : io return, sleep over, get lock;
running -> [*] : complete;

![Alt text](http://g.gravizo.com/g?
[] -> ready : start;
ready -> running : get cpu;
running -> ready : lost cpu;
running -down-> block : io, sleep, locked;
block -up-> ready : io return, sleep over, get lock;
running -> [
] : complete;
)

  1. 类图
    用来描述类与类之间的关系。

  2. 访问权限控制

```
class Dummy {
- private field1
# protected field2
~ package method1()
+ public method2()
}
```

![Alt text](http:https://img.haomeiwen.com/i637398/50e8c74f441b145f?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  1. 类与类之间的关系

  2. 继承

  ```
  Father <|-- Son
  ```
  ![Alt text](http://upload-images.jianshu.io/upload_images/637398-1b39c44083fb427d?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

1. 实现
  ```
  abstract class AbstractList
  interface List
  List <|.. AbstractList
  ```
  ![Alt text](http:https://img.haomeiwen.com/i637398/e1cab0e4f0303ff8?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

 1. 依赖
   一个类A使用到了另一个类B,而这种使用关系是具有偶然性的、临时性的、非常弱的,表现在代码层面,为类B作为参数被类A在某个method中使用,例如人和烟草的关系。
   ```
   Human ..> Cigarette
   ```
   ![Alt text](http://upload-images.jianshu.io/upload_images/637398-b23a192499e6eafa?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

 1. 关联
   强依赖关系,表现在代码层面,为被关联类B以类属性的形式出现在关联类A中。
   ```
   class Water
   class Human
   Human --> Water
   ```
   ![Alt text](http:https://img.haomeiwen.com/i637398/f3258bc89d5de6f0?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

 1. 聚合
   关联关系的一种特例,他体现的是整体与部分、拥有的关系,即has-a的关系,此时整体与部分之间是可分离的,他们可以具有各自的生命周期。
   ```
   Company o-- Human
   ```
   ![Alt text](http://upload-images.jianshu.io/upload_images/637398-00b9d2ab5efb86bf?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1. 组合
    关联关系的一种特例,他体现的是一种contains-a的关系,这种关系比聚合更强,也称为强聚合;他同样体现整体与部分间的关系,但此时整体与部分是不可分的,整体的生命周期结束也就意味着部分的生命周期结束。
```
Human *-- Brain
```
![Alt text](http://upload-images.jianshu.io/upload_images/637398-4a767ca51f40b8e1?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上一篇下一篇

猜你喜欢

热点阅读