๐Ÿ Design patterns, taught in Python

Design patterns and SOLID principles, in idiomatic Python.

The Gang of Four patterns and the SOLID principles, sorted three ways: the patterns that dissolve into Python language features, the patterns that survive as design decisions, and the names that need disambiguating, headlined by the Decorator pattern and the @decorator syntax. Each pattern is taught as a decision under forces, with new shipping-domain Python 3.15 code behind it and a pytest suite, checked by mypy in strict mode, proving each example does what the chapter claims. Read it straight through, or jump to the part that answers the question you have right now.

Python 3.15 mypy --strict SOLID GoF patterns pytest

The parts

Fourteen parts and the appendices, read in order the first time. Each part page lists its chapters; each chapter stands on its own for a reader jumping in to solve one problem. The first part sets up the toolchain every example runs on.

Part 0

Orientation

The toolchain every example runs on (uv, ruff, mypy, pytest) and the consignment example that proves it works.

1 chapterExplore โ†’
Part 1

Why This Is Not a GoF Retelling

Patterns as decisions under forces, and the claim that several 1994 catalogue entries are now Python language features rather than designs you write.

2 chaptersExplore โ†’
Part 2

Why Python Is Different

The map that sorts every catalogue pattern into those a language feature dissolves, those that remain design, and those whose name means two things in Python, followed by naming patterns so that teams and AI assistants read the same contract.

2 chaptersExplore โ†’
Part 3

Principles: SOLID

The five SOLID principles in Python: single responsibility, open/closed and Liskov substitution, then interface segregation and dependency inversion without a framework.

2 chaptersExplore โ†’
Part 4

The Python Substrate

The data model and dunder methods, dataclasses and value semantics, Protocol and structural typing with mypy, context managers, and proving a design works with pytest, pytest-mock, and mypy.

5 chaptersExplore โ†’
Part 5

Selecting and Varying Behaviour

Strategy as a callable, Template Method and its function alternative, State and finite state machines, and Command with undo and command queues.

4 chaptersExplore โ†’
Part 6

Creating Objects Under Control

Factory Method and Abstract Factory, Builder with dataclasses and Prototype, and Singleton as a module, a Borg, or a metaclass.

3 chaptersExplore โ†’
Part 7

Composing and Adapting Structure

Adapter, Facade, and Bridge; the Decorator pattern told apart from the @decorator syntax, with Proxy; and Composite with Flyweight.

3 chaptersExplore โ†’
Part 8

Coordinating and Traversing

Observer, Mediator and Chain of Responsibility, Iterator through generators and the iterator protocol, and Visitor, Memento, and Interpreter.

4 chaptersExplore โ†’
Part 9

Idiomatic Python

Decorators done right, descriptors and properties, singledispatch, metaclasses and __init_subclass__, and dynamic attributes with __getattr__.

5 chaptersExplore โ†’
Part 10

Patterns in the Wild

The shortcuts Python offers in place of full patterns (dict dispatch, function tables, EAFP) and the anti-patterns that come from misusing the catalogue.

2 chaptersExplore โ†’
Part 11

Changing Inherited Code

Seams in Python, dependency-breaking techniques, Fowler and Feathers contrasted and combined, and the decision between refactoring and rewriting.

4 chaptersExplore โ†’
Part 12

Concurrency

Two chapters on the GIL and performance, and on how asyncio reshapes the patterns.

2 chaptersExplore โ†’
Part 13

Putting It Together

A final project that combines the patterns, and working with AI and LLM tools on pattern-heavy code.

2 chaptersExplore โ†’
Part 14

Appendices

Reference material: build and toolchain, pytest, mocking, xUnit test patterns, a pattern-to-force quick reference, and programming principles reviewed in code.

6 chaptersExplore โ†’