Mastering Python Design Patterns: Craft essential Python patterns by following core design principles
By Kamon Ayeva and Sakis Kasampalis
()
Related to Mastering Python Design Patterns
Related ebooks
Hands-On Python for DevOps: Leverage Python's native libraries to streamline your workflow and save time with automation Rating: 0 out of 5 stars0 ratingsPython Programming for Newbies Rating: 0 out of 5 stars0 ratingsFastAPI Cookbook: Develop high-performance APIs and web applications with Python Rating: 0 out of 5 stars0 ratings.NET Design Patterns Rating: 3 out of 5 stars3/5Python Apps on Visual Studio Code: Develop apps and utilize the true potential of Visual Studio Code (English Edition) Rating: 0 out of 5 stars0 ratingsReinforcement Learning Algorithms with Python: Learn, understand, and develop smart algorithms for addressing AI challenges Rating: 0 out of 5 stars0 ratingsAI-Assisted Programming for Web and Machine Learning: Improve your development workflow with ChatGPT and GitHub Copilot Rating: 0 out of 5 stars0 ratingsPython and SQL Bible: From Beginner to World Expert: Unleash the true potential of data analysis and manipulation. Rating: 0 out of 5 stars0 ratingsMachine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition) Rating: 0 out of 5 stars0 ratingsPython Tools for Visual Studio Rating: 0 out of 5 stars0 ratingsLearning Yii Testing Rating: 1 out of 5 stars1/5Learning Python Design Patterns - Second Edition: Learning Python Design Patterns - Second Edition Rating: 0 out of 5 stars0 ratingsFull-Stack Web Development with TypeScript 5: Craft modern full-stack projects with Bun, PostgreSQL, Svelte, TypeScript, and OpenAI Rating: 0 out of 5 stars0 ratingsKnockoutJS Blueprints Rating: 0 out of 5 stars0 ratingsImplementing GitOps with Kubernetes: Automate, manage, scale, and secure infrastructure and cloud-native applications on AWS and Azure Rating: 0 out of 5 stars0 ratingsMastering JavaScript Design Patterns Rating: 4 out of 5 stars4/5Building Web Applications with Python and Neo4j Rating: 0 out of 5 stars0 ratingsUnlocking the Power of Auto-GPT and Its Plugins: Implement, customize, and optimize Auto-GPT for building robust AI applications Rating: 0 out of 5 stars0 ratingsBuilding LLM Powered Applications: Create intelligent apps and agents with large language models Rating: 0 out of 5 stars0 ratingsGetting Started with Python Data Analysis Rating: 0 out of 5 stars0 ratingsMongoDB High Availability Rating: 5 out of 5 stars5/5Modular Programming with Python Rating: 0 out of 5 stars0 ratingsASP.NET 3.5 Application Architecture and Design Rating: 0 out of 5 stars0 ratingsGetting Started with Review Board Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsHTML in 30 Pages Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5A Slackers Guide to Coding with Python: Ultimate Beginners Guide to Learning Python Quick Rating: 0 out of 5 stars0 ratingsJavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5
Reviews for Mastering Python Design Patterns
0 ratings0 reviews
Book preview
Mastering Python Design Patterns - Kamon Ayeva
Mastering Python Design Patterns
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Kunal Sawant
Publishing Product Manager: Samriddhi Murarka
Book Project Manager: Manisha Singh
Senior Editor: Nithya Sadanandan
Technical Editor: Vidhisha Patidar
Copy Editor: Safis Editing
Proofreader: Nithya Sadanandan
Indexer: Tejal Soni
Production Designer: Nilesh Mohite
DevRel Marketing Coordinator: Shrinidhi Manoharan
First published: January 2015
Second edition: August 2018
Third edition: May 2024
Production reference: 1230524
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK
ISBN 978-1-83763-961-8
www.packtpub.com
I would like to thank my parents for their love and support.
– Kamon Ayeva
Contributors
About the authors
Kamon Ayeva is a seasoned Python expert with over two decades of experience in the technology sector. As the founder of Content Gardening Studio, a consulting and custom development services firm, he specializes in web development, data, and AI, delivering top-notch Python solutions to clients globally. A trusted educator, Kamon has trained numerous developers, solidifying his reputation as an authority in the Python community. He is also the co-author of the previous edition of Mastering Python Design Patterns. On social media, you can find him on Twitter under the handle @kamon, where he continues to share invaluable insights and trends in Python and software design.
Sakis Kasampalis is a software architect living in the Netherlands. He is not dogmatic about particular programming languages and tools; his principle is that the right tool should be used for the right job. One of his favorite tools is Python because he finds it very productive. Sakis was also the technical reviewer of Mastering Object-oriented Python and Learning Python Design Patterns, published by Packt Publishing.
About the reviewers
Fréjus L. O. Adjé is a software engineer with more than 10 years of industry experience. Holding a degree in Computer Network and Internet Engineering, he is highly regarded for his proficiency in creating dynamic web applications. With nearly 7 years of focused expertise in Python, he has successfully led teams and delivered innovative solutions. Committed to lifelong learning, he actively seeks to stay ahead in the rapidly evolving technology landscape while sharing his expertise by mentoring new developers. Off-duty, Fréjus enjoys chess, design, and football, dedicating his free time to chess matches, exploring trends, and playing or watching football, which broadens his global perspective and enriches his software development approach.
Encolpe DEGOUT has been a developer and an open-source advocate since 1998. He started using Python and Zope in 2002 and then became involved in Nuxeo CPS and Plone CMS. He promotes the open-source ecosystem by organizing conferences and giving courses. Besides these involvements, he participates in automating data integration in a search engine specialized in European media and worldwide press.
Gianguglielmo Calvi is a computer scientist and knowledge manager, founder of Heuristica, and co-founder of EnQu Ideation. With a strong background in programming in C/C++ and Python, he has over two decades of experience in international projects. He currently serves as a Senior Knowledge Management Systems Expert consultant at the Green Growth Knowledge Partnership. His career includes roles as a researcher in Cognitive Science and AI at ISTC-CNR and as a knowledge manager at the International Labour Organization, UN/CEFACT, UNDSS, WHO EUROPE, and Voolinks. Gianguglielmo holds a master’s degree in computer science from the University of Pisa, a Knowledge Management certification from the IKF Institute, and various international certifications.
Table of Contents
Preface
Part 1: Start with Principles
1
Foundational Design Principles
Technical requirements
Following the Encapsulate What Varies principle
What does it mean?
Benefits
Techniques for achieving encapsulation
An example – encapsulating using polymorphism
An example – encapsulating using a property
Following the Favor Composition Over Inheritance principle
What does it mean?
Benefits
Techniques for composition
An example – compose a car using the engine
Following the Program to Interfaces, Not Implementations principle
What does it mean?
Benefits
Techniques for interfaces
An example – different types of logger
An example – different types of logger, now using Protocols
Following the Loose Coupling principle
What does it mean?
Benefits
Techniques for loose coupling
An example – a message service
Summary
2
SOLID Principles
Technical requirements
SRP
An example of software design following the SRP
OCP
An example of design following the OCP
LSP
An example of design following the LSP
ISP
An example of design following the ISP
DIP
An example of design following the ISP
Summary
Part 2: From the Gang of Four
3
Creational Design Patterns
Technical requirements
The factory pattern
The factory method
The abstract factory pattern
The builder pattern
Real-world examples
Comparison with the factory pattern
Use cases for the builder pattern
Implementing the builder pattern
The prototype pattern
Real-world examples
Use cases for the prototype pattern
Implementing the prototype pattern
The singleton pattern
Real-world examples
Use cases for the singleton pattern
Implementing the singleton pattern
Should you use the singleton pattern?
The object pool pattern
Real-world examples
Use cases for the object pool pattern
Implementing the object pool pattern
Summary
4
Structural Design Patterns
Technical requirements
The adapter pattern
Real-world examples
Use cases for the adapter pattern
Implementing the adapter pattern – adapt a legacy class
Implementing the adapter pattern – adapt several classes into a unified interface
The decorator pattern
Real-world examples
Use cases for the decorator pattern
Implementing the decorator pattern
The bridge pattern
Real-world examples
Use cases for the bridge pattern
Implementing the bridge pattern
The facade pattern
Real-world examples
Use cases for the facade pattern
Implementing the facade pattern
The flyweight pattern
Real-world examples
Use cases for the flyweight pattern
Implementing the flyweight pattern
The proxy pattern
Real-world examples
Use cases for the proxy pattern
Implementing the proxy pattern – a virtual proxy
Implementing the proxy pattern – a protection proxy
Implementing the proxy pattern – a remote proxy
Implementing the proxy pattern – a smart proxy
Summary
5
Behavioral Design Patterns
Technical requirements
The Chain of Responsibility pattern
Real-world examples
Use cases for the Chain of Responsibility pattern
Implementing the Chain of Responsibility pattern
The Command pattern
Real-world examples
Use cases for the Command pattern
Implementing the Command pattern
The Observer pattern
Real-world examples
Use cases for the Observer pattern
Implementing the Observer pattern
The State pattern
Real-world examples
Use cases for the State pattern
Implementing the State pattern
The Interpreter pattern
Real-world examples
Use cases for the Interpreter pattern
Implementing the Interpreter pattern
The Strategy pattern
Real-world examples
Use cases for the Strategy pattern
Implementing the Strategy pattern
The Memento pattern
Real-world examples
Use cases for the Memento pattern
Implementing the Memento pattern
The Iterator pattern
Use cases for the Iterator pattern
Implementing the Iterator pattern
The Template pattern
Real-world examples
Use cases for the Template pattern
Implementing the Template pattern
Other behavioral design patterns
Summary
Part 3: Beyond the Gang of Four
6
Architectural Design Patterns
Technical requirements
The MVC pattern
Real-world examples
Use cases for the MVC pattern
Implementing the MVC pattern
The Microservices pattern
Real-world examples
Use cases for the Microservices pattern
Implementing the microservices pattern – a payment service using gRPC
Implementing the microservices pattern – an LLM service using Lanarky
The Serverless pattern
Real-world examples
Use cases for the Serverless pattern
Implementing the Serverless pattern
The Event Sourcing pattern
Real-world examples
Use cases for the Event Sourcing pattern
Implementing the event sourcing pattern – the manual way
Implementing the Event Sourcing pattern – using a library
Other architectural design patterns
Summary
7
Concurrency and Asynchronous Patterns
Technical requirements
The Thread Pool pattern
Real-world examples
Use cases for the Thread Pool pattern
Implementing the Thread Pool pattern
The Worker Model pattern
Real-world examples
Use cases for the Worker Model pattern
Implementing the Worker Model pattern
The Future and Promise pattern
Real-world examples
Use cases for the Future and Promise pattern
Implementing the Future and Promise pattern – using concurrent.futures
Implementing the Future and Promise pattern – using asyncio
The Observer pattern in reactive programming
Real-world examples
Use cases for the Observer pattern in reactive programming
Implementing the Observer pattern in reactive programming
Other concurrency and asynchronous patterns
Summary
8
Performance Patterns
Technical requirements
The Cache-Aside pattern
Real-world examples
Use cases for the cache-aside pattern
Implementing the cache-aside pattern
The Memoization pattern
Real-world examples
Use cases for the memoization pattern
Implementing the memoization pattern
The Lazy Loading pattern
Real-world examples
Use cases for the lazy loading pattern
Implementing the lazy loading pattern – lazy attribute loading
Implementing the lazy loading pattern – using caching
Summary
9
Distributed Systems Patterns
Technical requirements
The Throttling pattern
Real-world examples
Use cases for the Throttling pattern
Implementing the Throttling pattern
The Retry pattern
Real-world examples
Use cases for the Retry pattern
Implementing the Retry pattern
The Circuit Breaker pattern
Real-world examples
Use cases for the Circuit Breaker pattern
Implementing the Circuit Breaker pattern
Other distributed systems patterns
Summary
10
Patterns for Testing
Technical requirements
The Mock Object pattern
Real-world examples
Use cases for the Mock Object pattern
Implementing the Mock Object pattern
The Dependency Injection pattern
Real-world examples
Use cases for the Dependency Injection pattern
Implementing the Dependency Injection pattern – using a mock object
Implementing the Dependency Injection pattern – using a decorator
Summary
11
Python Anti-Patterns
Technical requirements
Code style violations
Tools for fixing coding style violations
Indentation
Maximum line length and blank lines
Imports
Naming conventions
Comments
Whitespace in expressions and statements
Correctness anti-patterns
Using the type() function for comparing types
Mutable default argument
Accessing a protected member from outside a class
Maintainability anti-patterns
Using a wildcard import
LBYL versus EAFP
Overusing inheritance and tight coupling
Using global variables for sharing data between functions
Performance anti-patterns
Not using .join() to concatenate strings in a loop
Using global variables for caching
Summary
Index
Other Books You May Enjoy
Preface
Explore the world of design principles and design patterns in the context of the Python programming language with this comprehensive guide. Learn about classic and modern design patterns and how to use them to solve problems you encounter daily as a Python developer or software architect.
With code examples, real-world case studies, and detailed solution implementations, this book is a must-read for Python developers looking to elevate their coding skills. Co-authored by a Python expert with over two decades of experience, this new edition expands the scope to cover more design pattern categories. Gain insights into creational, structural, behavioral, architectural, and other important patterns for modern software design, such as concurrency, asynchronous, and performance patterns. Learn how to apply these patterns in various domains like event handling systems, concurrency, distributed systems, and testing. The book also presents Python anti-patterns, helping you avoid common pitfalls.
Whether you’re developing user interfaces, web applications, APIs, data pipelines, or AI models, this book equips you with the knowledge to build robust and maintainable software.
This book adopts a hands-on approach, providing code examples for each design pattern. Each chapter includes step-by-step instructions to test the code, making it an interactive learning experience. Where applicable, for each design principle or pattern, the book presents at least one real-world example, which may or may not be Python-based, and at least one Python-based example.
Who this book is for
This book is for Python developers looking to deepen their understanding of design patterns and how they can be applied to various types of projects. With a focus on intermediate and advanced Python programmers, the book also includes introductory chapters that make it accessible for those who are relatively new to the language. Whether you’re a web developer, data engineer, or AI specialist, this book offers valuable insights into the best practices for software design, backed by real-world examples and decades of experience. It’s also an excellent resource for software architects and team leaders who want to improve code quality and maintainability across their projects.
What this book covers
Chapter 1
, Foundational Design Principles, covers principles of encapsulation, composition, programming to interfaces, and loose coupling to help you create more adaptable and maintainable systems.
Chapter 2
, SOLID Principles, Provides guidelines for designing robust, maintainable, and scalable software. Each of these principles contributes to creating clean and adaptable code.
Chapter 3
, Creational Design Patterns, explores patterns that help manage object creation by controlling which classes to instantiate.
Chapter 4
, Structural Design Patterns, provides insights into patterns that facilitate the design process by identifying simple ways to establish relationships between entities. This chapter delves into six essential structural patterns, providing you with the skills to structure your code efficiently and elegantly.
Chapter 5
, Behavioral Design Patterns, shares patterns that focus on the interactions and responsibilities of objects, promoting effective communication and flexible assignment of responsibilities. This chapter explores key patterns such as Strategy, Observer, and Command, demonstrating how they streamline object collaboration and enhance the adaptability of code.
Chapter 6
, Architectural Design Patterns, delves into patterns that provide templates for solving common architectural problems, facilitating the development of scalable, maintainable, and reusable systems.
Chapter 7
, Concurrency and Asynchronous Patterns, explores patterns that help you develop applications that are both fast and user-friendly, particularly in environments with heavy I/O operations or significant computational work.
Chapter 8
, Performance Patterns, provides guidance on patterns that address common bottlenecks and optimization challenges, offering proven methodologies to improve execution time, reduce memory usage, and scale effectively.
Chapter 9
, Distributed Systems Patterns, shows patterns that empower developers to architect robust distributed systems, from managing communication between nodes to ensuring fault tolerance and consistency.
Chapter 10
, Patterns for Testing, presents patterns that help in isolating components, making tests more reliable, and promoting code reusability.
Chapter 11
, Python Anti-Patterns, explores common programming practices that, while not necessarily wrong, often lead to less efficient, less readable, and/or less maintainable code. You will learn to understand and avoid these pitfalls.
To get the most out of this book
Use a machine with a recent version of Windows, Linux, or macOS.
Install Python 3.12. It is also useful to create a virtual environment from your Python installation so that when you add third-party modules required for following some of the chapters, you do not end up polluting your global Python. This is a fundamental best practice for productivity with Python, and you will find many resources on the Internet that explain how to do this.
Install and use Docker on your machine. This will help with the requirement of some external software services or tools, such as LocalStack (used in Chapter 6
) and the Redis server (used in Chapter 8
).
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
Download the example code files
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Mastering-Python-Design-Patterns-Third-Edition
. If there’s an update to the code, it will be updated in the GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/
. Check them out!
Conventions used
There are a number of text conventions used and formatting specificities throughout this book.
Most of the code has been automatically formatted
The formatting has been done using the Black tool, as is commonly done by Python developers for productivity reasons. So it might not look exactly like the code you would write yourself. But it is valid; it is a PEP 8-compliant code. The goal is to improve the readability of the code snippets.
So, some code snippets in the code files as well as in the book’s pages may look like the following:
State = Enum(
State
,
NEW RUNNING SLEEPING RESTART ZOMBIE
,
)
Another example might be the following:
msg = (
ftrying to create process '{name}'
ffor user '{user}'
)
print(msg)
The code snippets in the book’s pages may be shortened
To improve readability, when there is a documentation string (docstring) for a function or class, and it is too long, we remove it from the code snippet in the book.
When some code (class or function) is too long to display on the chapter’s pages, we may shorten it, and refer the reader to the complete code in the file.
Note
In case of an issue with long commands, which are spread across several lines (with the ‘/’ character as separator), you can reformat the long command text, removing the ‘/’ character, to make sure that the command is correctly interpreted in the terminal.
Other conventions
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: Define the Logger interface with a log method.
A block of code is set as follows:
class MyInterface(ABC):
@abstractmethod
def do_something(self, param: str):
pass
Any command-line input or output is written as follows:
python3.12 –m pip install -–user mypy
Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: It is one of the core concepts in object-oriented programming OOP that enables a single interface to represent different types.
Tips or important notes
Appear like this.
Get in touch
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata
and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected]
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com
.
Share your thoughts
Once you’ve read Mastering Python Design Patterns, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page
for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Download a free PDF copy of this book
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily
Follow these simple steps to get the benefits:
Scan the QR code or visit the link below
https://packt.link/free-ebook/9781837639618
2. Submit your proof of purchase
3. That’s it! We’ll send your free PDF and other benefits to your email directly
Part 1: Start with Principles
This first part introduces you to the foundational software design principles and the S.O.L.I.D. principles that build upon them. This part includes the following chapters:
Chapter 1
, Foundational Design Principles
Chapter 2
, SOLID Principles
1
Foundational Design Principles
Design principles form the foundation of any well-architected software. They serve as the guiding light that helps developers navigate the path of creating maintainable, scalable, and robust