[emacs] Drawing uml under emacs org-mode using plantUML - 类图


[emacs] Drawing uml under emacs org-mode using plantUML - 类图

 

[emacs] Drawing uml under emacs org-mode using plantUML - 类图

Table of Contents

  • 1 准备工作
    • 1.1 org-mode学习入门的相关资料
    • 1.2 搭建plantuml环境
    • 1.3 先爽一下
  • 2 PlantUML使用说明
    • 2.1 PlantUML 简介
    • 2.2 类图
      • 2.2.1 类之间的关系
      • 2.2.2 添加label
      • 2.2.3 添加方法
      • 2.2.4 注释以及定型
      • 2.2.5 抽象类以及接口
      • 2.2.6 方法属性的隐藏
      • 2.2.7 packages
      • 2.2.8 命名空间
      • 2.2.9 箭头方向的控制
      • 2.2.10 添加标题

1 准备工作

 

1.1 org-mode学习入门的相关资料

  1. David O'Toole Org tutorial
  2. 博客园-心内求法的博文
  3. 利用org-mode写cnblogs博客
  4. 官网的入门系列

1.2 搭建plantuml环境

  1. 安装plantuml, 参考
  2. 下载相关lisp文件, ob-plantuml.el
  3. 将plantuml的安装路径, 加入到 ob-plantuml.el 文件中
    # line 44
    (defcustom org-plantuml-jar-path "/your/install/path/plantuml.jar"
      "Path to the plantuml.jar file."
      :group 'org-babel
      :version "24.1"
      :type 'string)
    
  4. (require 'ob-plantuml) 加入到init.el或是.emacs内

1.3 先爽一下

当上述的工作都完成之后, 就可以happy的使用plantuml这个神奇的工具了. 需要编辑的代码如下:

Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response

在上述代码内部使用 的快捷键就能够在下面获得相应的图像. 是不是很爽呀, ;)

2 PlantUML使用说明

 

2.1 PlantUML 简介

PlantUML 允许使用简单的直观的语言实现, 能够用来绘制:

  1. 序列图
  2. 用例
  3. 类图
  4. 行为图
  5. 结构图
  6. 状态图
  7. 对象图

2.2 类图

由于对于一个程序员而言, 类的设计相对于其他的绘图而言更为重要, 因此此处首先介绍了plantUML在类图绘制中的使用方法

2.2.1 类之间的关系

此处, 类之间的关系以及所使用的符号如下:

 
联合, association <–
扩展, extension <(竖线)–
组成, composition *–
聚集, agregation o–
将表格中的 -- 替换成 .. 就能够获得相应的点线, 其中一个短线"<-"表示横向, 两个短线"<–"表示竖向

对于上述的关系表示非常困惑, 不急, 慢慢看来, ;)

  1. Association 1 2 有两个类, A 和 B, 如果A够根据将消息发送给B, 或是B的实例能够影响A, 那么就称两者之间的关系为Association, 并且在一般情况下两者是相互的. 如下所示:
    class class1 as "A"
    class class2 as "B"
    class1 --> class2
    

    class B {...}
    class A {
      B* itsB;
    }
    
  2. extension3, 也被称为继承关系, 或is-a关系.
    class food {
    };
    class apple : public food{
    };
    

    上面的关系就可以理解为, apple是food

    class class1 as "food"
    class class2 as "apple"
    
    class2 --|> class1
    

  3. composition and aggregation. 对于这两者的理解wikpedia中有一段很好的描述 4:

    一个大学里面有很多的学院, 每个学院又有很多的教授. 当这个大学倒闭关门的时候, 原来存在的学院也就随着大学的关门而不存在了, 但是这些教授却依然活着. 因此, 学校是由学院组成的, 即compostition, 学院是教授的集合地, aggregation

    class Professor;
    
    class Department
    {
     private:
     // Aggregation
     Professor* members[5];
    };
    
    class University
    {
     private:
     std::vector faculty;
     create_dept() {
    
       // Composition
       faculty.push_back(Department());
       faculty.push_back(Department());
     }
    };
    
    class class1 as "University"
    class class2 as "Department"
    class class3 as "Professor"
    
    class3 -right--o class2
    class2 -right--* class1
    

2.2.2 添加label

在关系的后面加个":", 而后添加相应的label, 在连接符两边用""可以添加对应类的label, 如

Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : agregation

2.2.3 添加方法

在类名后面添加":", 如:

Object <|-- ArrayList

Object : equals(0
ArrayList : Object[] elementData
ArrayList : size()

另一种方法为:

class Dummy {
  String data
  void methods()
}

在类中添加不同的方法的时候, 往往需要给出相应的方法的属性, 如private, protected, public等:

 
- private
# protected
~ package private
+ public

2.2.4 注释以及定型

对图中的类进行注释, 采用"..", 定型(即, 给类一个特定的分类), 采用""和" ".

在注释中还可以使用html标签对注释的内容的格式进行美化.

  • 加粗
  • 加下划线
  • 斜体
  • , , 删除线
  • or 字体颜色
  • or 字体颜色
  • 字体大小
  • or 添加图片
note top of Object
  In java, every class
  extends
  this one
end note

note as N1
  This note is also
  on several
  words lines
end note

2.2.5 抽象类以及接口

抽象类使用"abstract"或"abstract class"关键字, 接口用"interface"关键字, 枚举用"enum"关键字

abstract class AbstractList
abstract AbstractConllection
interface List
interface Collection

List <|-- AbstractList
Collection <|-- AbstractConllection

Collection <|- List
AbstractConllection <|- AbstractList
AbstractList <|- ArrayList

ArrayList : Object[] elementData
ArrayList : size()

enum TimeUnit
TimeUnit : DAYS
TimeUnit : HOURS
TimeUnit : MINUTES 

2.2.6 方法属性的隐藏

可以使用 "hide/show" 命令实现对方法以及属性的隐藏, 常用的命令如下:

  • hide empty members
  • hide empty fields or hide empty attributes
  • hide fields or hide attributes
  • hide methods
  • hide members
  • hide circle 隐藏类名前面圆形的字符
  • hide stereotype 通过""和" "增加的标识
  • hide class
  • hide interface
  • hide enum
  • hide 1 http://en.wikipedia.org/wiki/Association_%28object-oriented_programming%29

    2 http://ootips.org/uml-hasa.html

    3 http://publib.boulder.ibm.com/infocenter/rsahelp/v7r0m0/index.jsp?topic=/com.ibm.xtools.viz.j2se.doc/topics/cgeneralizn.html

    4 http://en.wikipedia.org/wiki/Object_composition#Aggregation

    Date: 2014-04-18 Fri

    Author: Zhong Xiewei

    Org version 7.8.11 with Emacs version 24

    Validate XHTML 1.0