A library demonstrating the 23 Gang of Four design patterns implemented in Python with modern idioms and type hints.
- Factory - Create objects without specifying exact class
- Singleton - Ensure only one instance exists
- Builder - Construct complex objects step by step
- Prototype - Clone objects instead of creating new ones
- Abstract Factory - Create families of related objects
- Strategy - Define interchangeable algorithm families
- Observer - Notify dependents of state changes
- Command - Encapsulate requests as objects
- Chain of Responsibility - Pass requests along handler chain
- Interpreter - Interpret language grammar
- State - Alter behavior when internal state changes
- Template Method - Define algorithm skeleton in base class
- Iterator - Access elements sequentially
- Visitor - Separate algorithms from object structure
- Mediator - Reduce coupling between communicating objects
- Memento - Capture and restore object state
- Decorator - Add responsibilities dynamically
- Adapter - Make incompatible interfaces work together
- Composite - Treat individual and composed objects uniformly
- Facade - Provide simplified interface to complex subsystem
- Proxy - Control access to another object
- Bridge - Decouple abstraction from implementation
- Composition/Inheritance - Demonstrate OOP principles
Full documentation available at: https://sorenwacker.github.io/DesignPatterns/
- Introduction
- Overview
- Design Patterns
- Pattern Selection Guide
- Pattern Comparison Guide
- Practical Examples
- Anti-Patterns
- Quick Reference
# Using uv (recommended)
uv pip install -e .
# Using pip
pip install -e .Run examples:
python examples/factory_logger.py
python examples/strategy_payment.pyUse in code:
from design_patterns.creational.factory import AnimalFactory
from design_patterns.behavioral.strategy import ShoppingCart, CreditCardPayment
factory = AnimalFactory()
dog = factory.get_animal("dog", "Buddy")
cart = ShoppingCart()
cart.set_payment_strategy(CreditCardPayment("1234-5678"))Run tests:
pytestGenerate documentation locally:
mkdocs serveMIT License - see LICENSE.