PlantUML类图

PlantUML类图

 
雨客  2016-04-08 11:38:03 浏览796 评论0

摘要: 类之间的关系 PlantUML用下面的符号来表示类之间的关系: 泛化,Generalization:<|-- 关联,Association:<-- 组合,Composition:*-- 聚合,Aggregation:o-- 实现,Realization:<|.. 依赖,Dependency:<.. 以上是常见的六种关系,--可以替换成..就可以得到虚线。

类之间的关系

PlantUML用下面的符号来表示类之间的关系:

  • 泛化,Generalization<|--
  • 关联,Association<--
  • 组合,Composition*--
  • 聚合,Aggregationo--
  • 实现,Realization<|..
  • 依赖,Dependency<..

以上是常见的六种关系,--可以替换成..就可以得到虚线。另外,其中的符号是可以改变方向的,例如:<|--表示右边的类泛化左边的类;--|>表示左边的类泛化右边的类。

例如,下面的是--

@startuml

Class01 <|-- Class02:泛化
Class03 <-- Class04:关联
Class05 *-- Class06:组合
Class07 o-- Class08:聚合
Class09 -- Class10

@enduml

生成的类图如下:

--可以替换成..,对应的虚线:

@startuml

Class11 <|.. Class12:实现
Class13 <.. Class14:依赖
Class15 *.. Class16
Class17 o.. Class18
Class19 .. Class20

@enduml

生成的类图如下:

关系上的标签

可以在关系上添加标签,只需要在文本后面添加冒号和标签名称即可。可以在关联的两边使用双引号。例如:

@startuml

Class01 "1" *-- "many" Class02 : contains Class03 o-- Class04 : aggregation Class05 --> "1" Class06 @enduml 

生成的类图如下:

你可以在关系上使用<或者>表名两个类之间的关系,例如:

@startuml

class Car

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

生成的类图如下:

上面的类图意思是:

  • Driver 驾驶 Car
  • Car 有4个 Wheel
  • Person 拥有 Car

添加方法

在类名后面添加冒号可以添加方法和方法的参数,例如:

@startuml

Object <|-- ArrayList

Object : equals() ArrayList : Object[] elementData ArrayList : size() @enduml 

生成的类图如下:

也可以使用{}来定义所有的字段及字段和方法,例如:

@startuml
class Dummy {
  String data void methods() } class Flight { flightNumber : Integer departureTime : Date } @enduml 

生成的类图如下:

定义可见性

以下符号定义字段或者方法的可见性:

  • -private
  • #protected
  • ~package private
  • +public

例如:

@startuml

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

你可以使用skinparam classAttributeIconSize 0关掉icon的显示:

@startuml
skinparam classAttributeIconSize 0
class Dummy { -field1 #field2 ~method1() +method2() } @enduml 

抽象和静态

你可以使用{static}或者{abstract}来修饰字段或者方法,修饰符需要在行的开头或者末尾使用。你也可以使用{classifier}代替{static}

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

类主体

默认的,字段和方法是由PlantUML自动分组的,你也可以使用: -- .. == __这些分隔符手动进行分组。

@startuml
class Foo1 {
  You can use several lines .. as you want and group == things together. __ You can have as many groups as you want -- End of classes } class User { .. Simple Getter .. + getName() + getAddress() .. Some setter .. + setName() __ private data __ int age -- encrypted -- String password } @enduml 

注释和原型

原型使用class<<>>进行定义。

注释使用note left ofnote right ofnote top ofnote bottom of关键字进行定义。

你也可以在最后一个定义的类上使用note leftnote rightnote topnote bottom关键字。

注释可以使用..与其他对象进行连接。

@startuml
class Object << general >>
Object <|--- ArrayList

note top of Object : In java, every class\nextends this one.

note "This is a floating note" as N1
note "This note is connected\nto several objects." as N2
Object .. N2
N2 .. ArrayList

class Foo
note left: On last defined class

@enduml

注释的其他特性

注释可以使用一些html标签进行修饰:

  • <b>
  • <u>
  • <i>
  • <s><del><strike>
  • <font color="#AAAAAA"> 或者 <font color="colorName">
  • <color:#AAAAAA> 或者 <color:colorName>
  • <size:nn> 该表font大小
  • <img src="file"> 或者 <img:file>,文件必须是可以访问的。
@startuml

class Foo
note left: On last defined class

note top of Object
  In java, <size:18>every</size> <u>class</u>
  <b>extends</b>
  <i>this</i> one.
end note

note as N1
  This note is <u>also</u>
  <b><color:royalBlue>on several</color>
  <s>words</s> lines
  And this is hosted by <img:sourceforge.jpg>
end note

@enduml

连接上的注释

可以在连接上定义注释,只需要使用note on link,你可以使用note left on linknote right on linknote top on linknote bottom on link来改变注释的位置。

@startuml

class Dummy
Dummy --> Foo : A link
note on link #red: note that is red

Dummy --> Foo2 : Another link
note right on link #blue
    this is my note on right link
    and in blue
end note

@enduml

抽象类和接口

可以使用abstract或者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 

使用非字母

类名可以使用非字母的方式显示:

@startuml
class "This is my class" as class1
class class2 as "It works this way too"

class2 *-- "foo/dummy" : use
@enduml

隐藏字段和方法

@startuml

class Dummy1 {
  +myMethods() } class Dummy2 { +hiddenMethod() } class Dummy3 <<Serializable>> { String name } hide members hide <<Serializable>> circle show Dummy1 methods show <<Serializable>> fields @enduml 

隐藏类

@startuml

class Foo1
class Foo2 Foo2 *-- Foo1 hide Foo2 @enduml 

使用泛型

@startuml

class Foo<? extends Element> { int size() } Foo *- Element @enduml 

@startuml

package "Classic Collections" #yellow{ Object <|-- ArrayList } package net.sourceforge.plantuml { Object <|-- Demo1 Demo1 *- Demo2 } @enduml 

包可以设置样式,也可以使用skinparam packageStyle设置为默认样式。

@startuml
scale 750 width
package foo1 <<Node>> { class Class1 } package foo2 <<Rect>> { class Class2 } package foo3 <<Folder>> { class Class3 } package foo4 <<Frame>> { class Class4 } package foo5 <<Cloud>> { class Class5 } package foo6 <<Database>> { class Class6 } @enduml 

也可以在包之间设置联系:

@startuml

skinparam packageStyle rect

package foo1.foo2 { } package foo1.foo2.foo3 { class Object } foo1.foo2 +-- foo1.foo2.foo3 @enduml 

命名空间

命名空间内使用默认的类,需要在类名前加上

@startuml

class BaseClass

namespace net.dummy #DDDDDD { .BaseClass <|-- Person Meeting o-- Person .BaseClass <|- Meeting } namespace net.foo { net.dummy.Person <|- Person .BaseClass <|-- Person net.dummy.Meeting o-- Person } BaseClass <|-- net.unused.Person @enduml 

命名空间可以自动设置,通过set namespaceSeparator ???设置命名空间分隔符,使用set namespaceSeparator none可以关闭自动设置命名空间。

@startuml

set namespaceSeparator ::
class X1::X2::foo { some info } @enduml 

改变箭头方向

@startuml
Room o- Student Room *-- Chair @enduml 

换个方向:

@startuml
Student -o Room Chair --* Room @enduml 

也可以在箭头上使用left, right, up or down关键字:

@startuml
foo -left-> dummyLeft foo -right-> dummyRight foo -up-> dummyUp foo -down-> dummyDown @enduml 

这些关键字可以使用开头的几个字符简写,例如,使用-d-代替-down-

标题

使用title或者title end title

@startuml

title Simple <b>example</b>\nof title Object <|-- ArrayList @enduml 

设置Legend

@startuml

Object <|- ArrayList

legend right <b>Object</b> and <b>ArrayList</b> are simple class endlegend @enduml 

关联类

一个类和两个类有关联时设置关系:

@startuml
class Student {
  Name } Student "0..*" - "1..*" Course (Student, Course) .. Enrollment class Enrollment { drop() cancel() } @enduml 

换一个方向:

@startuml
class Student {
  Name } Student "0..*" -- "1..*" Course (Student, Course) . Enrollment class Enrollment { drop() cancel() } @enduml 

其他

还有一些特性,如设置皮肤参数、颜色、拆分大文件等等,请参考官方文档

例子

一个完整的例子:

@startuml 

title class-diagram.png
scale 1.5
/'组合关系(composition)'/
class Human {
    - Head mHead;
    - Heart mHeart;
    ..
    - CreditCard mCard;
    --
    + void travel(Vehicle vehicle);
}

Human *-up- Head : contains >
Human *-up- Heart : contains >

/'聚合关系(aggregation)'/
Human o-left- CreditCard : owns >

/'依赖关系(dependency)'/
Human .down.> Vehicle : dependent

/'关联关系(association'/
Human -down-> Company : associate

/'继承关系(extention)'/
interface IProgram {
    + void program();
}
class Programmer {
    + void program();
}
Programmer -left-|> Human : extend
Programmer .up.|> IProgram : implement
@enduml

下面例子,参考自Class Diagram

Java集合类图

@startuml
abstract class AbstractList
abstract AbstractCollection interface List interface Collection List <|-- AbstractList Collection <|-- AbstractCollection Collection <|- List AbstractCollection <|- AbstractList AbstractList <|-- ArrayList ArrayList : Object[] elementData ArrayList : size() enum TimeUnit TimeUnit : DAYS TimeUnit : HOURS TimeUnit : MINUTES @enduml 

类和接口

@startuml
Object -- AbstractList
  
class ArrayList extends Object { int size } interface List extends Collection { add() } interface Set extends Collection class TreeSet implements SortedSet @enduml 

Repository接口

@startuml
package framework <<Folder>>{ interface BasicRepository { E find(Object pk); List<E> findAll(); void save(E entity); void update(E entity); void remove(E entity); } class AbstractHibernateRepository << @Repository >> { -EntityManager entityManager; } } interface PartnerRepository { List<PartnerEntity> findByFoo(...); List<PartnerEntity> search(String pattern, int maxResult); } class HibernatePartnerRepository << @Repository >> { } class InMemoryPartnerRepository { } BasicRepository <|.. PartnerRepository BasicRepository <|.. AbstractHibernateRepository AbstractHibernateRepository <|-- HibernatePartnerRepository PartnerRepository <|.. HibernatePartnerRepository PartnerRepository <|.. InMemoryPartnerRepository @enduml 

Java异常层次

@startuml

namespace java.lang #DDDDDD { class Error << unchecked >> Throwable <|-- Error Throwable <|-- Exception Exception <|-- CloneNotSupportedException Exception <|-- RuntimeException RuntimeException <|-- ArithmeticException RuntimeException <|-- ClassCastException RuntimeException <|-- IllegalArgumentException RuntimeException <|-- IllegalStateException Exception <|-- ReflectiveOperationException ReflectiveOperationException <|-- ClassNotFoundException } namespace java.io #DDDDDD { java.lang.Exception <|-- IOException IOException <|-- EOFException IOException <|-- FileNotFoundException } namespace java.net #DDDDDD { java.io.IOException <|-- MalformedURLException java.io.IOException <|-- UnknownHostException } @enduml 

转载于:https://www.cnblogs.com/Jeson2016/p/6837017.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值