{"id":6387,"date":"2022-06-20T07:37:45","date_gmt":"2022-06-20T07:37:45","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/design-patterns-and-architectural-patterns-a-first-overview\/"},"modified":"2022-06-20T07:37:45","modified_gmt":"2022-06-20T07:37:45","slug":"design-patterns-and-architectural-patterns-a-first-overview","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/design-patterns-and-architectural-patterns-a-first-overview\/","title":{"rendered":"Design Patterns and Architectural Patterns with C++: A First Overview"},"content":{"rendered":"<p>Based on my last poll, &#8220;Which mentoring program should I implement next?&#8221; I recognized that there is a significant demand for writing about &#8220;Design Patterns and Architectural Patterns with C++&#8221;. Today, I would like to present to you my plan for future posts.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6385\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/patterns.png\" alt=\"patterns\" width=\"650\" height=\"332\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/patterns.png 1095w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/patterns-300x153.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/patterns-1024x524.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/patterns-768x393.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Although I call this introduction post &#8220;Design Patterns and Architectural Patterns with C++&#8221;, the focus of this article series is way broader. I also write about basic terminology, idioms, and concurrency patterns. The image serves two purposes.<\/p>\n<ul>\n<li>It gives you the first idea of my plan. This means you know what you can expect.<\/li>\n<li>You may miss one of your topics in my overview. Consequentially, write me an e-mail or, even better, write a guest post.<\/li>\n<\/ul>\n<p>I refine my plan as I go. Here is my first refinement.<\/p>\n<h2>Terminology<\/h2>\n<h3>Design Pattern<\/h3>\n<p>The term design patterns go back to Christoph Alexander, who wrote about architecture and urban planning: &#8220;<em>Each pattern is a three part rule, which expresses a relation between a certain context, a problem, and a solution<\/em>.&#8221; The classic &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software<\/a>&#8221; by Eric Gamma, Richard Helm, Ralph Johnson, and John Vlissides (short GOF) coined this term for software development.<\/p>\n<p>Roughly said, there are three types of patterns: architectural patterns, design patterns, and idioms.<\/p>\n<h3>Types of Patterns<\/h3>\n<p>Architectural patterns describe the fundamental structure of a software system and are often based on design patterns. An idiom is an implementation of an architecture or design pattern in a concrete programming language. This classification of patterns goes back to the second classic that is also a must-read:&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Pattern-Oriented_Software_Architecture\">Pattern-Oriented Software Architecture: A System of Patterns&#8221;<\/a> Frank Buschmann, Regine Meunier, Hans Rohnert, Peter Sommerlad, and Michael Stal (short POSA).<\/p>\n<h3>Anti-Patterns<\/h3>\n<p>We have Patterns and Anti-Patterns. Anti-Patterns are a proven way to shoot yourself into the foot.<\/p>\n<p>These are only the main parts of the terminology I will write about. I also will write about additional aspects of patterns, such as their advantages and disadvantages, history, and structure.<\/p>\n<\/p>\n<h2>Design Patterns<\/h2>\n<p>I make it short. The seminal book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software<\/a>&#8221; has 23 patterns. They are classified in two ways:<\/p>\n<ul>\n<li>Creational, structural, and behavioral<\/li>\n<li>Class patterns and object patterns<\/li>\n<\/ul>\n<p>The classification of class patterns and object patterns is essentially a classification in inheritance versus composition as a means to build abstractions out of existing abstractions. Not all of the 23 patterns are highly relevant today. Therefore, I will give a concise overview and code examples in modern C++ about the following patterns written in bold letters:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6386\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/15DesignPattern.jpg\" alt=\"15DesignPattern\" width=\"650\" height=\"282\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/15DesignPattern.jpg 807w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/15DesignPattern-300x130.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/15DesignPattern-768x333.jpg 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Okay, both classics GOF (1994) and POSA (1996) are a bit dated. What does this mean for modern C++? This is precisely the question I tackle in the next series of posts.<\/p>\n<h2>Idioms<\/h2>\n<p>An idiom is an implementation of an architecture or design pattern in a concrete programming language. We have many idioms in C++ such as<\/p>\n<ul>\n<li><strong>Copy-and-swap<\/strong><\/li>\n<li><strong>The rule of zero, fix, or six<\/strong><\/li>\n<li><strong>Hidden friends<\/strong><\/li>\n<li><strong>Resource acquisition is initialization (RAII)<br \/><\/strong><\/li>\n<li><strong>Dynamic polymorphism and static polymorphism<\/strong><\/li>\n<li><strong>Templates (curiously recurring template pattern (CRTP), expression templates, policy and traits, tag dispatching, type erasure, &#8230; )<\/strong><\/li>\n<\/ul>\n<p><strong>This is probably the part of my tour through patterns where I could benefit the most from your comments. Which other idioms do you know in C++?<\/strong><\/p>\n<h2>Architectural Patterns<\/h2>\n<p>Architectural patterns describe the fundamental structure of a software system and are often based on design patterns. I will present at least the following ones.<\/p>\n<ul>\n<li><strong>Pipes-and-Filters<\/strong>: Decomposes a complex task into a series of elementary tasks that can be composed<\/li>\n<li><strong>Layers<\/strong>: Split the software system into layers, where each layer has a certain responsibility and provides a service to a higher layer.<\/li>\n<li><strong>Model View Controller (MVC)<\/strong>: Decompose a (user) interface into the three components model, view, and controller\n<ul>\n<li>Model: the core of the application that registers views and controls; updates the view and the controller<\/li>\n<li>View: Presents the information to the user; get the data from the model<\/li>\n<li>Controller: Interacts with the user and updates the data<\/li>\n<\/ul>\n<\/li>\n<li><strong>Reactor<\/strong>: An event-driven application that can accept multiple client requests simultaneously and distribute them to different service providers.<\/li>\n<\/ul>\n<h2>Concurrency Patterns<\/h2>\n<p>A necessary requirement for a data race is a shared mutable state. Consequentially, the synchronization patterns deal with both issues. Here are the synchronization patterns I want to write about:<\/p>\n<ul>\n<li>Sharing\n<ul>\n<li><strong>Copied value<\/strong>: Copied data cannot be a victim of a data race.<\/li>\n<li><strong>Thread-specific storage<\/strong>: Enables global state within a thread.<\/li>\n<li><strong>Futures<\/strong>: Non-modifiable placeholder for a value set by a promise.<\/li>\n<\/ul>\n<\/li>\n<li>Mutation\n<ul>\n<li><strong>Scoped locking<\/strong>: RAII applied to locking.<\/li>\n<li><strong>Strategized locking<\/strong>: Use different locking strategies.<\/li>\n<li><strong>Thread-safe interface<\/strong>: Extends the critical section to an object.<\/li>\n<li><strong>Guarded suspension<\/strong>: Combine a lock to be acquired and a precondition to be satisfied before an operation can be executed.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Furthermore, we have to think about concurrent architecture.&nbsp;<\/p>\n<ul>\n<li><strong>Active object<\/strong>: Separates the method execution from the method call.<\/li>\n<li><strong>Monitor object<\/strong>: Synchronizes access to an object so that only one member function can be executed at any moment in time.<\/li>\n<\/ul>\n<h2>What&#8217;s next?<\/h2>\n<p>In my next post, I start my journey through the &#8220;Design Patterns and Architectural Patterns with C++&#8221;. First, I will write about design patterns&#8217; origins and history.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Based on my last poll, &#8220;Which mentoring program should I implement next?&#8221; I recognized that there is a significant demand for writing about &#8220;Design Patterns and Architectural Patterns with C++&#8221;. Today, I would like to present to you my plan for future posts.<\/p>\n","protected":false},"author":21,"featured_media":6385,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6387"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6387\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6385"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}