UML语言的案例——基于plantuml

UML(Unified modeling language) 是统一建模语言或者标准建模语言。通过使用 UML 的面向对象图的方式,可以更明确、清晰的表达项目中的架设思想、项目结构、执行顺序等一些逻辑思维。

本文基于plantuml阐述了uml语言的典型图。PlantUML语言简单直接易学,就好像Markdown语法一样,简单高效。

plantuml 具有完善的支持网站 https://plantuml.com/zh/。

拥有一个在线编辑工具:http://www.plantuml.com/plantuml/uml/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000

时序图

1.简单图

你可以用->来绘制参与者之间传递的消息, 而不必显式地声明参与者。
你也可以使用-->绘制一个虚线箭头。另外,还能用 <-<--,这不影响绘图,但可以提高可读性。 注意:仅适用于时序图,对于其它示意图,规则是不同的。

sequenceDiagram
@startuml
用户 ->> 认证中心: 登录操作
认证中心 ->> 缓存: 存放(key=token+ip,value=token)token

用户 <- 认证中心 : 认证成功返回token
用户 ->> 认证中心: 下次访问头部携带token认证
认证中心 <- 缓存: key=token+ip获取token
其他服务 <- 认证中心: 存在且校验成功则跳转到用户请求的其他服务
其他服务 ->> 用户: 信息
@enduml

简单时序图

2.时序图

下图展示了生命线的激活与撤销。关键字activatedeactivate用来表示参与者的生命活动。

一旦参与者被激活,它的生命线就会显示出来。activatedeactivate适用于以上情形,destroy表示一个参与者的生命线的终结。

@startuml
participant User

User -> A: DoWork
activate A #FFBBBB

A -> A: Internal call
activate A #DarkSalmon

A -> B: << createRequest >>
activate B

B --> A: RequestCreated
deactivate B
deactivate A
A -> User: Done
deactivate A

@enduml

具有生命周期的参与者

用例图

1.用例

用例用圆括号括起来。也可以用关键字usecase来定义用例。还可以用关键字as定义一个别名,这个别名可以在以后定义关系的时候使用。

2.角色

角色用两个冒号包裹起来,也可以用actor关键字来定义角色。

@startuml
skinparam actorStyle awesome
:User: --> (Use)
"Main Admin" as Admin
"Use the application" as (Use)
Admin --> (Admin the application)
@enduml

角色图

3.打包图
@startuml
left to right direction
actor Guest as g
package Professional {
  actor Chef as c
  actor "Food Critic" as fc
}
package Restaurant {
  usecase "Eat Food" as UC1
  usecase "Pay for Food" as UC2
  usecase "Drink" as UC3
  usecase "Review" as UC4
}
fc --> UC4
g --> UC1
g --> UC2
g --> UC3
@enduml

打包图

4.完整例子

箭头
用箭头-->连接角色和用例。横杠-越多,箭头越长。 通过在箭头定义的后面加一个冒号及文字的方式来添加标签。

继承
如果一个角色或者用例继承于另一个,那么可以用–>符号表示。

使用注释
可以用note left of , note right of , note top of , note bottom of等关键字给一个对象添加注释。注释还可以通过note关键字来定义,然后用..连接其他对象。

@startuml
left to right direction
skinparam packageStyle rectangle
actor customer
actor clerk
rectangle checkout {
  customer -- (checkout)
  (checkout) .> (payment) : include
  (help) .> (checkout) : extends
  (checkout) -- clerk
}
@enduml

参与者

类图

1.类图元素
@startuml
abstract        abstract
abstract class  "abstract class"
annotation      annotation
circle          circle
()              circle_short_form
class           class
diamond         diamond
<>              diamond_short_form
entity          entity
enum            enum
interface       interface
@enduml

类图的各种组件

2.类之间的关系

类之间的关系通过以下符号定义:

TypeSymbolDrawing
Extension(扩展)<|–
Composition(组合)*–
Aggregation(聚合)o–
@startuml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml

各种箭头示意图

2.示例

为了生命对象属性或者方法,可以使用字段名或者方法名。系统通过是否有括号来判断是方法还是字段。使用{}把字段或者方法括起来。

@startuml
left to right direction
class Dummy {
  String data
  flightNumber : Integer
  Integer function()
}

class Flight {
   flightNumber : Integer
   departureTime : Date
   Integer yyy(Date)
}

Flight <-- Dummy
@enduml

3.定义可访问性

一旦定义了域或者方法,可以定义相应条目的可访问性质。

CharacterIcon for fieldIcon for methodVisibility
-private
#protected
~package private
+public
@startuml

class Dummy {
 -field1
 #field2
 ~method1()
 +method2()
}

@enduml

类图

活动图

1.简单活动

使用(*)作为活动图的开始点和结束点。有时可用(*top)强制开始点位于图示的顶端。

@startuml

(*) -up-> "First Activity"
-right-> "Second Activity"
--> "Third Activity"
-left-> (*)

@enduml

活动图

2.分支活动

可以使用关键字if/then/else创建分支。

@startuml
(*) --> "Initialization"

if "Some Test" then
  -->[true] "Some Activity"
  --> "Another activity"
  -right-> (*)
else
  ->[false] "Something else"
  -->[Ending process] (*)
endif

@enduml

分支活动图

3.新特性
@startuml
start
if (condition A) then (yes)
  :Text 1;
elseif (condition B) then (yes)
  :Text 2;
  stop
elseif (condition C) then (yes)
  :Text 3;
elseif (condition D) then (yes)
  :Text 4;
else (nothing)
  :Text else;
endif
stop
@enduml

分支

组件图

组件

必须用中括号括起来。还可以使用关键字component定义一个组件。并且可以用关键字as给组件定义一个别名

@startuml

[First component]
[Another component] as Comp2
component Comp3
component [Last\ncomponent] as Comp4

@enduml

组件图

状态图

简单图
@startuml

[*] --> State1
State1 --> [*]
State1 : this is a string
State1 : this is another string

State1 -> State2
State2 --> [*]

@enduml

简单状态图

部署图

定时图

@startuml
scale 5 as 150 pixels

clock clk with period 1
binary "enable" as en
binary "R/W" as rw
binary "data Valid" as dv
concise "dataBus" as db
concise "address bus" as addr

@6 as :write_beg
@10 as :write_end

@15 as :read_beg
@19 as :read_end


@0
en is low
db is "0x0"
addr is "0x03f"
rw is low
dv is 0

@:write_beg-3
 en is high
@:write_beg-2
 db is "0xDEADBEEF"
@:write_beg-1
dv is 1
@:write_beg
rw is high


@:write_end
rw is low
dv is low
@:write_end+1
rw is low
db is "0x0"
addr is "0x23"

@12
dv is high
@13
db is "0xFFFF"

@20
en is low
dv is low
@21
db is "0x0"

highlight :write_beg to :write_end #Gold:Write
highlight :read_beg to :read_end #lightBlue:Read

db@:write_beg-1 <-> @:write_end : setup time
db@:write_beg-1 -> addr@:write_end+1 : hold
@enduml

定时图

Network diagram

待补充。

Salt

Salt 是 PlantUML 下面的子项目用来帮助用户来设计图形接口.

@startsalt
{
  Just plain text
  [This is my button]
  ()  Unchecked radio
  (X) Checked radio
  []  Unchecked box
  [X] Checked box
  "Enter text here   "
  ^This is a droplist^
}
@endsalt

接口图

@startsalt
{
{T
 + World
 ++ America
 +++ Canada
 +++ USA
 ++++ New York
 ++++ Boston
 +++ Mexico
 ++ Europe
 +++ Italy
 +++ Germany
 ++++ Berlin
 ++ Africa
}
}
@endsalt

分级图

Gantt Diagram

@startgantt
[Prototype design] lasts 15 days
[Test prototype] lasts 10 days
-- All example --
[Task 1 (1 day)] lasts 1 day
[T2 (5 days)] lasts 5 days
[T3 (1 week)] lasts 1 week
[T4 (1 week and 4 days)] lasts 1 week and 4 days
[T5 (2 weeks)] lasts 2 weeks
@endgantt

甘特图

思维导图

同时兼容OrgMode语法。

@startmindmap
* Debian
** Ubuntu
*** Linux Mint
*** Kubuntu
*** Lubuntu
*** KDE Neon
** LMDE
** SolydXK
** SteamOS
** Raspbian with a very long name
*** <s>Raspmbc</s> => OSMC
*** <s>Raspyfi</s> => Volumio
@endmindmap

思维导图

Work Breakdown Structure (WBS)

略,待补充。


更多内容见uml官网。

如果您觉得上面的内容对您有帮助欢迎点赞、评论、转发!

更多内容请查阅作者博客:https://jiaruiblog.com
或者star作者github: https://github.com/Jarrettluo?tab=repositories

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值