← Back to all products

Python Design Patterns

$29

23 GoF patterns implemented in idiomatic Python with real-world examples, type hints, and unit tests.

📁 17 files🏷 v1.0.0
PythonMarkdownJSON

📁 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

tests/test_patterns.py """Tests for all 12 design patterns.""" from __future__ import annotations import asyncio import json import pytest # ─── Creational ────────────────────────────────────────────── class TestSingleton: def test_same_instance(self) -> None: from patterns.creational.singleton import Singleton class Config(metaclass=Singleton): pass a, b = Config(), Config() assert a is b Singleton.reset(Config) def test_reset(self) -> None: from patterns.creational.singleton import Singleton class Svc(metaclass=Singleton): pass a = Svc() Singleton.reset(Svc) b = Svc() assert a is not b Singleton.reset(Svc) class TestFactory: def test_register_and_create(self) -> None: from patterns.creational.factory import Factory class Animals(Factory): pass @Animals.register("dog") class Dog: sound = "woof" obj = Animals.create("dog") assert obj.sound == "woof" # ... 239 more lines ...