Design patterns in Python

The process of designing software is, in part, a process of identifying the various functions a system must perform, and deciding the best way to implement those functions. Complex designs are often layered, from the top level architecture down to the detailed design of individual classes and functions.

In this process, the same problems often crop up again and again, even in totally different application areas. Over the years, many designers will have tackled these problems, each finding their own solution. Different designers will have come up with good or bad solutions to the same problem.

In their book Design patterns : elements of reusable object-oriented software, the so called "gang of four" (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) investigated various common software design problems, and identified best-practice solutions that are commonly used in the software industry.

These common solutions were called design patterns.

A design pattern describes a basic approach to solving a particular design problem. It doesn't exist as a code library, or a set of detailed instructions for implementing a solution. Instead it describes the basic elements involved in the design, and how they relate to each other. It is up to the software designer to apply the pattern to their own software design.

The authors of the book are clear that they did not invent the patterns, they discovered patterns that were already widely used. Each pattern had probably been reinvented many times by different developers over the years. Nonetheless, the GoF authors did a very good job of analysing and describing the patterns, and equally in naming them. The names are very important, because when one engineer tells another that they have used, for example, decorator pattern in a particular module, they will both immediately have a common understanding of how the code works.

In all, the authors identified 23 patterns, which they divided into 3 categories:

  • Creational patterns.

  • Structural patterns.

  • Behavioural patterns.

We will cover some of these patterns in the articles in this section.

The patterns are primarily object-oriented. Patterns are usually based on the relationships and interactions between a set of classes that implement the pattern.

I will be adding more articles on the implementation of various design patterns in Python over the coming weeks.

You can read this series on