• 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
    extends this one.
    
    note "This is a floating note" as N1
    note "This note is connected
    to 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
    

  • 相关阅读:
    艾伟也谈项目管理,项目经理成长日记(6)——对不上的帐 狼人:
    艾伟也谈项目管理,Google的产品质量之道 狼人:
    艾伟也谈项目管理,项目管理 – 人员外购利弊谈 狼人:
    艾伟也谈项目管理,项目经理成长日记(7)——说是细,做的粗 狼人:
    艾伟也谈项目管理,谈软件协作:君子和而不同,小人同而不和 狼人:
    艾伟也谈项目管理,工作感言:任务分配及管理 狼人:
    艾伟也谈项目管理,敏捷教练的工具箱 狼人:
    《青春依然,再见理想——献给学弟学妹》大四学生的万字忏悔书,警示学弟学妹
    errno含义
    使用索引统计信息(Index Statistics)优化查询语句,提高查询效率
  • 原文地址:https://www.cnblogs.com/Jeson2016/p/6837017.html
Copyright © 2020-2023  润新知