← Back to all products
$29
Python Design Patterns
23 GoF patterns implemented in idiomatic Python with real-world examples, type hints, and unit tests.
JSONMarkdownPython
📄 Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample📁 File Structure 17 files
python-design-patterns/
├── LICENSE
├── README.md
├── guides/
│ └── when-to-use-patterns.md
├── patterns/
│ ├── behavioral/
│ │ ├── chain_of_responsibility.py
│ │ ├── command.py
│ │ ├── observer.py
│ │ └── strategy.py
│ ├── concurrency/
│ │ ├── circuit_breaker.py
│ │ └── producer_consumer.py
│ ├── creational/
│ │ ├── builder.py
│ │ ├── factory.py
│ │ └── singleton.py
│ └── structural/
│ ├── adapter.py
│ ├── decorator.py
│ └── facade.py
└── tests/
└── test_patterns.py
📖 Documentation Preview README excerpt
Python Design Patterns — Production-Ready Implementations
12 essential design patterns implemented in modern Python with type hints, tests, and a decision guide.
What You Get
- 12 pattern implementations across creational, structural, behavioral, and concurrency categories
- Thread-safe singleton, async producer/consumer, and circuit breaker patterns
- Modern Python — dataclasses, Protocol,
functools.wraps,asyncio.Queue - Comprehensive tests covering every pattern with real-world scenarios
- Decision guide — know exactly when to reach for which pattern
File Tree
python-design-patterns/
├── README.md
├── manifest.json
├── LICENSE
├── patterns/
│ ├── creational/
│ │ ├── singleton.py # Thread-safe singleton via metaclass
│ │ ├── factory.py # Abstract factory with registration
│ │ └── builder.py # Fluent builder with validation
│ ├── structural/
│ │ ├── adapter.py # Third-party API adapter
│ │ ├── decorator.py # Function/class decorators
│ │ └── facade.py # Subsystem facade
│ ├── behavioral/
│ │ ├── observer.py # Type-safe event system
│ │ ├── strategy.py # Strategy via Protocol
│ │ ├── command.py # Command with undo/redo stack
│ │ └── chain_of_responsibility.py
│ └── concurrency/
│ ├── producer_consumer.py # asyncio.Queue pipeline
│ └── circuit_breaker.py # Configurable circuit breaker
├── tests/
│ └── test_patterns.py
└── guides/
└── when-to-use-patterns.md
Getting Started
Singleton — one instance, thread-safe
from patterns.creational.singleton import Singleton
class AppConfig(metaclass=Singleton):
def __init__(self) -> None:
self.debug = False
a = AppConfig()
b = AppConfig()
assert a is b # Same instance
Factory — register and create objects by key
... continues with setup instructions, usage examples, and more.
📄 Code Sample .py preview
patterns/creational/singleton.py"""Thread-safe Singleton pattern using a metaclass.
Usage:
class AppConfig(metaclass=Singleton):
def __init__(self) -> None:
self.debug = False
a = AppConfig()
b = AppConfig()
assert a is b
"""
from __future__ import annotations
import threading
from typing import Any, ClassVar
class Singleton(type):
"""Metaclass that ensures only one instance of a class exists.
Thread-safe via a per-class lock — safe for use in multi-threaded
applications without external synchronization.
"""
_instances: ClassVar[dict[type, Any]] = {}
_lock: ClassVar[threading.Lock] = threading.Lock()
def __call__(cls, *args: Any, **kwargs: Any) -> Any:
if cls not in cls._instances:
with cls._lock:
# Double-checked locking
if cls not in cls._instances:
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]
@classmethod
def reset(mcs, cls: type) -> None:
"""Remove a singleton instance (useful for testing).