BA Prince the B.A. Subscribe

Home / Mermaid for Business Analysts

Mermaid Class Diagram for a Domain Model

Mermaid Class Diagram for a Domain Model

Watch on YouTube Subscribe

Write a UML class diagram in Mermaid for a library system: classes, attributes, an operation, and associations with multiplicity. A simple domain model a business analyst can review with developers, made in the free Mermaid live editor.

Step by step

The domain model

  1. Domain model in code. A domain model shows the things a system knows about and how they relate. A UML class diagram is the classic way to draw one. Here is one for a library, written as text in the free Mermaid live editor.
  2. Diagram type. Clear the editor and type classDiagram on the first line, as one word with a capital D. This tells Mermaid which kind of diagram to draw.
    classDiagram

Classes

  1. Class and attributes. Type class Member and a pair of curly brackets. Inside, write each attribute as a type then a name: +int memberId and +String name. The + means public. borrow() has round brackets, so it is an operation, something the class does. The preview draws Member as a box in 3 parts: the name on top, the attributes in the middle and the operations at the bottom. Add class Book with +String isbn and +String title. Class names are singular nouns: Book, not Books.
        class Member {
            +int memberId
            +String name
            +borrow()
        }
        class Book {
            +String isbn
            +String title
        }
  2. The link class. Add class Loan. It holds what belongs to one borrowing, like +Date dueDate. Its operation +isLate() bool returns true or false. The type after the brackets, bool, is what the operation gives back. The preview now shows 3 boxes, Member, Book and Loan, side by side with no lines between them yet.
        class Loan {
            +Date dueDate
            +isLate() bool
        }

Associations

  1. Multiplicity. On a new line, type Member "1" -- "0..*" Loan : makes. Two dashes draw a solid line with no arrow, which is a plain association. The values in quotes are the multiplicity: 1 member makes 0..* loans, read as zero or many. The verb after the colon labels the line. Add Book "1" -- "0..*" Loan : covers. It follows the same pattern: 1 book is covered by zero or many loans over time. The preview now shows each line with its multiplicity at both ends and its verb in the middle.
        Member "1" -- "0..*" Loan : makes
        Book "1" -- "0..*" Loan : covers

Check it

  1. Check with the business. Loan sits between Member and Book. Ask the business: can a single loan cover 2 books? If yes, the "1" at the Book end becomes "1..*", and the design changes with it.

Recap

  1. The rules. The rules: one class per thing, attributes as type then name, operations with round brackets, and multiplicity in quotes on each association.

Common mistakes

  • Typing class diagram as two words. The first line must be classDiagram, one word with a capital D. If it isn't, Mermaid doesn't know what kind of diagram it is and shows a syntax error.
  • Leaving the brackets off an operation. Mermaid tells operations from attributes only by the round brackets. Write +borrow(), not +borrow, or it shows up with the attributes.
  • Multiplicity without quotes. Put each value in double quotes, like "1" and "0..*". Without the quotes, Mermaid doesn't read them as multiplicity and the line breaks the diagram.
  • Reading the multiplicity from the wrong end. Each value counts the class it sits next to. In Member "1" -- "0..*" Loan, each loan has exactly 1 member, and each member has zero or many loans.

When a business analyst uses this

In a requirements workshop for a new library system, a business analyst writes the domain model in Mermaid and shares it with developers and the business. Together they check each class, attribute and multiplicity, for example whether one loan can cover 2 books, and record the answers in the decision log before sign-off.

Questions people ask

How do you show multiplicity in a Mermaid class diagram?

Put a value in double quotes on each side of the link, like Member "1" -- "0..*" Loan : makes. Mermaid shows each value at its end of the line.

How do you add a method with a return type in Mermaid?

Write the method with round brackets, then a space and the return type, like +isLate() bool. Mermaid puts it in the operations part of the class box.

What does the plus sign mean in a Mermaid class diagram?

It marks an attribute or operation as public. Mermaid also uses - for private, # for protected and ~ for package or internal.

More in Mermaid for Business Analysts