Mermaid之Class Diagram
类图(Class Diagram)
“In software engineering, a class diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects.”
-Wikipedia
class diagram一般用来表明应用(Application)的整体概念模型(general conceptual modeling),开发人员可根据class diagram使用具体的编程语言实现应用。
Note
Title以及Note在CSDN的渲染中暂时还不支持。
语法(Syntax)
\quad\quad
在class diagram中,类由三部分组成,即 类名、属性以及方法。
如何定义一个类
\quad\quad
在class diagram中,有两种方式可以定义一个类,直接给出例子
classDiagram
class Student {
-String name
-int id
+getName() String
+getId() int
}
class Teacher
Teacher : -String name
Teacher : -int id
Teacher : +getName() String
Teacher : +getId() int
\quad\quad
是的,正如你所见,mermaid支持函数的返回值类型后缀。其中第一种方法和我们定义类的过程比较类似。
可见性
+Public可见性-Private可见性#Protected可见性~Package可见性
如何表示类之间的继承关系
classDiagram
class Person {
-String name
+getName() String
}
class Eatable {
<<interface>>
+eat() void*
}
Person <|-- Student
Eatable <|.. Student
class Student {
-String name
+getName() String
+eat() void
}
类之间有哪些关系
| 符号 | 描述 |
|---|---|
<|-- | Inheritance |
<|.. | Realization |
<-- | Association |
<.. | Dependency |
*-- | Composition |
o-- | Aggregation |
-- | Link(Solid) |
.. | Link(Dashed) |
- 依赖关系(Dependency)
\quad\quad 假设有两个类,类A与类B。类A的某个成员方法使用了类B,则说明类A依赖类B。是一种use-a的关系。 - 关联关系(Association)
\quad\quad 假设有两个类,类A与类B。类B做为类A的成员变量存在,类A也可做为类B的成员变量存在。 - 聚合关系(Aggregation)
- 组合关系(Composition)
\quad\quad 其中依赖关系是一种临时的关系,依赖关系主要体现在方法参数上,只有调用方法时才有关系。关联关系是一种长期的关系,主要体现在成员变量上,无论是否调用方法,这种关系都是存在的。
\quad\quad 从关联到聚合再到组合,关系逐步递进。关联关系双方是平级的,而聚合关系是一种整体与部分的关系。聚合关系中的部分离开整体仍可存在,组合关系中的部分离开整体则没有意义。
聚合是一种特殊的关联关系,它是较强的一种关联关系,强调的是整体与部分之间的关系,从语法上是无法区分的,只能从语义上区分,组合也是关联关系的一种特例,这种关系比聚合关系更强。它强调了整体与部分生命周期是一致的,而聚合的整体和部分之间在生命周期上没有什么必然关系。——《关联、聚合、组合的区别》
如何表示泛型
结尾
\quad\quad
最后,以mermaid网站上的示例程序结尾吧:
---
title: Animal example
---
classDiagram
Animal <|-- Duck
Animal <|-- Fish
Animal <|-- Zebra
Animal : +int age
Animal : +String gender
Animal: +isMammal()
Animal: +mate()
class Duck{
+String beakColor
+swim()
+quack()
}
class Fish{
-int sizeInFeet
-canEat()
}
class Zebra{
+bool is_wild
+run()
}
博客介绍了UML类图,其用于表明应用整体概念模型,开发人员可据此用具体编程语言实现应用。还阐述了类图语法,包括类的定义、可见性,以及类之间的依赖、关联、聚合、组合等关系,最后以示例程序结尾。

596

被折叠的 条评论
为什么被折叠?



