Mastering Python Design Patterns - Third Edition

Book description

Explore Python design patterns such as observer, proxy, throttling, dependency injection, and anti-patterns to develop efficient and scalable applications

Key Features

  • Master essential design principles to build robust software architecture with the latest features in Python 3.10
  • Apply proven design patterns to solve complex problems efficiently
  • Understand anti-patterns to avoid common pitfalls in Python programming
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

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 showing you how to apply them to solve daily challenges as a Python developer or architect. This new edition covers creational, structural, behavioral, and architectural patterns, including concurrency, asynchronous, and performance patterns. You'll explore 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.

What you will learn

  • Master fundamental design principles and SOLID concepts
  • Become familiar with Gang of Four (GoF) patterns and apply them effectively in Python
  • Explore architectural design patterns to architect robust systems
  • Delve into concurrency and performance patterns for optimized code
  • Discover distributed systems patterns for scalable applications
  • Get up to speed with testing patterns to ensure code reliability and maintainability
  • Develop modular, decoupled systems and manage dependencies efficiently

Who this book is for

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.

Table of contents

  1. Mastering Python Design Patterns
  2. Contributors
  3. About the authors
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
      1. Most of the code has been automatically formatted
      2. The code snippets in the book’s pages may be shortened
      3. Other conventions
    6. Get in touch
    7. Share your thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Start with Principles
  7. Chapter 1: Foundational Design Principles
    1. Technical requirements
    2. Following the Encapsulate What Varies principle
      1. What does it mean?
      2. Benefits
      3. Techniques for achieving encapsulation
      4. An example – encapsulating using polymorphism
      5. An example – encapsulating using a property
    3. Following the Favor Composition Over Inheritance principle
      1. What does it mean?
      2. Benefits
      3. Techniques for composition
      4. An example – compose a car using the engine
    4. Following the Program to Interfaces, Not Implementations principle
      1. What does it mean?
      2. Benefits
      3. Techniques for interfaces
      4. An example – different types of logger
      5. An example – different types of logger, now using Protocols
    5. Following the Loose Coupling principle
      1. What does it mean?
      2. Benefits
      3. Techniques for loose coupling
      4. An example – a message service
    6. Summary
  8. Chapter 2: SOLID Principles
    1. Technical requirements
    2. SRP
      1. An example of software design following the SRP
    3. OCP
      1. An example of design following the OCP
    4. LSP
      1. An example of design following the LSP
    5. ISP
      1. An example of design following the ISP
    6. DIP
      1. An example of design following the ISP
    7. Summary
  9. Part 2: From the Gang of Four
  10. Chapter 3: Creational Design Patterns
    1. Technical requirements
    2. The factory pattern
      1. The factory method
      2. The abstract factory pattern
    3. The builder pattern
      1. Real-world examples
      2. Comparison with the factory pattern
      3. Use cases for the builder pattern
      4. Implementing the builder pattern
    4. The prototype pattern
      1. Real-world examples
      2. Use cases for the prototype pattern
      3. Implementing the prototype pattern
    5. The singleton pattern
      1. Real-world examples
      2. Use cases for the singleton pattern
      3. Implementing the singleton pattern
      4. Should you use the singleton pattern?
    6. The object pool pattern
      1. Real-world examples
      2. Use cases for the object pool pattern
      3. Implementing the object pool pattern
    7. Summary
  11. Chapter 4: Structural Design Patterns
    1. Technical requirements
    2. The adapter pattern
      1. Real-world examples
      2. Use cases for the adapter pattern
      3. Implementing the adapter pattern – adapt a legacy class
      4. Implementing the adapter pattern – adapt several classes into a unified interface
    3. The decorator pattern
      1. Real-world examples
      2. Use cases for the decorator pattern
      3. Implementing the decorator pattern
    4. The bridge pattern
      1. Real-world examples
      2. Use cases for the bridge pattern
      3. Implementing the bridge pattern
    5. The facade pattern
      1. Real-world examples
      2. Use cases for the facade pattern
      3. Implementing the facade pattern
    6. The flyweight pattern
      1. Real-world examples
      2. Use cases for the flyweight pattern
      3. Implementing the flyweight pattern
    7. The proxy pattern
      1. Real-world examples
      2. Use cases for the proxy pattern
      3. Implementing the proxy pattern – a virtual proxy
      4. Implementing the proxy pattern – a protection proxy
      5. Implementing the proxy pattern – a remote proxy
      6. Implementing the proxy pattern – a smart proxy
    8. Summary
  12. Chapter 5: Behavioral Design Patterns
    1. Technical requirements
    2. The Chain of Responsibility pattern
      1. Real-world examples
      2. Use cases for the Chain of Responsibility pattern
      3. Implementing the Chain of Responsibility pattern
    3. The Command pattern
      1. Real-world examples
      2. Use cases for the Command pattern
      3. Implementing the Command pattern
    4. The Observer pattern
      1. Real-world examples
      2. Use cases for the Observer pattern
      3. Implementing the Observer pattern
    5. The State pattern
      1. Real-world examples
      2. Use cases for the State pattern
      3. Implementing the State pattern
    6. The Interpreter pattern
      1. Real-world examples
      2. Use cases for the Interpreter pattern
      3. Implementing the Interpreter pattern
    7. The Strategy pattern
      1. Real-world examples
      2. Use cases for the Strategy pattern
      3. Implementing the Strategy pattern
    8. The Memento pattern
      1. Real-world examples
      2. Use cases for the Memento pattern
      3. Implementing the Memento pattern
    9. The Iterator pattern
      1. Use cases for the Iterator pattern
      2. Implementing the Iterator pattern
    10. The Template pattern
      1. Real-world examples
      2. Use cases for the Template pattern
      3. Implementing the Template pattern
    11. Other behavioral design patterns
    12. Summary
  13. Part 3: Beyond the Gang of Four
  14. Chapter 6: Architectural Design Patterns
    1. Technical requirements
    2. The MVC pattern
      1. Real-world examples
      2. Use cases for the MVC pattern
      3. Implementing the MVC pattern
    3. The Microservices pattern
      1. Real-world examples
      2. Use cases for the Microservices pattern
      3. Implementing the microservices pattern – a payment service using gRPC
      4. Implementing the microservices pattern – an LLM service using Lanarky
    4. The Serverless pattern
      1. Real-world examples
      2. Use cases for the Serverless pattern
      3. Implementing the Serverless pattern
    5. The Event Sourcing pattern
      1. Real-world examples
      2. Use cases for the Event Sourcing pattern
      3. Implementing the event sourcing pattern – the manual way
      4. Implementing the Event Sourcing pattern – using a library
    6. Other architectural design patterns
    7. Summary
  15. Chapter 7: Concurrency and Asynchronous Patterns
    1. Technical requirements
    2. The Thread Pool pattern
      1. Real-world examples
      2. Use cases for the Thread Pool pattern
      3. Implementing the Thread Pool pattern
    3. The Worker Model pattern
      1. Real-world examples
      2. Use cases for the Worker Model pattern
      3. Implementing the Worker Model pattern
    4. The Future and Promise pattern
      1. Real-world examples
      2. Use cases for the Future and Promise pattern
      3. Implementing the Future and Promise pattern – using concurrent.futures
      4. Implementing the Future and Promise pattern – using asyncio
    5. The Observer pattern in reactive programming
      1. Real-world examples
      2. Use cases for the Observer pattern in reactive programming
      3. Implementing the Observer pattern in reactive programming
    6. Other concurrency and asynchronous patterns
    7. Summary
  16. Chapter 8: Performance Patterns
    1. Technical requirements
    2. The Cache-Aside pattern
      1. Real-world examples
      2. Use cases for the cache-aside pattern
      3. Implementing the cache-aside pattern
    3. The Memoization pattern
      1. Real-world examples
      2. Use cases for the memoization pattern
      3. Implementing the memoization pattern
    4. The Lazy Loading pattern
      1. Real-world examples
      2. Use cases for the lazy loading pattern
      3. Implementing the lazy loading pattern – lazy attribute loading
      4. Implementing the lazy loading pattern – using caching
    5. Summary
  17. Chapter 9: Distributed Systems Patterns
    1. Technical requirements
    2. The Throttling pattern
      1. Real-world examples
      2. Use cases for the Throttling pattern
      3. Implementing the Throttling pattern
    3. The Retry pattern
      1. Real-world examples
      2. Use cases for the Retry pattern
      3. Implementing the Retry pattern
    4. The Circuit Breaker pattern
      1. Real-world examples
      2. Use cases for the Circuit Breaker pattern
      3. Implementing the Circuit Breaker pattern
    5. Other distributed systems patterns
    6. Summary
  18. Chapter 10: Patterns for Testing
    1. Technical requirements
    2. The Mock Object pattern
      1. Real-world examples
      2. Use cases for the Mock Object pattern
      3. Implementing the Mock Object pattern
    3. The Dependency Injection pattern
      1. Real-world examples
      2. Use cases for the Dependency Injection pattern
      3. Implementing the Dependency Injection pattern – using a mock object
      4. Implementing the Dependency Injection pattern – using a decorator
    4. Summary
  19. Chapter 11: Python Anti-Patterns
    1. Technical requirements
    2. Code style violations
    3. Tools for fixing coding style violations
      1. Indentation
      2. Maximum line length and blank lines
      3. Imports
      4. Naming conventions
      5. Comments
      6. Whitespace in expressions and statements
    4. Correctness anti-patterns
      1. Using the type() function for comparing types
      2. Mutable default argument
      3. Accessing a protected member from outside a class
    5. Maintainability anti-patterns
      1. Using a wildcard import
      2. LBYL versus EAFP
      3. Overusing inheritance and tight coupling
      4. Using global variables for sharing data between functions
    6. Performance anti-patterns
      1. Not using .join() to concatenate strings in a loop
      2. Using global variables for caching
    7. Summary
  20. Index
    1. Why subscribe?
  21. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Mastering Python Design Patterns - Third Edition
  • Author(s): Kamon Ayeva, Sakis Kasampalis
  • Release date: May 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781837639618