Gang of Four patterns implemented the Pythonic way — Creational, Structural, Behavioral, plus SOLID principles.
How objects are created — Singleton, Factory, Builder, Prototype, Abstract Factory
copy.How objects are composed — Adapter, Decorator, Facade, Proxy
How objects communicate — Observer, Strategy, Command, Iterator, State
yield makes this trivial.Python-specific patterns using dunder methods, context managers, and metaclasses
@property for computed attributes.Five principles of object-oriented design — the foundation of clean architecture
Quick reference — which pattern to use when
| Problem | Pattern | Python Shortcut |
|---|---|---|
| Need exactly one instance | Singleton | Module-level variable |
| Create objects without knowing class | Factory | Dict of classes |
| Build complex objects step by step | Builder | @dataclass with defaults |
| Clone existing objects | Prototype | copy.deepcopy() |
| Make incompatible interfaces work | Adapter | Wrapper class |
| Add behavior dynamically | Decorator | @decorator syntax |
| Simplify complex subsystem | Facade | Wrapper function/class |
| Control access to object | Proxy | @property, __getattr__ |
| Notify on state changes | Observer | Callback lists, signals |
| Swap algorithms at runtime | Strategy | Dict of functions |
| Undo/redo operations | Command | Command objects + history stack |
| Traverse collection | Iterator | yield (generator) |
| Object behaves differently per state | State | State classes |
| Auto cleanup resources | Context Manager | with + __enter__/__exit__ |