2.9. UML Class Diagram
2.9.1. Attributes
>>> class User:
... firstname: str
... lastname: str
```mermaid
classDiagram
class User {
firstname: str
lastname: str
}
```
@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()
}
```
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)
}
```
2.9.4. Method Return Type
>>> class User:
... def say_hello(self) -> str:
... pass
```mermaid
classDiagram
class User {
say_hello() str
}
```
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()*
}
```
2.9.6. Static Methods
>>> class User:
... @staticmethod
... def say_hello():
... pass
```mermaid
classDiagram
class User {
say_hello()$
}
```
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
}
```
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
}
```
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
}
```
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
}
```
2.9.12. Django
GraphViz: https://graphviz.org/
Django Extensions: https://django-extensions.readthedocs.io/en/latest/graph_models.html#example-usage
$ 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
2.9.13. Use Case - 1
@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