PlantUML参考手册


title: PlantUML参考手册
date: 2020-03-03 15:37:35
tags:
- UML
- 手册
categories:
- 手册
- UML


PlantUML参考手册

前言

UML中的关系主要分为六种:

  • 关联(association)
  • 聚合(aggregation)
  • 组合(composition)
  • 依赖(dependency)/使用
  • 泛化(generalization)/继承(inheritance)
  • 实现(realization)
关系 PlantUML中的表示方法 样式
依赖关系 <…
关联关系 <--
聚合关系 o--
组合关系 *--
实现关系 <|…
泛化/继承关系 <|--

依赖关系

简单理解就是在某个方法里通过传参、返回值、成员变量、局部变量的方式用到了某个类

Java表达方式

class ImageLoader{
    loadImage(ImageInfo info)
}

class ImageInfo{
}

UML类图表达方式

使用UML类图表示该关系:用实心箭头+虚线表示

PlantUML代码表达方式

//ImageLoader 依赖/使用 
PlantUML中的表示方法:ImageInfo <… ImageLoader :依赖关系

关联关系

简单理解就是类里面作为属性引用了另一个类

Java表达方式

class Window {
	public WindowManager mWindowManager
}

class WindowManager{
}

UML类图表达方式

使用UML类图表示该关系:用实心箭头+实线表示

PlantUML代码表达方式

PlantUML中的表示方法:WindowManager <-- Window:关联关系

聚合关系

表示整体和部分,一个类属性使用列表保存很多个另一个类对象(可通过List、set、数组等等集合)

Java表达方式

class Company{
	List<Department> mDepartments
}
class Department{
}

UML类图表达方式

使用UML类图表示该关系:用空心菱形+实线表示

PlantUML代码表达方式

PlantUML中的表示方法:Company o-- Department:聚合关系

组合关系

相比聚合更加紧密的关系,生死与共,不能单独存在

Java表达方式

class Bird{//小鸟
	Wing mWing;//翅膀
}
class Wing{//翅膀
}

UML类图表达方式

使用UML类图表示该关系:用实心菱形+实线表示

PlantUML代码表达方式

PlantUML中的表示方法:Bird *-- Wing:组合关系

泛化/继承关系

用来表示类之间的继承关系

Java表达方式

abstract class Factory{
 	abstract makeProduct()
}
class PhoneFactory extends Factory{
}
class CarFactory extends Factory{
}

UML类图表达方式

使用UML类图表示该关系:用空心箭头+实线表示

PlantUML代码表达方式

PlantUML中的表示方法:Factory <|-- PhoneFactory:泛化/继承关系

实现关系

用来表示一个类实现某个一个接口

Java表达方式

interface IColorDraw{
    draw()
}
class RedColorDraw{
    draw()
}
class BlueColorDraw{
    draw()
}

UML类图表达方式

使用UML类图表示该关系:用空心箭头+虚线表示

PlantUML代码表达方式

PlantUML中的表示方法:IColorDraw <|… RedColorDraw:实现关系

完整的PlantUML表示

UML类图

PlantUML代码

@startuml
class ImageLoader{
    loadImage(ImageInfo)
}

class ImageInfo{
}

ImageInfo <.. ImageLoader :依赖关系

class Window {
-WindowManager mWindowManager
}

class WindowManager

WindowManager <-- Window:关联关系

class Company
class Department
Company o-- Department:聚合关系

class Bird
class Wing
Bird *-- Wing:组合关系

abstract class Factory
class PhoneFactory
class CarFactory
Factory <|-- PhoneFactory:泛化关系
Factory <|-- CarFactory:也就是继承关系

interface IColorDraw{
    draw()
}
class RedColorDraw{
    draw()
}
class BlueColorDraw{
    draw()
}
IColorDraw <|.. RedColorDraw:实现关系
IColorDraw <|.. BlueColorDraw:实现某个接口
@enduml

关系上的标识

  1. 在关系之间使用标签来说明时, 使用 :后接 标签文择本地图片字。对元素的说明,你可以在每一边使用 "" 来说明.

    @startuml
    
    Class01 "1" *-- "many" Class02 : contains
    Class03 o-- Class04 : aggregation
    Class05 --> "1" Class06
    
    @enduml
    
  2. 在标签的开始或结束位置添加<>以表明是哪个对象作用到哪个对象上。

    @startuml
    class Car
    
    Driver - Car : drives >
    Car *- Wheel : have 4 >
    Car -- Person : < owns
    
    @enduml
    

定义可访问性

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

表示方式 描述属性时的样式 描述方法时的样式 代表的访问标识符
- img img private
# img img protected
~ img img package private
+ img img public

例:

@startuml

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

@enduml

抽象与静态

通过修饰符{static}或者{abstract},可以定义静态或者抽象的方法或者属性。这些修饰符可以写在行的开始或者结束。也可以使用{classifier}这个修饰符来代替{static}.

@startuml
class Dummy {
  {static} String id
  {abstract} void methods()
}
@enduml

备注和模板

模板通过类关键字(“<<”和”>>”)来定义你可以使用note left of , note right of , note top of , note bottom of这些关键字来添加备注。你还可以在类的声明末尾使用note left, note right,note top, note bottom来添加备注。此外,单独用note这个关键字也是可以的,使用 .. 符号可以作出一条连接它与其它对象的虚线。

@startuml
class Object << general >>  /'<<模板内容 >>'/
Object <|--- ArrayList

/'方式1:note top of 名称 : 内容....'/
note top of Object : In java, every class
extends this one.
/'声明一个备注格式:
    note "内容" as 备注名称
'/
note "This is a floating note" as N1
note "This note is connected
to several objects." as N2
/'方式2:名称 .. 备注名称'/
Object .. N2
N2 .. ArrayList

/'方式3: 在声明类下一行用  note left: 内容....
        left 表示标签显示在左侧
        right 表示显示在右侧
'/
class Foo
note left: On last defined class

@enduml

抽象类和接口

用关键字abstractabstract class来定义抽象类。抽象类用斜体显示。 也可以使用interface, annotationenum关键字。

@startuml

abstract class AbstractList   /'定义抽象类'/
abstract AbstractCollection
interface List   /'定义接口'/
interface Collection

List <|-- AbstractList
Collection <|-- AbstractCollection

Collection <|- List
AbstractCollection <|- AbstractList
AbstractList <|-- ArrayList

class ArrayList {
  Object[] elementData
  size()
}
/'定义枚举'/
enum TimeUnit {
  DAYS
  HOURS
  MINUTES
}

annotation SuppressWarnings

@enduml
原文地址:https://www.cnblogs.com/mikisakura/p/12978510.html