This directory contains idiomatic Python implementations of classic software design patterns. The focus is on readability, explicit behavior, and testability, using Python’s dynamic features responsibly.
Each pattern is implemented independently and validated with unit tests using pytest.
python/
├── src/
│ ├── behavioral/ # Behavioral design patterns
│ ├── creational/ # Creational design patterns
│ └── structural/ # Structural design patterns
├── tests/
│ ├── behavioral/ # Tests for behavioral patterns
│ ├── creational/ # Tests for creational patterns
│ └── structural/ # Tests for structural patterns
├── pyproject.toml # Tooling configuration (pytest, black, mypy, etc.)
├── requirements.txt # Python dependencies
├── Makefile # Test and tooling automation
└── README.md # This file
Each pattern directory contains:
- One or more implementation modules
- Corresponding test modules
- Minimal, explicit dependencies
Behavioral patterns focus on object interaction and responsibility.
Creational patterns address object creation while keeping usage flexible and explicit.
Structural patterns focus on composing objects into larger structures.
Python requires an explicit virtual environment.
Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # Linux / macOS
venv\Scripts\activate # WindowsInstall dependencies:
make installAll Makefile commands assume an active virtual environment.
All implementations are validated using pytest.
Run tests:
make testVerbose output:
pytest -vvGenerate a coverage report:
pytest --cov=src --cov-report=xmlThe coverage report will be generated as coverage.xml.
A Makefile is provided to standardize common tasks:
make install # Install dependencies
make format # Format code (black)
make lint # Static type checking (mypy)
make test # Run tests with coverage
make clean # Remove caches and artifacts- Pythonic and readable code
- Explicit is better than implicit
- Favor composition and clear data flow
- Minimal abstractions
- Tests as first-class citizens
This directory is intended as a reference and learning resource for applying classic design patterns in Python while respecting Python’s design philosophy.