Patterns don't live in isolation, they are in relation to each other. A relation can mean they are in contrast to each other, connected, build a sequence of patterns, build a repository of patter, or even a pattern language. Let's dive deeper into these relations.
The terms design pattern, algorithm, and framework have something in common. Let me differentiate them.
About Design Patterns, Algorithms, and Frameworks
Before I write about the difference between these three terms, here are their compact definitions.
- Design Pattern: "Each pattern is a three part rule, which expresses a relation between a certain context, a problem, and a solution." (Christopher Alexander)
- Algorithm: "In mathematics and computer science, an algorithm is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform computation." (https://en.wikipedia.org/wiki/Algorithm)
- Framework: "In computer programming, a software framework is an abstraction in which software, providing generic functionality, can be selectively changed by additional user-written code, thus providing application-specific software." (https://en.wikipedia.org/wiki/Software_framework)
Okay, let's dive deeper.
Design Patterns versus Algorithms
Based on the definitions, an algorithm is a finite sequence of steps to solve a specific problem, but a design pattern is a general solution to solve a problem in a specific context.
Design Patterns Versus Frameworks
First, a framework is based on the Hollywood Principle ("Don't call us, we call you"). The Hollywood Principe means that the control flow is dictated by the framework but not by the caller when he would use a library. The framework provides a minimal application that can only be extended by the user by overriding specific methods.
Finally, here is the differentiation of design patterns and frameworks from the book "Design Patterns: Elements of Reusable Object-Oriented Software" (Design Patterns):
Because patterns and frameworks have some similarities, people often wonder how or even if they differ. They are different in three major ways:
- Design patterns are more abstract than frameworks. Frameworks can be embodied in code, but only examples of patterns can be embodied in code. A strength of frameworks is that they can be written down in programming languages and not only studied but executed and reused directly. In contrast, the design patterns in this book have to be implemented each time they're used. Design patterns also explain the intent, trade-offs, and consequences of a design.
- Design patterns are smaller architectural elements than frameworks. A typical framework contains several design patterns, but the reverse is never true.
- Design patterns are less specialized than frameworks. Frameworks always have a particular application domain. A graphical editor framework might be used in a factory simulation, but it won't be mistaken for a simulation framework. In contrast, the design patterns in this catalog can be used in nearly any kind of application. While more specialized design patterns than ours are certainly possible (say, design patterns for distributed systems or concurrent programming), even these wouldn't dictate an application architecture like a framework would.
The following more theoretical observations about the relations of patterns are based on the book "Pattern-Oriented Software Architecture, Volume 5" (POSA 5). The authors of the book are Frank Buschmann, Kevlin Henny, and Douglas C. Schmidt.
Patterns are not islands. They are often in relation to each other. Consequentially, there are various ways to describe their relations.
Patterns typically complement each other. One pattern makes the design process started with another pattern more complete. This process also includes patterns that solve a similar design challenge.
The strategy pattern and the template method are patterns complements. Both are behavioral patterns from the classic "Design Patterns" book. They serve a similar purpose: variations of algorithms should be handled in a uniform way. The main difference is that the strategy pattern provides its implementation on the object level and uses object composition and delegation; the template method provides its implementation on the class level based on virtuality.
Often, compound patterns build a new pattern.
A typical example of a pattern compound is the architectural pattern Model-View-Controler (MVC). The MVC in divides the program logic of a user interface into the separate components model, view, and controller. The model manages the data and rules of the application. The view represents the data, and the controller interacts with the user.
Here are a few patterns from the "Design Patterns" book used in the MVC.
A pattern sequence is a typical sequence of patterns that can be applied in another design challenge.
Imagine you want to iterate through a tree and perform various operations on leaf nodes, such as
First, you need an iterator through this tree (iterator pattern). Of course, you have to distinguish between nodes having children or not. Nodes having children delegates the operations
just to their children. Nodes having no children display themselves (composite pattern). The visitor pattern comes into play to support various operations on the tree. All three patterns are often used in sequence.
A pattern collection is an organized repository of patterns.
In the end, there are thousands of patterns, and the need arises to collect and find them if necessary.
There are various ways to organize patterns. For example, you can collect them by their level (architectural pattern, design pattern, idioms), by their domain (avionic, finance, healthcare, .. ), or by their intent: The Design Patterns book and the POSA books are ordered by intent. For example, the following paragraph about pattern languages shows the ordering of POSA 4.
A pattern language describes a complete set of patterns for a specific domain. Its intention is to solve any design challenge in this specific domain. The book
- From Mud to Structure
- Distributed Infrastructure
- Event Demultiplexing and Dispatching
- Interface Partitioning
- Component Partitioning
- Application Control
- Object Interaction
- Adaption and Extension
- Modal Behavior
- Resource Management
- Database Access
I will write about many of these patterns in future posts.
An anti-pattern is a proven way to shoot yourself into your foot. The term anti-pattern was coined by Andrew Koenig and is the topic of my next post about patterns.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Dominik Vošček, and Rob North.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.
My special thanks to Embarcadero
My special thanks to PVS-Studio
My special thanks to Tipi.build
I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ - The Core Language
- C++ - The Standard Library
- C++ - Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org