Cover....1
Title Page....2
Copyright....3
Dedication....4
Contributors....5
Table of Contents....8
Preface....14
Part 1: Start with Principles....20
Chapter 1: Foundational Design Principles....22
Technical requirements....23
Following the Encapsulate What Varies principle....23
What does it mean?....23
Benefits....23
Techniques for achieving encapsulation....24
An example – encapsulating using polymorphism....25
An example – encapsulating using a property....26
Following the Favor Composition Over Inheritance principle....28
What does it mean?....28
Benefits....29
Techniques for composition....29
An example – compose a car using the engine....29
Following the Program to Interfaces, Not Implementations principle....31
What does it mean?....31
Benefits....31
Techniques for interfaces....31
An example – different types of logger....33
An example – different types of logger, now using Protocols....35
Following the Loose Coupling principle....37
What does it mean?....37
Benefits....37
Techniques for loose coupling....37
An example – a message service....38
Summary....40
Chapter 2: SOLID Principles....42
Technical requirements....42
SRP....42
An example of software design following the SRP....43
OCP....45
An example of design following the OCP....46
LSP....49
An example of design following the LSP....49
ISP....52
An example of design following the ISP....52
DIP....55
An example of design following the ISP....56
Summary....58
Part 2: From the Gang of Four....60
Chapter 3: Creational Design Patterns....62
Technical requirements....63
The factory pattern....63
The factory method....63
The abstract factory pattern....72
The builder pattern....77
Real-world examples....78
Comparison with the factory pattern....78
Use cases for the builder pattern....78
Implementing the builder pattern....78
The prototype pattern....83
Real-world examples....83
Use cases for the prototype pattern....84
Implementing the prototype pattern....84
The singleton pattern....88
Real-world examples....88
Use cases for the singleton pattern....89
Implementing the singleton pattern....89
Should you use the singleton pattern?....92
The object pool pattern....92
Real-world examples....92
Use cases for the object pool pattern....93
Implementing the object pool pattern....93
Summary....95
Chapter 4: Structural Design Patterns....96
The adapter pattern....96
Real-world examples....97
Use cases for the adapter pattern....97
Implementing the adapter pattern – adapt a legacy class....97
Implementing the adapter pattern – adapt several classes into a unified interface....99
The decorator pattern....102
Real-world examples....103
Use cases for the decorator pattern....103
Implementing the decorator pattern....104
The bridge pattern....108
Real-world examples....109
Use cases for the bridge pattern....109
Implementing the bridge pattern....109
The facade pattern....112
Real-world examples....112
Use cases for the facade pattern....113
Implementing the facade pattern....113
The flyweight pattern....117
Real-world examples....117
Use cases for the flyweight pattern....118
Implementing the flyweight pattern....118
The proxy pattern....122
Real-world examples....122
Use cases for the proxy pattern....123
Implementing the proxy pattern – a virtual proxy....123
Implementing the proxy pattern – a protection proxy....126
Implementing the proxy pattern – a remote proxy....128
Implementing the proxy pattern – a smart proxy....130
Summary....133
Chapter 5: Behavioral Design Patterns....136
Technical requirements....137
The Chain of Responsibility pattern....137
Real-world examples....137
Use cases for the Chain of Responsibility pattern....138
Implementing the Chain of Responsibility pattern....138
The Command pattern....142
Real-world examples....142
Use cases for the Command pattern....143
Implementing the Command pattern....143
The Observer pattern....147
Real-world examples....147
Use cases for the Observer pattern....148
Implementing the Observer pattern....148
The State pattern....151
Real-world examples....152
Use cases for the State pattern....153
Implementing the State pattern....153
The Interpreter pattern....159
Real-world examples....159
Use cases for the Interpreter pattern....159
Implementing the Interpreter pattern....160
The Strategy pattern....163
Real-world examples....164
Use cases for the Strategy pattern....164
Implementing the Strategy pattern....164
The Memento pattern....168
Real-world examples....168
Use cases for the Memento pattern....169
Implementing the Memento pattern....169
The Iterator pattern....172
Use cases for the Iterator pattern....172
Implementing the Iterator pattern....173
The Template pattern....175
Real-world examples....175
Use cases for the Template pattern....175
Implementing the Template pattern....176
Other behavioral design patterns....178
Summary....178
Part 3: Beyond the Gang of Four....180
Chapter 6: Architectural Design Patterns....182
Technical requirements....182
The MVC pattern....183
Real-world examples....184
Use cases for the MVC pattern....185
Implementing the MVC pattern....186
The Microservices pattern....188
Real-world examples....189
Use cases for the Microservices pattern....190
Implementing the microservices pattern – a payment service using gRPC....190
Implementing the microservices pattern – an LLM service using Lanarky....193
The Serverless pattern....196
Real-world examples....196
Use cases for the Serverless pattern....197
Implementing the Serverless pattern....197
The Event Sourcing pattern....199
Real-world examples....199
Use cases for the Event Sourcing pattern....199
Implementing the event sourcing pattern – the manual way....200
Implementing the Event Sourcing pattern – using a library....202
Other architectural design patterns....204
Summary....204
Chapter 7: Concurrency and Asynchronous Patterns....206
Technical requirements....206
The Thread Pool pattern....207
Real-world examples....207
Use cases for the Thread Pool pattern....208
Implementing the Thread Pool pattern....208
The Worker Model pattern....210
Real-world examples....210
Use cases for the Worker Model pattern....210
Implementing the Worker Model pattern....211
The Future and Promise pattern....213
Real-world examples....213
Use cases for the Future and Promise pattern....214
Implementing the Future and Promise pattern – using concurrent.futures....215
Implementing the Future and Promise pattern – using asyncio....216
The Observer pattern in reactive programming....218
Real-world examples....218
Use cases for the Observer pattern in reactive programming....219
Implementing the Observer pattern in reactive programming....219
Other concurrency and asynchronous patterns....222
Summary....223
Chapter 8: Performance Patterns....224
Technical requirements....224
The Cache-Aside pattern....225
Real-world examples....225
Use cases for the cache-aside pattern....225
Implementing the cache-aside pattern....225
The Memoization pattern....231
Real-world examples....232
Use cases for the memoization pattern....232
Implementing the memoization pattern....232
The Lazy Loading pattern....234
Real-world examples....234
Use cases for the lazy loading pattern....234
Implementing the lazy loading pattern – lazy attribute loading....235
Implementing the lazy loading pattern – using caching....236
Summary....238
Chapter 9: Distributed Systems Patterns....240
Technical requirements....240
The Throttling pattern....241
Real-world examples....241
Use cases for the Throttling pattern....242
Implementing the Throttling pattern....242
The Retry pattern....245
Real-world examples....245
Use cases for the Retry pattern....246
Implementing the Retry pattern....246
The Circuit Breaker pattern....248
Real-world examples....248
Use cases for the Circuit Breaker pattern....249
Implementing the Circuit Breaker pattern....249
Other distributed systems patterns....251
Summary....251
Chapter 10: Patterns for Testing....254
Technical requirements....254
The Mock Object pattern....254
Real-world examples....255
Use cases for the Mock Object pattern....255
Implementing the Mock Object pattern....256
The Dependency Injection pattern....258
Real-world examples....258
Use cases for the Dependency Injection pattern....258
Implementing the Dependency Injection pattern – using a mock object....259
Implementing the Dependency Injection pattern – using a decorator....261
Summary....265
Chapter 11: Python Anti-Patterns....266
Technical requirements....266
Code style violations....266
Tools for fixing coding style violations....267
Indentation....267
Maximum line length and blank lines....267
Imports....268
Naming conventions....268
Comments....269
Whitespace in expressions and statements....269
Correctness anti-patterns....270
Using the type() function for comparing types....270
Mutable default argument....271
Accessing a protected member from outside a class....273
Maintainability anti-patterns....274
Using a wildcard import....275
LBYL versus EAFP....275
Overusing inheritance and tight coupling....276
Using global variables for sharing data between functions....277
Performance anti-patterns....278
Not using .join() to concatenate strings in a loop....278
Using global variables for caching....279
Summary....281
Index....284
About Packt....292
Other Books You May Enjoy....293
As software systems become increasingly complex, maintaining code quality, scalability, and efficiency can be a daunting challenge. Mastering Python Design Patterns is an essential resource that equips you with the tools you need to overcome these hurdles and create robust, scalable applications.
The book delves into design principles and patterns in Python, covering both classic and modern patterns, and apply them to solve daily challenges as a Python developer or architect.
Co-authored by two Python experts with a combined experience of three decades, this new edition covers creational, structural, behavioral, and architectural patterns, including concurrency, asynchronous, and performance patterns. You'll find out how these patterns are relevant to various domains, such as event handling, concurrency, distributed systems, and testing. Whether you're working on user interfaces (UIs), web apps, APIs, data pipelines, or AI models, this book equips you with the knowledge to build robust and maintainable software. The book also presents Python anti-patterns, helping you avoid common pitfalls and ensuring your code remains clean and efficient.
By the end of this book, you'll be able to confidently apply classic and modern Python design patterns to build robust, scalable applications.
With a focus on intermediate and advanced Python programmers, this book offers valuable insights into the best practices for software design, backed by real-world examples and decades of experience. The book is also an excellent resource for software architects and team leaders who want to improve code quality and maintainability across their projects. Prior Python proficiency, including syntax, data structures, and OOP will help you get the most out of this book.