2.5. UML Class Diagram

2.5.1. Attributes

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

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

2.5.2. Methods Without Parameters

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

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

2.5.3. Methods With Parameters

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

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

2.5.4. Method Return Type

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

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

2.5.5. Abstract Methods

>>> from abc import abstractmethod, ABCMeta
>>>
>>>
>>> class Astronaut(metaclass=ABCMeta):
...     @abstractmethod
...     def say_hello(self):
...         pass
```mermaid
classDiagram

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

2.5.6. Static Methods

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

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

2.5.7. Types

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

class Astronaut {
    firstname: str
    lastname: str

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

2.5.8. Access Modifiers

  • + - Public

  • - - Private

  • # - Protected

  • ~ - Package/Internal

2.5.9. Access Modifiers - Public

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

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

2.5.10. Access Modifiers - Protected

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

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

2.5.11. Access Modifiers - Private

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

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

2.5.12. Boxes and Arrows

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

2.5.13. Use Cases

../../_images/uml-classdiagram-usecase-02.png
../../_images/uml-classdiagram-usecase-03.png
../../_images/uml-classdiagram-usecase-04.png
../../_images/uml-classdiagram-usecase-05.png
../../_images/uml-classdiagram-usecase-06.png
../../_images/uml-classdiagram-usecase-07.png
../../_images/uml-classdiagram-usecase-08.jpg
../../_images/uml-classdiagram-usecase-09.jpg
../../_images/uml-classdiagram-usecase-10.png

2.5.14. 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.5.15. Generate from code

$ pyreverse -o png -p mymodule .