Skip to content

Instantly share code, notes, and snippets.

@JeffMill
Last active October 8, 2025 22:05
Show Gist options
  • Save JeffMill/0883d7d5ca75f616a44e4c11a78584a2 to your computer and use it in GitHub Desktop.
Save JeffMill/0883d7d5ca75f616a44e4c11a78584a2 to your computer and use it in GitHub Desktop.
Mermaid Class Diagram Reference

Mermaid Class Diagram Reference

Adapted from Class diagrams

Type: classdiagram

Visibility

e.g. -idCard : IdCard

Placed before name:

Notation Description
+ Public
- Private
# Protected
~ Package/Internal

Placed after name (after "()" or return type):

Notation Description
* Abstract e.g.: someAbstractMethod()* or someAbstractMethod() int*
$ Static e.g.: someStaticMethod()$ or someStaticMethod() String$ or String someField$

Relationship

Syntax: [classA][Arrow][ClassB]

e.g. classA <|-- classB

Arrow Description
<-- Inheritance
*-- Composition
o-- Aggregation
--> Association
-- Link (Solid)
..> Dependency
..|> Realization
.. Link (Dashed)

Labels on Relations

Syntax: [classA][Arrow][ClassB]:LabelText

e.g. classA <|-- classB : label

Two-way relations (N:M)

Syntax: [Relation Type][Link][Relation Type]

e.g. Animal <|--|> Zebra

Relation Type Description
<|-- Inheritance
*-- Composition
o-- Aggregation
--> Association
-- Link (Solid)
..> Dependency
..|> Realization
.. Link (Dashed)
Link Description
-- Solid
.. Dashed

Lollipop Interface

Syntax: bar ()-- foo

The interface (bar) with the lollipop connects to the class (foo).

Define Namespace

e.g. namespace BaseShapes {

Cardinality / Multiplicity

Syntax: [classA] "cardinality1" [Arrow] "cardinality2" [ClassB]:LabelText

e.g. Customer "1" --> "*" Ticket

Cardinality Description
1 Only 1
0..1 Zero or One
1..* One or more
* Many
n n (where n>1)
0..n zero to n (where n>1)
1..n one to n (where n>1)

Annotations on classes

e.g. class Shape { <<interface>>

Annotation Description
<<Interface>> Interface class
<<Abstract>> abstract class
<<Service>> service class
<<Enumeration>> enum

Comments

e.g. %% This whole line is a comment

Notes

e.g. note "This is a general note"

e.g. note for MyClass "This is a note for MyClass

Example diagram

classDiagram
direction LR
class ModelFactory {
        +create(model_name) Model
}
class Context {
        -Model model
        +init(config)
        +training()
}
class Model {
        <<interface>>
        +fit()
}
namespace torch {
        class Module {
        }
}
namespace sklearn {
        class Estimator {
        }
}
class TorchModel {
        -Module module
        +fit()
}
class SklearnModel {
        -Estimator estimator
        +fit()
}

Context --> Model : STRATEGY

Estimator *-- SklearnModel : ADAPTER
ModelFactory <-- Context : FACTORY
Model <|.. SklearnModel : implements
Model <|.. TorchModel : implements
Module *-- TorchModel : ADAPTER
classDiagram
direction LR
class ModelFactory {
        +create(model_name) Model
}
class Context {
        -Model model
        +init(config)
        +training()
}
class Model {
        <<interface>>
        +fit()
}
namespace torch {
        class Module {
        }
}
namespace sklearn {
        class Estimator {
        }
}
class TorchModel {
        -Module module
        +fit()
}
class SklearnModel {
        -Estimator estimator
        +fit()
}

Context --> Model : STRATEGY

Estimator *-- SklearnModel : ADAPTER
ModelFactory <-- Context : FACTORY
Model <|.. SklearnModel : implements
Model <|.. TorchModel : implements
Module *-- TorchModel : ADAPTER
Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment