Mermaid class diagrams
Sketch the classes in a program and how they relate, in plain text. This guide covers the class diagram syntax, with examples you can load into the converter and download as a PDF.
Download PDF opens your browser's print dialog. Choose Save as PDF as the destination.
Your first class diagram
A class diagram is the standard way to show the structure of object-oriented code: which classes exist, what each one holds and does, and how they connect. It's a common sight in design documents, code reviews and course notes, and the notation comes from UML, so anyone who has studied software design will be able to read it.
Start the block with classDiagram. A class is a box with three parts: its name, its fields
and its methods. Define one with class, its name and its members in curly braces.
```mermaid
classDiagram
class BankAccount {
+String owner
+Decimal balance
+deposit(amount) void
+withdraw(amount) bool
}
```
Fields and methods
Mermaid tells fields and methods apart by the parentheses: anything with () is a method
and goes in the bottom part of the box. A field is written as its type and then its name, as in
String owner. A method can list its parameters inside the parentheses and its return type
after them, as in withdraw(amount) bool.
Members can also be added one at a time, with the class name, a colon and the member. This is handy for adding to a class defined elsewhere in the diagram, and both forms can be mixed.
```mermaid
classDiagram
class Thermostat
Thermostat : +float target
Thermostat : +float current
Thermostat : +setTarget(float degrees) void
Thermostat : +isHeating() bool
```
Visibility, static and abstract
A symbol before a member says who can use it, following UML's convention.
| Symbol | Means |
|---|---|
+ | Public: any code can use it |
- | Private: only the class itself |
# | Protected: the class and its subclasses |
~ | Package or internal |
A symbol at the end marks a method or field as special: $ makes it static, and
* makes it abstract. Put it straight after the closing
parenthesis of a method, or after a field's name.
```mermaid
classDiagram
class Shape {
#String name
-int id$
+area() float*
+describe() String
+count() int$
}
```
Relationships
Lines between classes show how they're connected, and the end of the line says what kind of connection
it is. The symbol sits next to the class it describes: in Animal <|-- Dog, the hollow
triangle points at Animal, the parent.
| You type | Relationship | Means |
|---|---|---|
A <|-- B | Inheritance | B is a kind of A |
A *-- B | Composition | A owns B; B can't exist without it |
A o-- B | Aggregation | A has B, but B can exist on its own |
A --> B | Association | A uses or knows about B |
A ..> B | Dependency | A depends on B for a while, such as a parameter |
A ..|> B | Realization | A implements the interface B |
A -- B | Link | A plain connection |
```mermaid
classDiagram
class Vehicle
class Car
class Bicycle
class Engine
class Driver
class Garage
Vehicle <|-- Car
Vehicle <|-- Bicycle
Car *-- Engine
Garage o-- Car
Driver --> Car
```
A class mentioned only in a relationship is still drawn, as an empty box, so the class
lines at the top here are optional. They're worth keeping when you want to control the order in which
classes are introduced.
Labels and multiplicity
A label after a colon names the relationship. Multiplicity, how many of each side take part, goes in
quotes next to each class: "1", "0..1", "*" or
"1..*".
```mermaid
classDiagram
Customer "1" --> "0..*" Order : places
Order "1" *-- "1..*" OrderLine : contains
OrderLine "0..*" --> "1" Product : refers to
```
Read it in both directions: one customer places any number of orders, and each order belongs to exactly one customer.
Generics and annotations
Generic types are written with tildes rather than angle brackets, which Mermaid would read as
HTML: List~String~ is drawn as List<String>. This works in
class names and in member types.
An annotation such as <<interface>> or <<abstract>>
appears above the class name. Put it on the first line inside the class's braces.
```mermaid
classDiagram
class Repository~T~ {
<<interface>>
+find(id) T
+save(T item) void
}
class UserRepository {
-List~User~ cache
+find(id) User
+save(User item) void
}
Repository~T~ <|.. UserRepository
```
Notes and direction
A note adds an explanation in a box of its own. note for ClassName "text" attaches it to
a class; a note without for stands alone. A direction line changes the layout
from top-to-bottom to LR, RL or BT, which helps when a class has
many subclasses side by side.
```mermaid
classDiagram
direction LR
class Payment {
+Decimal amount
+process() bool
}
Payment <|-- CardPayment
Payment <|-- BankTransfer
note for BankTransfer "Takes 1 to 3 working days"
```
Common mistakes
When a class diagram has a mistake, the preview shows its code with the error above it, including the line Mermaid stopped at. These are the usual causes:
- Angle brackets for generics.
List<String>doesn't work; writeList~String~. - Spaces in a class name. Mermaid runs the words together, so
class Order Lineis drawn as OrderLine. To show a name with spaces, give the class a label:class OrderLine["Order line"]. - The arrow pointing the wrong way. There's no error, but
Dog <|-- Animalsays that animals are a kind of dog. The triangle goes next to the parent. - A brace on the wrong line. The opening
{goes on the same line as the class name, and the closing}on a line of its own.
Class diagrams in a PDF
md2pdf.me draws each diagram as a vector graphic, so the boxes and text stay sharp however far you zoom into the PDF, and a diagram is never split across two pages. A diagram wider than the page's text is shrunk to fit it, and so is one taller than most of a page. Class diagrams spread sideways quickly, since sibling classes sit next to each other, so a large one can end up with small text.
Rather than one diagram of a whole codebase, draw one per area, each with a heading and a sentence on what it covers. Leave out the members that don't matter to the point you're making: a diagram about how classes relate often reads better with just class names.
A diagram's colors come from the style you choose. Classic, Academic and Résumé draw it in shades of grey for printing, Modern and Report in their blue accent, Worksheet in its own blue and purple, and GitHub in the same colors as github.com. The themed styles use their own colors: oxblood for Gothic, rust and navy for Sandwake, forest green for Elvish, teal and magenta for Cyberpunk, black and oxblood on newsprint grey for Newspaper, and ink and vermilion for Fiendish.
Other diagram types
- Flowcharts: steps, decisions and the arrows between them
- Sequence diagrams: messages between people or systems over time
- Gantt charts: project schedules
- State diagrams: the states something moves through
- Entity relationship diagrams: database tables