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.