Introduction

Learn about the common design patterns in Python.

We'll cover the following

Overview

In the previous chapter, we were briefly introduced to design patterns, and covered the iterator pattern, a pattern so useful and common that it has been abstracted into the core of the programming language itself. In this chapter, we’ll be reviewing other common patterns and how they are implemented in Python. As with iteration, Python often provides an alternative syntax to make working with such problems simpler. We will cover both the traditional design and the Python version for these patterns.

In this chapter, we’ll see:

  • The Decorator pattern
  • The Observer pattern
  • The Strategy pattern
  • The Command pattern
  • The State pattern
  • The Singleton pattern

This chapter’s case study will emphasize how the distance calculation is an example of the Strategy design pattern, and how we can leverage abstract base classes to design a variety of distance computations that can be compared to see which produces the most useful results.

We’ll start with the Decorator pattern. This is used to combine different kinds of functionality into a single resulting object.

Get hands-on with 1200+ tech skills courses.