Class diagram (Mermaid)

A class diagram earns its place when the relationships matter more than the fields. This one shows inheritance, composition and an association carrying multiplicity at both ends.


#What this example shows

  • Attributes and methods with visibility markers.

  • Inheritance drawn with <|--, composition with *--.

  • Multiplicity on both ends of a relationship, which is where the real information is.


#A worked example

Open it, edit it, or copy the source below.

Class diagram (Mermaid)

The source, which you can paste into a new diagram and edit:

classDiagram
  class Order {
    +UUID id
    +OrderStatus status
    +Money total
    +addItem(Product, int) void
    +submit() void
  }
  class OrderItem {
    +int quantity
    +Money unitPrice
    +lineTotal() Money
  }
  class Payment {
    <<abstract>>
    +Money amount
    +authorise() bool
  }
  class CardPayment {
    +String last4
  }
  class BankTransfer {
    +String reference
  }
  class Customer {
    +String email
    +Tier tier
  }

  Customer "1" --> "0..*" Order : places
  Order "1" *-- "1..*" OrderItem : contains
  Order "1" o-- "0..*" Payment : settled by
  Payment <|-- CardPayment
  Payment <|-- BankTransfer

#Making it your own

  • Drop the method lists if the diagram is for a non-engineering audience.

  • Use <<interface>> or <<enumeration>> where the stereotype matters.

  • Show only the classes under discussion. A whole-model class diagram is unreadable.



Copy it. Change one line. See what happens.