Class diagrams are a core part of UML, describing the static relationships between classes in a system. Both PlantUML and Mermaid make it easy to draw the six most common relationship types: Association, Aggregation, Composition, Generalization, Dependency, and Realization.
@startuml hide circle skinparam classAttributeIconSize 0
class Driver class Car class Engine
' Association Driver "1" -- "1" Car : drives >
' Aggregation Car o-- "1" Engine : has >
' Composition Car *-- "1..4" Wheel : has >
' Generalization Sedan --|> Car
' Dependency Car ..> Driver : creates
' Realization ICruiseControl <|.. CruiseControl
class ICruiseControl class CruiseControl @enduml
Mermaid 版
1 2 3 4 5 6 7 8 9 10 11 12 13
classDiagram class Driver class Car class Engine class ICruiseControl class CruiseControl
Driver "1" -- "1" Car : drives Car o-- "1" Engine : has Car *-- "1..4" Wheel : has Sedan --|> Car Car ..> Driver : creates ICruiseControl <|.. CruiseControl
对比 / Side-by-side
Relationship
PlantUML arrow
Mermaid arrow
Association
--
--
Aggregation
o--
o--
Composition
*--
*--
Generalization
--|>
--|>
Dependency
..>
..>
Realization
<|..
<|..
小结 / Wrap-up
PlantUML and Mermaid use nearly identical arrows for class-diagram relationships, making it easy to switch between them. Solid lines (--) denote structural relationships while dotted lines (..) denote dynamic dependencies. Arrow direction points toward the depended-upon element: composition beats aggregation beats plain association.