Mastering Python Design Patterns - Kamon Ayeva - E-Book

Mastering Python Design Patterns E-Book

Kamon Ayeva

0,0
28,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 344

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Mastering Python Design Patterns

Craft essential Python patterns by following core design principles

Kamon Ayeva

Sakis Kasampalis

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.

EncolpeDEGOUT 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

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 PrinciplesChapter 2, SOLID Principles