PlantUML 之类图

类图介绍

  1. @startuml
  2. Class01 <|-- Class02
  3. Class03 *-- Class04
  4. Class05 o-- Class06
  5. Class07 .. Class08
  6. Class09 -- Class10
  7. @enduml

plantuml - 图1

  1. @startuml
  2. Class11 <|.. Class12
  3. Class13 --> Class14
  4. Class15 ..> Class16
  5. Class17 ..|> Class18
  6. Class19 <--* Class20
  7. @enduml

plantuml - 图2

  1. @startuml
  2. Class21 #-- Class22
  3. Class23 x-- Class24
  4. Class25 }-- Class26
  5. Class27 +-- Class28
  6. Class29 ^-- Class30
  7. @enduml

plantuml - 图3

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

UML Sequence
UML Class
UML Activity

这里写图片描述

  1. @startuml
  2. title decoupling
  3. 'skinparam packageStyle rect/' 加入这行代码,样式纯矩形'/
  4. skinparam backgroundColor #EEEBDC
  5. skinparam roundcorner 20
  6. skinparam sequenceArrowThickness 2
  7. 'skinparam handwritten true
  8. class Rxbus {
  9. +IEventSubscriber iEventSubscriber
  10. +addSubscriber(IEventSubscriber)
  11. +sendMessageTo(Event)
  12. }
  13. interface IEventSubscriber
  14. Rxbus --> IEventSubscriber
  15. namespace com.xueshuyuan.cn.view #purple{
  16. interface ILoginView{
  17. +void loginWithPw()
  18. +void loginByToken()
  19. +void register()
  20. +void success()
  21. }
  22. class LoginActivity<<接收反馈并更新UI>> {
  23. +void loginWithPw()
  24. +void loginByToken()
  25. +void register()
  26. +void success()
  27. }
  28. ILoginView <|--[#red] LoginActivity
  29. }
  30. namespace com.xueshuyuan.cn.presenter #orange{
  31. interface ILoginPresenter{
  32. +void loginByToken()
  33. +void register()
  34. }
  35. class LoginPresenterImpl<<承接事件及接收通知处理并转发反馈>> {
  36. +ILoginView iLoginView
  37. +ILoginManager iLoginManager
  38. +LoginPresenterImpl(ILoginView, ILoginManager)
  39. +void loginWithPw()
  40. +void loginByToken()
  41. +void register()
  42. +void receiveMessage(Event)
  43. }
  44. ILoginPresenter <|--[#red] LoginPresenterImpl
  45. com.xueshuyuan.cn.view.LoginActivity <..[#red] LoginPresenterImpl : Dependency
  46. com.xueshuyuan.cn.moudle.LoginManagerImpl <..[#red] LoginPresenterImpl : Dependency
  47. .IEventSubscriber <|..[#red] ILoginPresenter
  48. }
  49. namespace com.xueshuyuan.cn.moudle #green{
  50. interface ILoginManager{
  51. +void loginWithPw()
  52. +void loginByToken()
  53. +void register()
  54. +boolean checkUserExit()
  55. +boolean checkPw()
  56. }
  57. class LoginManagerImpl<<承接事件及Rxbus发送通知>> {
  58. +void loginWithPw()
  59. +void loginByToken()
  60. +void register()
  61. +boolean checkUserExit()
  62. +boolean checkPw()
  63. +void sendMessageToXX(Event)
  64. }
  65. ILoginManager <|--[#red] LoginManagerImpl
  66. }
  67. @enduml

类图显示了系统的静态结构

类:类图中的主要元素,用矩形表示。矩形的上层表示类名中层表示属性下层表示方法

类之间的关系:关联、依赖、聚集、泛化和实现五种。

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

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

这里写图片描述

  1. @startuml
  2. ClassA <-- ClassB:关联
  3. ClassA <.. ClassB : 依赖
  4. ClassA o-- ClassB:聚集
  5. ClassA <|-- ClassB:泛化
  6. ClassA <|.. ClassB:实现
  7. @enduml

定义一个类的介绍

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

【1】普通定义

这里写图片描述

  1. @startuml
  2. Class China {
  3. String area
  4. int rivers
  5. long person
  6. class Beijing{}
  7. interface aa{}
  8. String getArea()
  9. long getPerson()
  10. }
  11. @enduml

【2】多样定义

这里写图片描述

  1. @startuml
  2. Class China {
  3. -String area /'-表示权限private'/
  4. #int rivers /'#表示权限protected'/
  5. +long person /'+表示权限public'/
  6. class Beijing{}
  7. interface aa{}
  8. ~String getArea() /'~表示权限package private'/
  9. long getPerson()
  10. }
  11. @enduml

静态属性+抽象方法

这里写图片描述

  1. @startuml
  2. Class China {
  3. {static}+int id /' 表示 静态属性(下划线) '/
  4. -String area
  5. #int rivers
  6. +long person
  7. ~String getArea()
  8. {abstract}long getPerson() /' 表示 抽象方法(斜体) '/
  9. }
  10. @enduml

自定义类主题

这里写图片描述

  1. @startuml
  2. class China {
  3. {static} int id
  4. int rivers
  5. long person
  6. .. /' 省略号 '/
  7. String city
  8. double lat
  9. ==/' 双分割线 '/
  10. {abstract}long getCities()
  11. __/' 单分割线 '/
  12. long getPerson()
  13. double getLat()
  14. }
  15. class Beijing {
  16. .. 注解说明 ..
  17. + setRiver()
  18. + setName()
  19. __ 注解说明 __
  20. + setPerson()
  21. -- 注解说明 --
  22. String password
  23. }
  24. China <-- Beijing
  25. @enduml

类图注释

这里写图片描述

  1. @startuml
  2. class MainActivity
  3. note left:左侧注明用途
  4. note right of MainActivity:右侧注明用途
  5. note top of MainActivity:上面注明用途
  6. note bottom of MainActivity:下面注明用途
  7. class List<<general>>
  8. note top of List : 接口类型,xxList extends it
  9. class ArrayList
  10. note left : 基于长度可变的数组的列表
  11. note "Collection 的衍生接口和类" as NOTE
  12. List .. NOTE
  13. NOTE .. ArrayList
  14. List <|-- ArrayList
  15. @enduml

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

【1】动物园的饲养员能够给各种各样的动物喂食,绘制逻辑,效果

这里写图片描述

  1. @startuml
  2. class Feeder<<饲养员>>{
  3. -void feed()
  4. }
  5. abstract Food
  6. class Bone
  7. class Fish
  8. Food <|--Bone
  9. Food <|--Fish
  10. abstract Animal{
  11. -void eat()
  12. }
  13. class Dog{
  14. -void eat()
  15. }
  16. class Cat{
  17. -void eat()
  18. }
  19. Animal <|-- Dog
  20. Animal <|-- Cat
  21. Feeder ..>Food
  22. Feeder ..>Animal
  23. @enduml

【2】关于Set以及其衍生类之间的关系,绘制逻辑

这里写图片描述

  1. @startuml
  2. interface Set<<接口>>{
  3. boolean add (Object o)
  4. boolean remove(Object o)
  5. }
  6. class HashSet{
  7. +boolean add (Object o)
  8. +boolean remove(Object o)
  9. }
  10. interface IntSet{
  11. boolean add (int i)
  12. boolean remove(int i)
  13. }
  14. class IntHashSet{
  15. +boolean add (int i)
  16. +boolean remove(int i)
  17. }
  18. Set <|.. HashSet
  19. HashSet <|-- IntHashSet
  20. IntSet <|.. IntHashSet
  21. class TreeSet{
  22. +boolean add (Object o)
  23. +boolean remove(Object o)
  24. }
  25. class IntTreeSet{
  26. +boolean add (int i)
  27. +boolean remove(int i)
  28. }
  29. IntSet <|.. IntTreeSet
  30. TreeSet <|-- IntTreeSet
  31. Set <|.. TreeSet
  32. @enduml

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

这里写图片描述

  1. @startuml
  2. ...略
  3. class HashSet<? extends String>{
  4. +boolean add (Object o)
  5. +boolean remove(Object o)
  6. }
  7. ...略
  8. @enduml

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

这里写图片描述

  1. @startuml
  2. interface Set<<接口>>{
  3. boolean add (Object o)
  4. boolean remove(Object o)
  5. }
  6. package "com.ztman.cn" #green{
  7. class HashSet<? extends String>{
  8. +boolean add (Object o)
  9. +boolean remove(Object o)
  10. }
  11. interface IntSet{
  12. boolean add (int i)
  13. boolean remove(int i)
  14. }
  15. class IntHashSet{
  16. +boolean add (int i)
  17. +boolean remove(int i)
  18. }
  19. }
  20. Set <|.. HashSet
  21. HashSet <|-- IntHashSet
  22. IntSet <|.. IntHashSet
  23. class TreeSet{
  24. +boolean add (Object o)
  25. +boolean remove(Object o)
  26. }
  27. class IntTreeSet{
  28. +boolean add (int i)
  29. +boolean remove(int i)
  30. }
  31. IntSet <|.. IntTreeSet
  32. TreeSet <|-- IntTreeSet
  33. Set <|.. TreeSet
  34. @enduml

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

这里写图片描述

  1. @startuml
  2. skinparam packageStyle rect/' 加入这行代码,样式纯矩形'/
  3. interface Set<<接口>>{
  4. boolean add (Object o)
  5. boolean remove(Object o)
  6. }
  7. package "com.ztman.org" as Pa #green{
  8. class HashSet<? extends String>{
  9. +boolean add (Object o)
  10. +boolean remove(Object o)
  11. }
  12. interface IntSet{
  13. boolean add (int i)
  14. boolean remove(int i)
  15. }
  16. class IntHashSet{
  17. +boolean add (int i)
  18. +boolean remove(int i)
  19. }
  20. }
  21. Set <|.. HashSet
  22. HashSet <|-- IntHashSet
  23. IntSet <|.. IntHashSet
  24. package "com.ztman.org.cn" as Pb #orange{
  25. class TreeSet {
  26. +boolean add (Object o)
  27. +boolean remove(Object o)
  28. }
  29. class IntTreeSet{
  30. +boolean add (int i)
  31. +boolean remove(int i)
  32. }
  33. }
  34. IntSet <|.. IntTreeSet
  35. TreeSet <|-- IntTreeSet
  36. Set <|.. TreeSet
  37. Pb +-- Pa
  38. @enduml

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

这里写图片描述

  1. @startuml
  2. skinparam packageStyle rect/' 加入这行代码,样式纯矩形'/
  3. interface Set<<接口>>{
  4. boolean add (Object o)
  5. boolean remove(Object o)
  6. }
  7. namespace com.ztman.org #green{
  8. class HashSet<? extends String>{
  9. +boolean add (Object o)
  10. +boolean remove(Object o)
  11. }
  12. interface IntSet{
  13. boolean add (int i)
  14. boolean remove(int i)
  15. }
  16. class IntHashSet{
  17. +boolean add (int i)
  18. +boolean remove(int i)
  19. }
  20. .Set <|.. HashSet
  21. HashSet <|-- IntHashSet
  22. IntSet <|.. IntHashSet
  23. }
  24. namespace com.ztman.org.cn #orange{
  25. class TreeSet {
  26. +boolean add (Object o)
  27. +boolean remove(Object o)
  28. }
  29. class IntTreeSet{
  30. +boolean add (int i)
  31. +boolean remove(int i)
  32. }
  33. com.ztman.org.IntSet <|.. IntTreeSet
  34. TreeSet <|-- IntTreeSet
  35. .Set <|.. TreeSet
  36. }
  37. @enduml

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

优秀类图源码展示

这里写图片描述

  1. @startuml
  2. title 视频播放功能的业务关联图
  3. package 全屏播放业务功能 <<Cloud>> #DeepSkyBlue{
  4. package 视频播放业务层 <<Frame>> #DodgerBlue{
  5. package 播放内核 <<Frame>> #Blue{
  6. interface ADVideoPlayerListener{
  7. +{static}{abstract}void onBufferUpdate(int position);//回调,视频缓冲
  8. +{static}{abstract}void onAdVideoLoadSucess();//回调,视频加载成功
  9. +{static}{abstract}void onAdVideoPlayComplete();//回调,视频正常播放完成
  10. +{static}{abstract}void onAdVideoLoadFailed();//回调,视频不能正常播放
  11. +{static}{abstract}void onClickVideo();//回调,点击视频区域的
  12. +{static}{abstract}void onClickFullScreenBtn();//回调,点击全屏按钮
  13. +{static}{abstract}void onClickBackBtn();
  14. +{static}{abstract}void onClickPlay();
  15. }
  16. class CustomVideoView {
  17. .. 视频播放器,只有暂停、播放和停止的基本功能 ..
  18. ..其他功能事件实现在业务层..
  19. interface ADVideoPlayerListener
  20. -MediaPlayer mediaPlayer
  21. -ADVideoPlayerListener listener
  22. -ScreenEventReceiver mScreenEventReceiver
  23. ...
  24. }
  25. CustomVideoView o-down- ADVideoPlayerListener
  26. }
  27. interface AdSDKShellListener {
  28. +{static}{abstract}ViewGroup getAdParent();
  29. +{static}{abstract}void onAdVideoLoadSuccess();
  30. +{static}{abstract}void onAdVideoLoadFailed();
  31. +{static}{abstract}void onAdVideoPlayComplete();
  32. +{static}{abstract}void onClickVideo(String url);
  33. }
  34. class VideoAdShell{
  35. ..视频播放器的业务封装层..
  36. interface ADVideoPlayerListener
  37. +VideoAdShell(AdValue, AdSDKShellListener, ADFrameImageLoadListener)
  38. -MediaPlayer mediaPlayer
  39. -ADVideoPlayerListener listener
  40. -ScreenEventReceiver mScreenEventReceiver
  41. ...
  42. }
  43. AdSDKShellListener -down-o VideoAdShell
  44. VideoAdShell -right-|> ADVideoPlayerListener
  45. VideoAdShell .right.> CustomVideoView
  46. }
  47. class VideoFullDialog{
  48. ..全屏视频播放..
  49. - AdSDKShellListener mShellListener
  50. -MediaPlayer mediaPlayer
  51. -ADVideoPlayerListener listener
  52. -ScreenEventReceiver mScreenEventReceiver
  53. +VideoFullDialog(Context, CustomVideoView, AdValue, int)
  54. +void setShellListener(AdSDKShellListener)//承接业务层回调
  55. +void setListener(FullToSmallListener)//切换小屏回调
  56. ...
  57. }
  58. VideoFullDialog -up-|> ADVideoPlayerListener
  59. }
  60. VideoAdShell<..>VideoFullDialog
  61. package 客户端 <<Frame>> #LightGray{
  62. package SDK入口 <<Frame>> #gray{
  63. interface AdSDKShellListener{
  64. +{static}{abstract}void onAdSuccess();//视频资源加载成功
  65. +{static}{abstract}void onAdFailed();//无法播放
  66. +{static}{abstract}void onClickVideo(String url);//点击播放窗口回调
  67. }
  68. class VideoAdFactory{
  69. ..使用构建者模式构建实例,方便用户使用..
  70. ..使用外观模式封装视频播放SDK,方便使用API..
  71. -final ViewGroup mParentView;
  72. -final AdValue mInstance;
  73. +void setAdResultListener(AdFactoryInterface)
  74. +void updateAdInScrollView//根据用户滑动页面来判断视频自动播放
  75. +{static} class Builder
  76. }
  77. VideoAdFactory -up-|> AdSDKShellListener
  78. }
  79. class Client<<用户层>> {
  80. ..生成VideoAdFactory实例,
  81. 调用视频的API..
  82. }
  83. Client -left-> VideoAdFactory
  84. }
  85. @enduml