Mastering Python Design Patterns
eBook - ePub

Mastering Python Design Patterns

A guide to creating smart, efficient, and reusable software, 2nd Edition

Kamon Ayeva, Sakis Kasampalis

Condividi libro
  1. 248 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Mastering Python Design Patterns

A guide to creating smart, efficient, and reusable software, 2nd Edition

Kamon Ayeva, Sakis Kasampalis

Dettagli del libro
Anteprima del libro
Indice dei contenuti

Informazioni sul libro

Exploit various design patterns to master the art of solving problems using Python

Key Features

  • Master the application design using the core design patterns and latest features of Python 3.7
  • Learn tricks to solve common design and architectural challenges
  • Choose the right plan to improve your programs and increase their productivity

Book Description

Python is an object-oriented scripting language that is used in a wide range of categories. In software engineering, a design pattern is an elected solution for solving software design problems. Although they have been around for a while, design patterns remain one of the top topics in software engineering, and are a ready source for software developers to solve the problems they face on a regular basis. This book takes you through a variety of design patterns and explains them with real-world examples. You will get to grips with low-level details and concepts that show you how to write Python code, without focusing on common solutions as enabled in Java and C++. You'll also fnd sections on corrections, best practices, system architecture, and its designing aspects. This book will help you learn the core concepts of design patterns and the way they can be used to resolve software design problems. You'll focus on most of the Gang of Four (GoF) design patterns, which are used to solve everyday problems, and take your skills to the next level with reactive and functional patterns that help you build resilient, scalable, and robust applications. By the end of the book, you'll be able to effciently address commonly faced problems and develop applications, and also be comfortable working on scalable and maintainable projects of any size.

What you will learn

  • Explore Factory Method and Abstract Factory for object creation
  • Clone objects using the Prototype pattern
  • Make incompatible interfaces compatible using the Adapter pattern
  • Secure an interface using the Proxy pattern
  • Choose an algorithm dynamically using the Strategy pattern
  • Keep the logic decoupled from the UI using the MVC pattern
  • Leverage the Observer pattern to understand reactive programming
  • Explore patterns for cloud-native, microservices, and serverless architectures

Who this book is for

This book is for intermediate Python developers. Prior knowledge of design patterns is not required to enjoy this book.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Mastering Python Design Patterns è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Mastering Python Design Patterns di Kamon Ayeva, Sakis Kasampalis in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Ciencia de la computación e Programación en Python. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.



Microservices and Patterns for the Cloud

Traditionally, developers working on building a server-side application have been using a single code base and implementing all or most functionalities right there, using common development practices such as functions and classes, and design patterns such as the ones we have covered in this book so far. But, with the evolution of the IT industry, economic factors, and pressure for fast times to market and returns on investment, there is a constant need to improve the practices of engineering teams and ensure more reactivity and scalability with servers, service delivery, and operations. We need to learn about other useful patterns, not only object-oriented programming ones. In this chapter, we will cover the last part of our exploration, focused on Modern Architecture-style design patterns.
One of the main additions to the catalog of patterns for engineers in recent years has been the Microservice Architecture pattern or Microservices. The idea is that we can build an application as a set of loosely coupled, collaborating services. In this architectural style, an application might consist of services such as the order management service, the customer management service, and so on. Services can be developed and deployed independently of one another.
Moreover, increasing numbers of applications are deployed in the cloud (AWS, Azure, Google Cloud, Digital Ocean, Heroku, and so on) and must be designed upfront with this type of environment and its constraints in mind. A number of patterns have emerged to help us deal with these new aspects of our work. Even the engineering teams, ways of working have evolved, hence the DevOps teams and roles we now have in many software development and production organizations. There are many of these cloud architecture related patterns, but we choose to focus on a few ones related to Microservices: Retry, Circuit Breaker, and Cache-Aside. There are many of these cloud architecture related patterns, but we choose to focus on a few ones related to Microservices: Retry, Circuit Breaker, Cache-Aside, and Throttling.
In this chapter, we will discuss the following patterns:
  • The Retry pattern
  • The Circuit Breaker pattern
  • The Cache-Aside pattern
  • The Throttling pattern

The Microservices pattern

Before Microservices, developers of an application have been using a single code base for implementing all functionalities. For example, the front-end UI that includes forms, buttons, and specialized JavaScript code for interactions and effects, the application logic that handles all the passage of data between the UI and the database, and another application logic that, based on triggers or scheduling, does some asynchronous actions behind the scenes, such as sending email notifications. Even the administration UI, as in a Django-based application, would be inside the same application.
For this way of building an application, we talk about the Monolith model or using a Monolithic architecture.
There is a cost to the Monolithic model of application development. Here are some disadvantages:
  • Since it uses a single code base, the development team has to work on maintaining the whole code base simultaneously
  • It is more difficult to organize testing and reproduce and fix bugs
  • Tests and deployments become difficult to manage as the application and its user base grows and its constraints increase
With the Microservice pattern or Microservices, an application might consist of services such as the order management service, the customer management service, and so on. And, services can be developed and deployed independently of one another.

Real-world examples

When looking for examples, I found Eventuate™ ( which is a platform for developing asynchronous microservices. It solves the distributed data management problems inherent in a microservice architecture, enabling you to focus on your business logic.
eShopOnContainers is one of the Application Architecture reference applications from the
.NET Foundation ( It is described as an easy-to-get-started sample reference microservice and container-based application.
We can also cite Serverless architecture/computing, which shares some of the characteristics of microservices. Its main particularity is that there is no provision of server resources for running and operating things. Basically, you split your application at an even more granular level (using functions), and you use serverless hosting vendors to deploy the services. What makes this model attractive is that, with these providers (the big three players being AWS Lambda, Google Cloud Functions and Azure Functions), in theory, you only pay for the use involved (the execution of the functions).

Use cases

We can think of several use cases where microservices offer a clever answer. We can use a microservices architecture-based design every time we are building an application that has at least one of the following characteristics:
  • There is a requirement to support different clients, including desktop and mobile
  • There is an API for third parties to consume
  • We have to communicate with other applications using messaging
  • We serve requests by accessing a database, communicating with other systems, and returning the right type of response (JSON, XML, HTML or even PDF)
  • There are logical components corresponding to different functional areas of the application


Let's briefly talk about software installation and application deployment in the Microservices world. Switching from deploying a single application to deployments of many small services means that the number of things that need to be handled increases exponentially. While you might have been fine with a single application server and few runtime dependencies, when moving to microservices, the number of dependencies will increase drastically. For example, one service could benefit from the relational database while the other would need ElasticSearch. You may need a service using MySQL and another one using the Redis server. So, using the microservices approach also means you need to use Containers.
Thanks to Docker, things are getting easier, since we can run those services as containers. The idea is that your application server, dependencies and runtime libraries, compiled code, configurations, and so on, are inside those containers. And then, all you have to do is run services packed as containers and make sure that they can communicate with each other.
You can implement the Microservices pattern, for a web app or an API, by directly using Django or Flask. However, for our examples, we are going to use a specialized microservices framework called Nameko (
According to their official website, Nameko is described as follows:
A microservices framework for Python that lets service developers concentrate on application logic and encourages testability.
Nameko has RPC over AMQP built in, allowing easy communication between the services. It also has a simple interface for HTTP requests.
Nameko and Flask: For writing Microservices that expose an HTTP endpoint, it is recommended that you use a more appropriate framework, such as Flask. To call Nameko methods over RPC using Flask, you can use flask_nameko, a wrapper built...

Indice dei contenuti