It's a tech blog!

What Is a Design Pattern?

By Marcelo Vieyra, October 23rd 2023 | 3 mins, 534 words

Design patterns are common solutions to frequently occurring problems in software design. They are like prefabricated blueprints that can be customized to address recurring design challenges in your code.

You can't simply pick a pattern and paste it into your program as if it were pre-written functions or libraries. A pattern isn't a specific piece of code but rather a general concept for solving a particular problem. You can follow the pattern's details and implement a solution that fits the realities of your own program.

Patterns are often mistaken for algorithms because both concepts describe typical solutions to well-known problems. While an algorithm always defines a clear set of actions to achieve a goal, a pattern is a higher-level description of a solution. The code for the same pattern applied to two different programs may vary.

An analogy for an algorithm would be a cooking recipe: both provide clear steps to reach a goal. On the other hand, a pattern is more similar to a blueprint, as you can see what the end result and functions should be, but the exact order of implementation depends on you.

What Does the Pattern Consist Of?

Most patterns are described with a high degree of formality so that people can reproduce them in many contexts. Here are the sections that are typically present in the description of a pattern:

  • The pattern's purpose briefly explains the problem and the solution it provides.
  • The motivation goes into more detail about the problem and the solution the pattern offers.
  • The class structure showcases each part of the pattern and how they relate.

Some pattern catalogs list other useful details such as the pattern's applicability, implementation steps, and relationships with other patterns.

Pattern classification

When it comes to design patterns, they vary in complexity, level of detail, and their applicability to the entire system being designed. A useful analogy can be drawn from road construction: you can make an intersection safer by installing traffic lights or by building a multi-level interchange with underground pedestrian passages.

The most basic and low-level patterns are often referred to as "idioms." These are typically applied to a single programming language.

On the other end of the spectrum, the most universal and high-level patterns are architectural patterns. Developers can implement these patterns in virtually any programming language. Unlike other patterns, they can be used to design the architecture of an entire application.

Furthermore, all patterns can be classified based on their purpose. This article covers three general groups of patterns:

Creational Patterns: These provide object creation mechanisms that enhance flexibility and the reuse of existing code. Read Creational Design Patterns in System Development

Structural Patterns: They explain how to assemble objects and classes into larger structures while maintaining the flexibility and efficiency of the structure. Read Structural Design Patterns in System Development

Behavioral Patterns: These focus on effective communication and the assignment of responsibilities among objects. Read Behavioral Design Patterns in System Development

In essence, understanding the classification of design patterns allows developers to choose the right pattern for the specific problem they are trying to solve, ranging from low-level idioms to high-level architectural solutions. This versatility is a crucial aspect of the power of design patterns in software development.