用代码画UML类图快餐教程

时间:2022-04-28 16:07:41

之前我们讨论了graphviz的dot图,mermaid画流程图和时序图,plantuml画时序图。

plantuml除了可以画时序图之外,还可以画类图。就跟写代码一样,直接上例子:

@startuml

interface Tree{
+ Tree getParent()
+ Object getPayload()
+ Tree getChild(int i)
+ int getChildCount()
+ String toStringTree()
}
interface SyntaxTree{
+ Interval getSourceInterval()
}
interface ParseTree{
+ <T> T accept(ParseTreeVisitor<? extends T> visitor)
+ String getText()
+ String toStringTree(Parser parser)
}
interface RuleNode{

+ RuleContext getRuleContext()
}
interface TerminalNode{
+ Token getSymbol()
}
class RuleContext
class ParserRuleContext

Tree <|-- SyntaxTree
SyntaxTree <|-- ParseTree
ParseTree <|-- RuleNode
ParseTree <|-- TerminalNode
RuleNode <|.. RuleContext
RuleContext <|-- ParserRuleContext

@enduml

简单介绍一下:
* interface定义接口,属性:公开的前面加”+”,私有的之前加”-”
* class定义类
* 关系定义:
* 泛化,Generalization:<|–
* 关联,Association:<–
* 组合,Composition:*–
* 聚合,Aggregation:o–
* 实现,Realization:<|..
* 依赖,Dependency:<..

来看看上面代码的效果吧:

用代码画UML类图快餐教程

更详细的信息,请参考官方文档:http://plantuml.com/class-diagram