PlantUML 之类图

类图介绍

今晚上借着燥热的天气学习下UML图的使用,然后开始连续3篇,时序图、活动图、类图的学习记录,为对项目开发中混沌的业务逻辑还以清晰.

UML Sequence
UML Class
UML Activity

这里写图片描述

@startuml
title  decoupling
'skinparam packageStyle rect/' 加入这行代码,样式纯矩形'/
skinparam backgroundColor #EEEBDC
skinparam roundcorner 20
skinparam sequenceArrowThickness 2
'skinparam handwritten true
class Rxbus {
+IEventSubscriber iEventSubscriber
+addSubscriber(IEventSubscriber)
+sendMessageTo(Event)
}

interface IEventSubscriber
Rxbus --> IEventSubscriber
namespace com.xueshuyuan.cn.view #purple{

interface ILoginView{
+void loginWithPw()
+void loginByToken()
+void register()
+void success()
}
class LoginActivity<<接收反馈并更新UI>> {
+void loginWithPw()
+void loginByToken()
+void register()
+void success()
}

ILoginView <|--[#red] LoginActivity
}

namespace com.xueshuyuan.cn.presenter #orange{
interface ILoginPresenter{
+void loginByToken()
+void register()
}
class LoginPresenterImpl<<承接事件及接收通知处理并转发反馈>> {
+ILoginView iLoginView
+ILoginManager iLoginManager
+LoginPresenterImpl(ILoginView, ILoginManager)
+void loginWithPw()
+void loginByToken()
+void register()
+void receiveMessage(Event)
}

ILoginPresenter <|--[#red] LoginPresenterImpl
com.xueshuyuan.cn.view.LoginActivity <..[#red] LoginPresenterImpl :  Dependency
com.xueshuyuan.cn.moudle.LoginManagerImpl <..[#red] LoginPresenterImpl :  Dependency
.IEventSubscriber <|..[#red] ILoginPresenter
}


namespace com.xueshuyuan.cn.moudle #green{
interface ILoginManager{
+void loginWithPw()
+void loginByToken()
+void register()
+boolean checkUserExit()
+boolean checkPw()
}
class LoginManagerImpl<<承接事件及Rxbus发送通知>> {
+void loginWithPw()
+void loginByToken()
+void register()
+boolean checkUserExit()
+boolean checkPw()
+void sendMessageToXX(Event)
}

ILoginManager <|--[#red] LoginManagerImpl
}



@enduml

类图显示了系统的静态结构
类:类图中的主要元素,用矩形表示。矩形的上层表示类名中层表示属性下层表示方法
类之间的关系:关联、依赖、聚集、泛化和实现五种。

五种类间关系的图形表示介绍:

关联 依赖 聚集 泛化 extends 实现 implements
带实线的箭头 带虚线的箭头 菱形箭头 带实线的三角形箭头 带虚线的三角形箭头

这里写图片描述

@startuml
ClassA <-- ClassB:关联
ClassA <.. ClassB : 依赖
ClassA o-- ClassB:聚集
ClassA <|-- ClassB:泛化
ClassA <|.. ClassB:实现
@enduml

定义一个类的介绍

矩形的上层表示类名中层表示属性下层表示方法

【1】普通定义

这里写图片描述

@startuml
Class China {
    String area
    int rivers
    long person
    class Beijing{}
    interface aa{}

    String getArea()
    long getPerson()
}
@enduml

【2】多样定义

这里写图片描述

@startuml
Class China {
    -String area /'-表示权限private'/
    #int rivers  /'#表示权限protected'/
    +long person /'+表示权限public'/
    class Beijing{}
    interface aa{}

    ~String getArea() /'~表示权限package private'/
    long getPerson()
}
@enduml

静态属性+抽象方法

这里写图片描述

@startuml
Class China {
    {static}+int id /' 表示 静态属性(下划线) '/
    -String area
    #int rivers
    +long person

    ~String getArea()
    {abstract}long getPerson() /' 表示 抽象方法(斜体) '/
}
@enduml

自定义类主题

这里写图片描述

@startuml
class China {
  {static} int id
  int rivers
  long person
  .. /' 省略号 '/
  String city
  double lat
  ==/' 双分割线 '/
  {abstract}long getCities()
  __/' 单分割线 '/
  long getPerson()
  double getLat()
}

class Beijing {
  .. 注解说明 ..
  + setRiver()
  + setName()
  __ 注解说明 __
  + setPerson()
  -- 注解说明 --
  String password
}
 China <-- Beijing
@enduml

类图注释

这里写图片描述

@startuml
class MainActivity
note left:左侧注明用途
note right of MainActivity:右侧注明用途
note top of MainActivity:上面注明用途
note bottom of MainActivity:下面注明用途

class List<<general>>
note top of List : 接口类型,xxList extends it

class ArrayList
note left : 基于长度可变的数组的列表

note "Collection 的衍生接口和类" as NOTE
List .. NOTE
NOTE .. ArrayList

List <|-- ArrayList
@enduml

关于类、抽象类和接口的定义及关系

【1】动物园的饲养员能够给各种各样的动物喂食,绘制逻辑,效果
这里写图片描述

@startuml
class Feeder<<饲养员>>{
-void feed()
}

abstract Food
class Bone
class Fish
Food <|--Bone
Food <|--Fish

abstract Animal{
-void eat()
}
class Dog{
-void eat()
}
class Cat{
-void eat()
}
Animal <|-- Dog
Animal <|-- Cat


Feeder ..>Food
Feeder ..>Animal

@enduml

【2】关于Set以及其衍生类之间的关系,绘制逻辑
这里写图片描述

@startuml
interface Set<<接口>>{
boolean add (Object o)
boolean remove(Object o)
}

class HashSet{
+boolean add (Object o)
+boolean remove(Object o)
}
interface IntSet{
boolean add (int i)
boolean remove(int i)
}
class IntHashSet{
+boolean add (int i)
+boolean remove(int i)
}

Set <|.. HashSet
HashSet <|-- IntHashSet
IntSet <|.. IntHashSet

class TreeSet{
+boolean add (Object o)
+boolean remove(Object o)
}
class IntTreeSet{
+boolean add (int i)
+boolean remove(int i)
}

IntSet <|.. IntTreeSet
TreeSet <|-- IntTreeSet
Set <|.. TreeSet

@enduml

使用泛型功能的类图,效果

这里写图片描述

@startuml
...略

class HashSet<? extends String>{
+boolean add (Object o)
+boolean remove(Object o)
}
...略

@enduml

同属一个包下的类、抽象类和接口,效果

这里写图片描述

@startuml
interface Set<<接口>>{
boolean add (Object o)
boolean remove(Object o)
}

package "com.ztman.cn" #green{
class HashSet<? extends String>{
+boolean add (Object o)
+boolean remove(Object o)
}
interface IntSet{
boolean add (int i)
boolean remove(int i)
}
class IntHashSet{
+boolean add (int i)
+boolean remove(int i)
}
}

Set <|.. HashSet
HashSet <|-- IntHashSet
IntSet <|.. IntHashSet

class TreeSet{
+boolean add (Object o)
+boolean remove(Object o)
}
class IntTreeSet{
+boolean add (int i)
+boolean remove(int i)
}

IntSet <|.. IntTreeSet
TreeSet <|-- IntTreeSet
Set <|.. TreeSet

@enduml

包与包之间建立关系,效果

这里写图片描述

@startuml
skinparam packageStyle rect/' 加入这行代码,样式纯矩形'/
interface Set<<接口>>{
boolean add (Object o)
boolean remove(Object o)
}

package "com.ztman.org"  as Pa #green{
class HashSet<? extends String>{
+boolean add (Object o)
+boolean remove(Object o)
}
interface IntSet{
boolean add (int i)
boolean remove(int i)
}
class IntHashSet{
+boolean add (int i)
+boolean remove(int i)
}
}

Set <|.. HashSet
HashSet <|-- IntHashSet
IntSet <|.. IntHashSet

package "com.ztman.org.cn"  as Pb #orange{
class TreeSet {
+boolean add (Object o)
+boolean remove(Object o)
}
class IntTreeSet{
+boolean add (int i)
+boolean remove(int i)
}
}
IntSet <|.. IntTreeSet
TreeSet <|-- IntTreeSet
Set <|.. TreeSet

Pb +-- Pa

@enduml

以命名空间分割,并在不同空间内类之间建立关系,效果

这里写图片描述

@startuml
skinparam packageStyle rect/' 加入这行代码,样式纯矩形'/
interface Set<<接口>>{
boolean add (Object o)
boolean remove(Object o)
}

namespace com.ztman.org #green{
class HashSet<? extends String>{
+boolean add (Object o)
+boolean remove(Object o)
}
interface IntSet{
boolean add (int i)
boolean remove(int i)
}
class IntHashSet{
+boolean add (int i)
+boolean remove(int i)
}

.Set <|.. HashSet
HashSet <|-- IntHashSet
IntSet <|.. IntHashSet
}


namespace com.ztman.org.cn #orange{
class TreeSet {
+boolean add (Object o)
+boolean remove(Object o)
}
class IntTreeSet{
+boolean add (int i)
+boolean remove(int i)
}

com.ztman.org.IntSet <|.. IntTreeSet
TreeSet <|-- IntTreeSet
.Set <|.. TreeSet
}

@enduml

对于一些单个存在且不想展示出来的类图的类、属性和方法,我们可以将其隐藏。只需要在其类 class 前加hide即可,显示使用show

内容参考:
https://blog.csdn.net/changsimeng/
https://blog.csdn.net/qq1300375795/article/

猜你喜欢

转载自blog.csdn.net/u012827205/article/details/80767632