Design patterns

By 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.

See also

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

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest