4.1.1 What Is a Class Diagram?
A class diagram models the static structure of a system.
Unlike:
Flowcharts → model process flow
Sequence diagrams → model interaction over time
Class diagrams model:
They are fundamental to:
Object-Oriented Design (OOD)
API contract documentation
System architecture planning
In Mermaid, every class diagram begins with:
4.1.2 Basic Class Declaration
The simplest possible class:
This creates a class named User.
At this level, we are defining only the existence of the class.
4.1.3 Adding Attributes
Attributes define the state of a class.
Syntax:
Example:
This models a User with three attributes.
4.1.4 Adding Methods
Methods define behavior.
Syntax:
Example:
Here:
login() and logout() are methods.
+ indicates public visibility.
4.1.5 Visibility Modifiers
Mermaid supports UML-style visibility symbols:
Example:
This indicates:
validateCredentials() is protected
Visibility helps communicate encapsulation principles.
4.1.6 Full Class Definition Example
This class includes:
It represents a typical domain entity.
4.1.7 Data Types in Class Diagrams
Mermaid does not enforce strict typing, but common conventions include:
Example:
Use consistent data type conventions throughout your diagram.
4.1.8 Parameterized Methods
Methods can include parameters.
This resembles real-world service contracts.
4.1.9 Static Methods and Attributes
Static members can be shown using $.
Example:
Static members belong to the class, not the instance.
4.1.10 Real-World Example — E-Commerce Domain
At this stage, we are defining classes only — relationships will be covered later.
4.1.11 Organizing Large Classes
For clarity:
Keep consistent indentation
Bad practice:
Good practice:
Consistency improves readability.
4.1.12 Minimal vs Detailed Class
Minimal:
Detailed:
Choose level of detail based on purpose:
High-level architecture → minimal
Design documentation → detailed
4.1.13 Refactoring Example (Hands-On)
Start simple:
Enhance it:
Now it resembles a real service model.
4.1.14 Common Beginner Mistakes
Forgetting classDiagram keyword
Mixing relationship syntax prematurely
Overloading classes with too many attributes
Incorrect:
Correct:
4.1.15 Hands-On Exercise
Design a Library System with:
Classes:
Requirements:
Each class must have at least 2 attributes
Each class must have at least 1 method
Focus only on defining classes — do not add relationships yet.
4.1.16 Best Practices for Class Definition
Use meaningful class names
Follow consistent naming convention (PascalCase)
Include only relevant methods
Model domain logic, not implementation detail
Remember:
A class diagram represents structure, not runtime behavior.
4.1.17 Key Takeaways
Defining classes in Mermaid involves:
Applying visibility modifiers
Maintaining structural clarity
You are modeling static system structure, not execution flow.
In the next topic, we will explore Relationships and Multiplicity, which define how classes connect and interact structurally.