课程 · 12 · 10 / 12
Design Patterns in Python: Elegant Solutions
Implement classic design patterns the Pythonic way. Master Factory, Strategy, Observer, Decorator, and Dependency Injection patterns.
TIPLearning Objectives: After this lesson, you'll implement classic design patterns the Pythonic way, understand when to use each pattern, and build flexible, maintainable code using Factory, Strategy, Observer, Decorator, and Dependency Injection patterns.
Why Design Patterns?
Design patterns are reusable solutions to common programming problems.
Patterns in Python
Python's flexibility often simplifies traditional patterns:
- First-class functions replace Strategy classes
- Duck typing reduces the need for interfaces
- Decorators are built into the language
- Multiple inheritance provides mixins
The classic Gang of Four patterns fall into three categories:
| Category | What it governs | Examples |
|---|---|---|
| Creational | How objects are created | Factory, Singleton, Builder |
| Structural | How objects compose | Adapter, Decorator, Facade |
| Behavioral | How objects communicate | Strategy, Observer, Command |
A few Python-specific considerations to keep in mind:
- Use functions where Java uses single-method interfaces
- Use protocols for duck typing
- Leverage
__dunder__methods - Keep it simple (YAGNI)
Creational Patterns
Factory Pattern
Singleton Pattern
Builder Pattern
Behavioral Patterns
Strategy Pattern
Observer Pattern
Command Pattern
Structural Patterns
Decorator Pattern (Structural)
Adapter Pattern
Dependency Injection
Practice Exercises
Exercise 1: Plugin System
Exercise 2: State Machine
Key Takeaways
| Pattern | Use When |
|---|---|
| Factory | Create objects without specifying exact class |
| Singleton | Ensure only one instance exists |
| Builder | Construct complex objects step by step |
| Strategy | Select algorithms at runtime |
| Observer | One-to-many notifications |
| Command | Encapsulate actions as objects (undo/redo) |
| Decorator | Add behavior dynamically |
| Adapter | Make incompatible interfaces work together |
| DI | Decouple dependencies for testing |
Pythonic Alternatives
| Traditional | Pythonic |
|---|---|
| Strategy class | Function or lambda |
| Factory class | Factory function |
| Singleton class | Module-level instance |
| Iterator class | Generator function |
| Decorator class | @decorator function |
Next Steps
In the next lesson, we'll explore Packaging and Distribution—modern Python packaging with pyproject.toml, dependency management, and publishing to PyPI.
Ready to ship your code? Packaging awaits!
Further Reading
Why Patterns in Python Are Different
- Peter Norvig — Design Patterns in Dynamic Languages — the seminal essay. Roughly: 16 of 23 GoF patterns disappear or simplify dramatically in Python.
- Python Patterns Guide — Brandon Rhodes. Patterns implemented the Pythonic way, not the Java way.
- Refactoring.Guru — Design Patterns in Python — the GoF patterns with diagrams and Python implementations.
Modern Patterns You Should Actually Use
- Dependency Injection in Python —
dependency-injectorlibrary, or pure-Python protocols. - Strategy → first-class function — Python doesn't need a
Strategyclass; pass a function. - Singleton → module — Python modules are already singletons. Stop writing the pattern.
- Observer → events / signals —
blinkerlibrary, orasyncioevent loop. - Builder →
dataclass/pydantic— declarative builders for free.
Architecture (Beyond Patterns)
- Architecture Patterns with Python — Percival & Gregory, free online. Repository / Service-Layer / Unit-of-Work patterns done right for Python.
Books
- Book: Architecture Patterns with Python — Harry Percival & Bob Gregory (free online). The single best architecture book for working Python developers.
- Book: Fluent Python (2nd ed.) — Chapter 10 ("Design Patterns with First-Class Functions") makes the case for the Pythonic refactor.