架构师进阶必备技能
定义
UML是一种开放的方法,用于说明、可视化、构建和编写一个正在开发的、面向对象的、软件密集系统的制品的开放方法。
模型
- 功能模型:从用户的角度展示系统的功能,包括用例图。
- 对象模型:采用对象,属性,操作,关联等概念展示系统的结构和基础,包括类别图、对象图。
- 动态模型:展现系统的内部行为。包括序列图,活动图,状态图。
教程
实现UML的工具有很多,首先排除所有绘图软件,因为这些软件绘制成的图片无法用GIT来进行版本控制,也很难集成在markdown里。我选择PlantUML(类似于HTML的标记性语言)来实现UML,采用gravizo来渲染PlantUML,可集成在markdown。
比如:

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

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

- 用例图
参与者与用例的交互。下图是饭店的用例图。
left to right direction;
skinparam packageStyle rect;
actor customer;
actor chef;
rectangle restaurant{
customer -> (eat food);
customer -> (pay for food);
chef -> (cook food);
}
;
customer -> (pay for food);
chef -> (cook food);
}
)
- 活动图
我一直是把活动图当流程图来用,描述程序的处理过程。下图描述的是一个经典的程序员笑话。
(*) --> "buy 10 apples";
if "is there watermelon " then;
-->[true] "buy a apple";
-right-> (*);
else;
->[false] "Something else";
-->(*);
endif;
 --> "buy 10 apples";
if "is there watermelon " then;
-->[true] "buy a apple";
-right-> ();
else;
->[false] "Something else";
-->(*);
endif;
)
- 组件图
表示组件是如何互相组织以构建更大的组件或是软件系统。下图是Web项目的组件图。
HTTP - [web server];
[web server] - [app server];
database "mysql" {;
[database];
};
[app server] - [database];

- 状态图
描述一个对象在其生存期间的动态行为。下图是线程的状态图。
[*] -> 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;

-
类图
用来描述类与类之间的关系。 -
访问权限控制
```
class Dummy {
- private field1
# protected field2
~ package method1()
+ public method2()
}
```

-
类与类之间的关系
-
继承
```
Father <|-- Son
```

1. 实现
```
abstract class AbstractList
interface List
List <|.. AbstractList
```

1. 依赖
一个类A使用到了另一个类B,而这种使用关系是具有偶然性的、临时性的、非常弱的,表现在代码层面,为类B作为参数被类A在某个method中使用,例如人和烟草的关系。
```
Human ..> Cigarette
```

1. 关联
强依赖关系,表现在代码层面,为被关联类B以类属性的形式出现在关联类A中。
```
class Water
class Human
Human --> Water
```

1. 聚合
关联关系的一种特例,他体现的是整体与部分、拥有的关系,即has-a的关系,此时整体与部分之间是可分离的,他们可以具有各自的生命周期。
```
Company o-- Human
```

1. 组合
关联关系的一种特例,他体现的是一种contains-a的关系,这种关系比聚合更强,也称为强聚合;他同样体现整体与部分间的关系,但此时整体与部分是不可分的,整体的生命周期结束也就意味着部分的生命周期结束。
```
Human *-- Brain
```

网友评论
a -> b: hello;
b -> a: hi;
)
这段代码写入markdown中, 并不能转出图片, 但是 写入浏览器的url中可以。 是什么原因呢。
这是用百度云分享的图片:https://pan.baidu.com/s/1boNhK7t
a -> b: hello;
b -> a: hi;
)