2022/9/5

Mermaid - Class Diagram

classDiagram
      Animal <|-- Duck
      Animal <|-- Fish
      Animal <|-- Zebra
      Animal : +int age
      Animal : +String gender
      Animal: +isMammal()
      Animal: +mate()
      class Duck{
          +String beakColor
          +swim()
          +quack()
      }
      class Fish{
          -int sizeInFeet
          -canEat()
      }
      class Zebra{
          +bool is_wild
          +run()
      }

render

語法

class 包含三個部分

  • 最上面是 class name,第一個字母是大寫

  • 中間是 attributes,第一個字母小寫,可加上類別

  • 最下面是 operations,第一個字母小寫

classDiagram
    class BankAccount
    BankAccount : +String owner
    BankAccount : +Bigdecimal balance
    BankAccount : +deposit(amount)
    BankAccount : +withdrawl(amount)

render

定義 class

  • 關鍵字為 class ex: class Animal

  • 兩個 class 之間有繼承關係 ex: Animal <|-- Dog

classDiagram
    class Animal
    Animal <|-- Dog

render

定義 members

有兩種寫法

classDiagram
 class BankAccount
 BankAccount : +String owner
 BankAccount : +BigDecimal balance
 BankAccount : +deposit(amount)
 BankAccount : +withdrawal(amount)

class BankAccount2{
    +String owner
    +BigDecimal balance
    +deposit(amount)
    +withdrawl(amount)
}

render

method 後面可加上 return type

classDiagram
class BankAccount{
    +String owner
    +BigDecimal balance
    +deposit(amount) bool
    +withdrawl(amount) int
}

GenericType: 在 Java 有時會用到 List<String> 這樣的資料結構,這邊用這樣的語法表示 List~string~

classDiagram
class Square~Shape~{
    int id
    List~Int~ position
    setPoints(List~Int~ points)
    getPoints() List~Int~
}

Square : -List~String~ messages
Square : +setMessages(List~String~ messages)
Square : +getMessages() List~String~

render

Visibility 有四種

  • + Public
  • - Private
  • # Protected
  • ~ Package/Internal

另外可在 method 與 field 後面加上 classifiers

  • * Abstract method e.g.: someAbstractMethod()*
  • $ Static method e.g.: someStaticMethod()$
  • $ Static field e.g.: String someField$

## 定義關係

[classA][Arrow][ClassB]
[classA][Arrow][ClassB]:LabelText
Type Description
<\ --
*-- Composition
o-- Aggregation
--> Association
-- Link (Solid)
..> Dependency
..\ >
.. Link (Dashed)

example : 後面是 label,可不填寫

classDiagram
classA --|> classB : Inheritance
classC --* classD : Composition
classE --o classF : Aggregation
classG --> classH : Association
classI -- classJ : Link(Solid)
classK ..> classL : Dependency
classM ..|> classN : Realization
classO .. classP : Link(Dashed)

render

classDiagram
classA <|-- classB : implements
classC *-- classD : composition
classE o-- classF : association

render

雙向關係

[Relation Type][Link][Relation Type]

Relation Type:

Type Description
<\
* Composition
o Aggregation
> Association
< Association
\ >

Link

Type Description
-- Solid
.. Dashed

cardinality/multiplicity on relations

  • 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}

語法

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

ex:

classDiagram
    Customer "1" --> "*" Ticket
    Student "1" --> "1..*" Course
    Galaxy --> "many" Star : Contains

render

Annotations on classes

用在 interface 或 abstract class

  • <<Interface>> To represent an Interface class
  • <<abstract>> To represent an abstract class
  • <<Service>> To represent a service class
  • <<enumeration>> To represent an enum

ex

classDiagram

class Shape
<<interface>> Shape
Shape : noOfVertices
Shape : draw()

class Color
<<enumeration>> Color
Color: RED
Color: BLUE
Color: GREEN
Color: WHITE
Color: BLACK

render

沒有留言:

張貼留言