2.9. UML Class Diagram

2.9.1. Attributes

>>> class User:
...     firstname: str
...     lastname: str
```mermaid
classDiagram

class User {
    firstname: str
    lastname: str
}
```
../../_images/uml-classdiagram-attributes.png
@startuml

class User {
    + firstname: str
    + lastname: str
    + age: int
}

@enduml

2.9.2. Methods Without Parameters

>>> class User:
...     def say_hello(self):
...         pass
```mermaid
classDiagram

class User {
    say_hello()
}
```
../../_images/uml-classdiagram-method-noparams.png

2.9.3. Methods With Parameters

>>> class User:
...     def say_hello(self, firstname: str, lastname: str):
...         pass
```mermaid
classDiagram

class User {
    say_hello(firstname: str, lastname: str)
}
```
../../_images/uml-classdiagram-method-params.png

2.9.4. Method Return Type

>>> class User:
...     def say_hello(self) -> str:
...         pass
```mermaid
classDiagram

class User {
    say_hello() str
}
```
../../_images/uml-classdiagram-method-return.png

2.9.5. Abstract Methods

>>> from abc import ABC, abstractmethod
>>>
>>>
>>> class User(ABC):
...     @abstractmethod
...     def say_hello(self):
...         pass
```mermaid
classDiagram

class User {
    say_hello()*
}
```
../../_images/uml-classdiagram-method-abstract.png

2.9.6. Static Methods

>>> class User:
...     @staticmethod
...     def say_hello():
...         pass
```mermaid
classDiagram

class User {
    say_hello()$
}
```
../../_images/uml-classdiagram-method-static.png

2.9.7. Types

>>> class User:
...     firstname: str
...     lastname: str
...
...     def say_hello(self, name: str) -> str:
...         pass
```mermaid
classDiagram

class User {
    firstname: str
    lastname: str

    say_hello(name: str) str
}
```
../../_images/uml-classdiagram-types.png

2.9.8. Access Modifiers

  • + - Public

  • - - Private

  • # - Protected

  • ~ - Package/Internal

2.9.9. Access Modifiers - Public

>>> class User:
...     firstname: str
...     lastname: str
...
...     def say_hello(self) -> str:
...         pass
```mermaid
classDiagram

class User {
    +firstname: str
    +lastname: str
    +say_hello() str
}
```
../../_images/uml-classdiagram-accessmodifiers-public.png

2.9.10. Access Modifiers - Protected

>>> class User:
...     _firstname: str
...     _lastname: str
...
...     def _say_hello(self) -> str:
...         pass
```mermaid
classDiagram

class User {
    #firstname: str
    #lastname: str
    #say_hello() str
}
```
../../_images/uml-classdiagram-accessmodifiers-protected.png

2.9.11. Access Modifiers - Private

>>> class User:
...     __firstname: str
...     __lastname: str
...
...     def __say_hello(self) -> str:
...         pass
```mermaid
classDiagram

class User {
    -firstname: str
    -lastname: str
    -say_hello() str
}
```
../../_images/uml-classdiagram-accessmodifiers-private.png

2.9.12. Django

$ brew install graphviz
$ pip install pydotplus
$ pip install django-extensions

# Add 'django_extensions' to INSTALLED_APP

$ python manage.py graph_models -a -g -o all.png
$ python manage.py graph_models myapp -g -o myapp.png
$ python manage.py graph_models -a -I Contact,Address -o models.png
$ python manage.py graph_models -a --arrow-shape normal -o myproject.png
../../_images/uml-django-models-1.png
../../_images/uml-django-models-2.png
../../_images/uml-django-models-3.png
../../_images/uml-django-models-4.png
../../_images/uml-django-models-5.png
../../_images/uml-django-models-6.png

2.9.13. Use Case - 1

../../_images/uml-usecase-bank.png
@startuml

class Bank {
    - name
    - bic
    - swift
    - atms
    + manages()
    + maintains()
}

class Customer {
    - firstname
    - lastname
    - birthdate
    + login()
    + logout()
}

class Address {
    - street
    - city
    - postcode
    - country
}

class Card {
    # customer
    # number
    # cvv
    # expiration
    # pin
    + change_pin()
    - create_transaction()
}

class CreditCard {
    - limit
    - interest_rate
}

class DebitCard

class ATM {
    - address
    + check_balance()
    + withdraw()
    - create_transaction()
}

class Transaction {
    - id
    - date
    - type
    - amount
    + create()
    + commit()
    + rollback()
}

class Account {
    # number
    # iban
    # balance
    + deposit()
    # create_transaction()
}

class CurrentAccount {
    + withdraw()
    + transfer()
}

class SavingAccount {
    - interest_rate
    + yield_interest()
}

Address <.. ATM
Address <.. Bank
Address <.. Customer

Bank o-- ATM : "0...*"
Bank o-- Account : "0...*"

Account o-- Card : "0...*"
Customer o-- Account : "1...*"

Account <|-- CurrentAccount
Account <|-- SavingAccount

Transaction <.right. ATM
Transaction <.. Account
Transaction <.. Card

Card <|-- CreditCard
Card <|-- DebitCard

@enduml
from decimal import Decimal
from datetime import date, datetime


class Address:
    street: str
    city: str
    postcode: str
    country: str


class Bank:
    name: str
    bic: str
    swift: str
    address: Address
    def manages(self): ...
    def maintains(self): ...


class Customer:
    firstname: str
    lastname: str
    birthdate: date
    address: Address
    def login(self): ...
    def logout(self): ...


class Card:
    customer: Customer
    number: int
    cvv: int
    expiration: date
    pin: int
    def change_pin(self): ...
    def create_transaction(self): ...


class CreditCard(Card):
    limit: Decimal
    interest_rate: float


class DebitCard(Card):
    pass


class ATM:
    bank: Bank
    address: Address
    def check_balance(self): ...
    def withdraw(self): ...
    def create_transaction(self): ...


class Transaction:
    id: str
    date: datetime
    type: str
    amount: Decimal
    def create(self): ...
    def commit(self): ...
    def rollback(self): ...


class Account:
    number: int
    iban: str
    balance: Decimal
    def deposit(self): ...
    def create_transaction(self): ...


class CurrentAccount:
    def withdraw(self): ...
    def transfer(self): ...


class SavingAccount:
    interest_rate: float
    def yield_interest(self): ...

2.9.14. Use Case - 2

  • Boxes and Arrows

../../_images/uml-classdiagram-usecase-01.jpg

2.9.15. Use Case - 3

../../_images/uml-classdiagram-usecase-02.png

2.9.16. Use Case - 4

../../_images/uml-classdiagram-usecase-03.png

2.9.17. Use Case - 5

../../_images/uml-classdiagram-usecase-04.png

2.9.18. Use Case - 6

../../_images/uml-classdiagram-usecase-05.png

2.9.19. Use Case - 7

../../_images/uml-classdiagram-usecase-06.png

2.9.20. Use Case - 8

../../_images/uml-classdiagram-usecase-07.png

2.9.21. Use Case - 9

../../_images/uml-classdiagram-usecase-08.jpg

2.9.22. Use Case - 10

../../_images/uml-classdiagram-usecase-09.jpg

2.9.23. Use Case - 11

../../_images/uml-classdiagram-usecase-10.png