About Algorithms, Frameworks, and Pattern Relations
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.
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
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.
Pattern Relations
Patterns are not islands. They are often in relation to each other. Consequentially, there are various ways to describe their relations.
Pattern Complements
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.
Pattern Compounds
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 POSA 1 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.
- The view observes (observer pattern) the model for changes.
- The controller is a strategy for handling user input (strategy pattern).
- Views can have subviews and are, therefore, components of the composite pattern.
Pattern Sequences
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 show
or count
.
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.
Pattern Collections
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.
Pattern Languages
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 Pattern-Oriented Software Architecture, Volume 4: A Pattern Language for Distributed Programming by Frank Buschmann, Kevlin Henney, and Douglas C. Schmidt presents such a pattern language. To make it short, the book presents more than 120 patterns grouped in the following way:
- From Mud to Structure
- Distributed Infrastructure
- Event Demultiplexing and Dispatching
- Interface Partitioning
- Component Partitioning
- Application Control
- Concurrency
- Synchronization
- Object Interaction
- Adaption and Extension
- Modal Behavior
- Resource Management
- Database Access
I will write about many of these patterns in future posts.
What’s next?
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, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
My special thanks to Embarcadero | |
My special thanks to PVS-Studio | |
My special thanks to Tipi.build | |
My special thanks to Take Up Code | |
My special thanks to SHAVEDYAKS |
Modernes C++ GmbH
Modernes C++ Mentoring (English)
Rainer Grimm
Yalovastraße 20
72108 Rottenburg
Mail: schulung@ModernesCpp.de
Mentoring: www.ModernesCpp.org
Modernes C++ Mentoring,
Leave a Reply
Want to join the discussion?Feel free to contribute!