Design patterns


Martin McBride, 2021-08-30
Tags creational pattern structural pattern behavioural pattern
Categories design pattern

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 - these patterns provide ways to create objects. Examples include Factory Method (that creates an object whose type is determined by context), Builder (used to break down the creation of complex objects into separate steps), and Singleton (used to ensure that only one object of a particular class can ever exist).
  • Structural patterns - these patterns control the relationships between objects. Examples include Composite (where an object delegates its behaviour to one or more other objects) and Decorator (which is used to modify the functionality of an object without changing the object itself or its interface).
  • Behavioural patterns - these relate to the interactions between classes at runtime. Examples include Observer (which allows objects to notified other object when their state changes), and Strategy that allows alternate algorithms to be selected.

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.

If you found this article useful, you might be interested in the book Functional Programming in Python or other books by the same author.

Popular tags

2d arrays abstract data type alignment and animation arc array arrays behavioural pattern bezier curve built-in function callable object circle classes close closure cmyk colour comparison operator comprehension context context manager conversion creational pattern data types design pattern device space dictionary drawing duck typing efficiency else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif gradient greyscale higher order function hsl html image image processing imagesurface immutable object index inner function input installing iter iterable iterator itertools l system lambda function len line linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map monad mutability named parameter numeric python numpy object open operator optional parameter or partial application path polygon positional parameter print pure function pycairo python standard library radial gradient range recipes rectangle recursion reduce rgb rotation scaling sector segment sequence singleton slice slicing sound spirograph sprite square str strategy stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency tuple turtle unpacking user space vectorisation webserver website while loop zip