3.3. UML Relationship

3.3.1. Rationale

  • Arrow with empty triangle (but on picture is filled)

  • <|-- - Inheritance

  • *-- - Composition

  • o-- - Aggregation

  • --> - Association

  • -- - Link (Solid)

  • ..> - Dependency

  • ..|> - Realization

  • .. - Link (Dashed)

../../_images/uml-mermaid-classdiagram-relations.png
../../_images/uml-relations.png

3.3.2. Inheritance

Simple Inheritance:

class Vehicle:
    def engine_start():
        pass

    def engine_stop():
        pass


class Car(Vehicle):
    pass

class Truck(Vehicle):
    pass

class Motorcycle(Vehicle):
    pass
```mermaid
classDiagram
    Vehicle <|-- Car
    Vehicle <|-- Truck
    Vehicle <|-- Motorcycle

    class Vehicle {
        engine_start()
        engine_stop()
    }
```
../../_images/uml-relations-inheritance-simple.png

Multilevel Inheritance:

class Vehicle:
    def engine_start():
        pass

    def engine_stop():
        pass

class HasWindows(Vehicle):
    def window_open():
        pass

    def window_close():
        pass


class Car(HasWindows):
    pass

class Truck(HasWindows):
    pass

class Motorcycle(Vehicle):
    pass
```mermaid
classDiagram
    Vehicle <|-- Motorcycle
    Vehicle <|-- HasWindows
    HasWindows <|-- Car
    HasWindows <|-- Truck

    class Vehicle {
        engine_start()
        engine_stop()
    }

    class HasWindows {
        window_open()
        window_close()
    }
```
../../_images/uml-relations-inheritance-multilevel.png

3.3.3. Composition

class Vehicle:
    def engine_start():
        pass

    def engine_stop():
        pass

class HasWindows:
    def window_open():
        pass

    def window_close():
        pass


class Car(Vehicle, HasWindows):
    pass

class Truck(Vehicle, HasWindows):
    pass

class Motorcycle(Vehicle):
    pass
```mermaid
classDiagram
    HasWindows --* Car
    HasWindows --* Truck

    Vehicle --* Car
    Vehicle --* Truck
    Vehicle --* Motorcycle

    class Vehicle {
        engine_start()
        engine_stop()
    }

    class HasWindows {
        window_open()
        window_close()
    }
```
../../_images/uml-relations-composition.png

3.3.4. Aggregation

class Mission:
    year: int
    name: str


class Astronaut:
    firstname: str
    lastname: str
    mission: list[Mission]
```mermaid
classDiagram
    Astronaut *--|> Mission

    class Astronaut {
        firstname: str
        lastname: str
        missions: list[Mission]
    }

    class Mission {
        year: int
        name: str
    }
```
../../_images/uml-relations-aggregation.png

3.3.5. Dependency

  • Somewhere in Shape class you are using Document class

class Spaceship:
    name: str


class Astronaut:
    firstname: str
    lastname: str

    def enter(spaceship: Spaceship):
        pass
```mermaid
classDiagram
    Astronaut ..> Spaceship

    class Astronaut {
        firstname: str
        lastname: str
        enter(spaceship: Spapceship)
    }

    class Spaceship {
        name: str
    }
```
../../_images/uml-relations-dependency.png