# Class Diagram

> Source: [*PlantUML: Class Diagram*](https://plantuml.com/en/class-diagram)\
> Digest Date: *January 15, 2023*

**Note**: The UML preview of this documentation is powered by **mermaid**, some PlantUML symbols are not supported by mermaid(e.g. `#--`), so I didn't record these special symbols.

* [1. Declaring Element](#1-declaring-element)
* [2. Relations between Classes](#2-relations-between-classes)
* [3. Label on Relations](#3-label-on-relations)
* [4. Using non-letters in element names and relation labels](#4-using-non-letters-in-element-names-and-relation-labels)
* [5. Adding methods](#5-adding-methods)
* [6. Defining visibility](#6-defining-visibility)

## 1. Declaring Element

![declaring-elements](https://883042997-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mi1EHMi0dOhf5YbiyN_%2Fuploads%2Fgit-blob-acf35cc65fc69b63857865054aaf7f1a4dfbac29%2Fdeclaring-elements.png?alt=media)

**Note**: `protocol` and `struct` was added in [PR-1028](https://github.com/plantuml/plantuml/pull/1028).

## 2. Relations between Classes

Relations between classes are defined using the following symbols :

| Type        | Symbol | Drawing                                                                                                                                                                                                  |
| ----------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Extension   | \`<    | --\`                                                                                                                                                                                                     |
| Composition | `*--`  | ![sym02](https://883042997-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mi1EHMi0dOhf5YbiyN_%2Fuploads%2Fgit-blob-49edb2376475f88cfcdb6ede505c8392e85f58fc%2Fsym02.png?alt=media) |
| Aggregation | `o--`  | ![sym03](https://883042997-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mi1EHMi0dOhf5YbiyN_%2Fuploads%2Fgit-blob-8da56929f5801cfc4914f481f4c67ce63e564540%2Fsym03.png?alt=media) |

It is possible to replace `--` by `..` to have a dotted line.

Knowing those rules, it is possible to draw the following drawings:

**Source**:

```
@startuml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml
```

**Preview by using mermaid**:

```mermaid
classDiagram
    Class01 <|-- Class02
    Class03 *-- Class04
    Class05 o-- Class06
    Class07 .. Class08
    Class09 -- Class10
```

***

**Source**:

```
@startuml
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml
```

**Preview by using mermaid**:

```mermaid
classDiagram
    Class11 <|.. Class12
    Class13 --> Class14
    Class15 ..> Class16
    Class17 ..|> Class18
    Class19 <--* Class20
```

## 3. Label on Relations

It is possible to add a label on the relation, using `:` , followed by the text of the label.

For cardinality, you can use double-quotes `""` on each side of the relation.

**Source**:

```
@startuml
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
Class05 --> "1" Class06
@enduml
```

**Preview by using mermaid**:

```mermaid
classDiagram
    Class01 "1" *-- "n" Class02 : contains
    Class03 o-- Class04 : aggregation
    Class05 --> "1" Class06
```

You can add an extra arrow pointing at one object showing which object acts on the other object, using `<` or `>` at the begin or at the end of the label.

**Source**:

```
@startuml
class Car
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml
```

**Preview by using mermaid**:

```mermaid
classDiagram
    class Car
    Driver -- Car : drives >
    Car *-- Wheel : have 4 >
    Car -- Person : < owns
```

> Warning: This feature is not fully supported by mermaid.

## 4. Using non-letters in element names and relation labels

Seems not so useful, omit for now.

## 5. Adding methods

To declare fields and methods, you can use the symbol `:` followed by the field's or method's name.

The system checks for parenthesis to choose between methods and fields.

**Source**:

```
@startuml
Object <|-- ArrayList

Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
```

**Preview by using mermaid**:

```mermaid
classDiagram
    Object <|-- ArrayList

    Object : equals()
    ArrayList : Object[] elementData
    ArrayList : size()
```

It is also possible to group between brackets `{}` all fields and methods.

**Note that the syntax is highly flexible about&#x20;*****type/name*****&#x20;order.**

**Source**:

```
@startuml
class Dummy {
  String data
  void methods()
}

class Flight {
   flightNumber : Integer
   departureTime : Date
}
@enduml
```

**Preview by using mermaid**:

```mermaid
classDiagram

    class Dummy {
        String data
        void methods()
    }

    class Flight {
        flightNumber : Integer
        departureTime : Date
    }
```

You can use `{field}` and `{method}` modifiers to override default behaviour of the parser about fields and methods.

**Source**:

```
@startuml

class Dummy {
    {field} A field (despite parentheses)
    {method} Some method
}

@enduml
```

**Preview by using mermaid**:

```mermaid
classDiagram

    class Dummy {
        {field} A field (despite parentheses)
        {method} Some method
    }
```

> Warning: This feature is not supported by mermaid.

## 6. Defining visibility
