2.7. UML Relationship
Arrow with empty triangle (but on picture is filled)
<|--
- Inheritance*--
- Compositiono--
- Aggregation-->
- Association--
- Link (Solid)..>
- Dependency..|>
- Realization..
- Link (Dashed)
2.7.1. Inheritance
>>> class Vehicle:
... def engine_start(self): pass
... def engine_stop(self): 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()
}
```
2.7.2. Linear Inheritance
>>> class Vehicle:
... def engine_start(self): pass
... def engine_stop(self): pass
>>>
>>> class VehicleWithWindows(Vehicle):
... def window_open(self): pass
... def window_close(self): pass
>>>
>>>
>>> class Car(VehicleWithWindows):
... pass
>>>
>>> class Truck(VehicleWithWindows):
... pass
>>>
>>> class Motorcycle(Vehicle):
... pass
```mermaid
classDiagram
Vehicle <|-- Motorcycle
Vehicle <|-- VehicleWithWindows
VehicleWithWindows <|-- Car
VehicleWithWindows <|-- Truck
class Vehicle {
engine_start()
engine_stop()
}
class VehicleWithWindows {
window_open()
window_close()
}
```
2.7.3. Multiple Inheritance
Also know as Mixin Classes
>>> class Vehicle:
... pass
>>>
>>> class HasEngine:
... def engine_start(self): pass
... def engine_stop(self): pass
>>>
>>> class HasWindows:
... def window_open(self): pass
... def window_close(self): pass
>>>
>>> class Car(Vehicle, HasEngine, HasWindows):
... pass
>>>
>>> class Truck(Vehicle, HasEngine, HasWindows):
... pass
>>>
>>> class Motorcycle(Vehicle, HasEngine):
... 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()
}
```
2.7.4. Composition
>>> class Vehicle:
... pass
>>>
>>> class Engine:
... def engine_start(self): pass
... def engine_stop(self): pass
>>>
>>> class Windows:
... def window_open(self): pass
... def window_close(self): pass
>>>
>>>
>>> class Car(Vehicle):
... engine: Engine
... window: Windows
>>>
>>> class Truck(Vehicle):
... engine: Engine
... window: Windows
>>>
>>> class Motorcycle(Vehicle):
... engine: Engine
```mermaid
classDiagram
class Engine {
engine_start()
engine_stop()
}
class Car {
engine: Engine
}
class Truck {
engine: Engine
}
class Motorcycle {
engine: Engine
}
```
2.7.5. Aggregation
>>> class Group:
... gid: int
... name: str
>>>
>>>
>>> class User:
... firstname: str
... lastname: str
... groups: list[Group]
>>> class Vehicle:
... pass
>>>
>>> class Part:
... pass
>>>
>>> class Engine(Part):
... def engine_start(self): pass
... def engine_stop(self): pass
>>>
>>> class Windows(Part):
... def window_open(self): pass
... def window_close(self): pass
>>>
>>>
>>> class Car(Vehicle):
... parts: list[Part] = [Engine, Windows]
>>>
>>> class Truck(Vehicle):
... parts: list[Part] = [Engine, Windows]
>>>
>>> class Motorcycle(Vehicle):
... parts: list[Part] = [Engine]
```mermaid
classDiagram
class User {
firstname: str
lastname: str
groups: list[Group]
}
class Group {
gid: int
name: str
}
User *--|> Group
```
2.7.6. Dependency
Somewhere in
User
class you are usingSpaceship
class
>>> class Group:
... name: str
>>>
>>>
>>> class User:
... firstname: str
... lastname: str
...
... def join(group: Group):
... pass
```mermaid
classDiagram
User ..> Group
class User {
firstname: str
lastname: str
join(group: Group)
}
class Group {
name: str
}
```
2.7.7. Cardinality
0
- Exactly 00..1
- Zero or one0..n
- Zero to n (where n > 1)0..*
- Zero or more1
- Only one1..n
- One to n (where n > 1)1..*
- One or more*
- Manyn..n
- {where n>1}