{"id":6391,"date":"2022-06-30T21:10:08","date_gmt":"2022-06-30T21:10:08","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/an-introduction-into-design-patterns\/"},"modified":"2022-06-30T21:10:08","modified_gmt":"2022-06-30T21:10:08","slug":"an-introduction-into-design-patterns","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/an-introduction-into-design-patterns\/","title":{"rendered":"The Advantages of Patterns"},"content":{"rendered":"<p>Before I write about patterns in my upcoming posts, I have to answer one question first. What are the advantages of patterns? As you may assume, I see many advantages, but I boil them down to three points: well-defined terminology, improved documentation, and learning from the best.<\/p>\n<p><!--more--><\/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=\"324\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>I gave my first talks about design patterns. This was around 2002 &#8211; 2008. Why? <strong>Patterns are probably the most valuable and impactful abstraction in modern software development.<\/strong><\/p>\n<p>Now, let me write about the important stuff.<\/p>\n<h2>Advantages of Patterns<\/h2>\n<p>My argumentation is based on three facts: well-defined terminology, improved documentation and learning from the best.<\/p>\n<h3>Well-defined terminology<\/h3>\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=\"500\" height=\"217\" 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: 500px) 100vw, 500px\" \/><\/p>\n<p>Patterns establish a well-defined terminology. Imagine, I go for a run and I explain to you that I saw an asthoning animal. I explain it to you in my bad English. The animal had kind of the size of a cat and had fur and long ears. Its hind legs were extremely long. It could, therefore, jump three meters far and two meters high. While running, it could its direction quickly. You may guess which animal I saw: <a href=\"https:\/\/en.wikipedia.org\/wiki\/European_hare\"> a European hare<\/a>. Using the exact term helps a lot. Now you have the term and you can look it up on Wikipedia. This is for me the main benefit of patterns. We have well defined-terminology. We know, what we are talking about.<\/p>\n<p>Let&#8217;s make it more concrete. You want to implement a newsreader and ask me for my advice. The customer of your newsreaders should be automatically informed if some news happens. My answer can very verbose and explain that your newsreader should have a register and unregister functionality. Additionally, the newsreader stores all the customers and each customer should support a notify member function. When the newsreader publishes something, it goes through its list of customers and calls and notifies all of them. There is more to it. The newsreader could send the news, or only send that there is news in which the customer could be interested. I&#8217;m not done with my advice, but I stop here. In contrast, my answer could be one term: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Observer_pattern\">the observer pattern<\/a>. You can read the details in the literature.<\/p>\n<\/p>\n<h3>Improved documentation<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5302\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/Observer.png\" alt=\"Observer\" width=\"500\" height=\"190\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/Observer.png 579w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/Observer-300x114.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Let me distinguish between fine-grained and high-level documentation of software.<\/p>\n<h4>Fine-grained documentation<\/h4>\n<p>Honestly, I&#8217;m no fan of fine-grained source code documentation like the following:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ initialize a vector of strings<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> myStrVec <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #cc3300;\">\"12345\"<\/span>, <span style=\"color: #cc3300;\">\"123456\"<\/span>, <span style=\"color: #cc3300;\">\"1234\"<\/span>, <span style=\"color: #cc3300;\">\"1\"<\/span>, <span style=\"color: #cc3300;\">\"12\"<\/span>, <span style=\"color: #cc3300;\">\"123\"<\/span>, <span style=\"color: #cc3300;\">\"12345\"<\/span>};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ sort the vector of strings based on their length ascending<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>sort(myStrVec.begin(), myStrVec.end(), lessLength);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>On the contrary, your code should be expressive and read like prose:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> myStrVec <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #cc3300;\">\"12345\"<\/span>, <span style=\"color: #cc3300;\">\"123456\"<\/span>, <span style=\"color: #cc3300;\">\"1234\"<\/span>, <span style=\"color: #cc3300;\">\"1\"<\/span>, <span style=\"color: #cc3300;\">\"12\"<\/span>, <span style=\"color: #cc3300;\">\"123\"<\/span>, <span style=\"color: #cc3300;\">\"12345\"<\/span>};\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>sort(myStrVec.begin(), myStrVec.end(), [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> f, <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> s){<span style=\"color: #006699; font-weight: bold;\">return<\/span> f.length() <span style=\"color: #555555;\">&lt;<\/span> s.length();});\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The code tells the truth and is by definition always up to date.<\/p>\n<p>It happened so often in my career as a software developer that I should maintain legacy software and improve it. Typically, this software was very complex, and it took me a considerable amount of brain power to understand it. Sometimes I couldn&#8217;t even understand the software. You may guess, how happy I was that I found documentation in the source code that helped me to put the puzzle pieces together. Sadly, I recognized later, that the documentation was out of date and I invested my brain power in the wrong direction. I went back to square one. Fine-grained source code documentation becomes easily out of date. Out-of-date documentation is awful. Your code should be self-explanatory.<\/p>\n<h4>High-level documentation<\/h4>\n<p>Honestly, I&#8217;m a big fan of high-level documentation of software.<\/p>\n<p>For example, you provide a graphic in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unified_Modeling_Language\">UML <\/a>or <a href=\"https:\/\/en.wikipedia.org\/wiki\/Systems_Modeling_Language\">SysML <\/a>describing the architecture of the software and say, that you apply the reactor pattern in your software. The reactor pattern is an architectural pattern. It describes a proven solution to build event-driven applications that can accept multiple client requests simultaneously and distribute them to different service providers. Now, that I get the big picture, I can dive more into the details:<\/p>\n<ul>\n<li>Study the literature about the<a href=\"https:\/\/en.wikipedia.org\/wiki\/Reactor_pattern\"> reactor pattern<\/a><\/li>\n<li>Discuss its impact with my colleagues<\/li>\n<li>Identify the critical components of the reactor pattern in the software; There must be components such as a reactor, an event demultiplexer, events, and various event handlers to be part of the software. They use names like <code>handleEvents, registerHandler, removeHandler, select, <\/code>or <code>getHandle.<\/code><\/li>\n<\/ul>\n<p>Furthermore, to implement the various aspects of a reactor pattern, design patterns come to our rescue. For example, the event handler should be informed if a special event happens. An<a href=\"https:\/\/en.wikipedia.org\/wiki\/Observer_pattern\"> observer pattern<\/a> may here be the right choice. Additionally, you should document that you used the observer pattern to solve the challenge. This is very valuable high-level documentation.<\/p>\n<h3>Learning from the best<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignleft size-full wp-image-6390\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/man-5649929_1280.png\" alt=\"man 5649929 1280\" width=\"170\" height=\"239\" style=\"margin-right: 30px; margin-left: 10px; float: left;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/man-5649929_1280.png 913w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/man-5649929_1280-214x300.png 214w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/man-5649929_1280-730x1024.png 730w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/man-5649929_1280-768x1077.png 768w\" sizes=\"auto, (max-width: 170px) 100vw, 170px\" \/><\/p>\n<p>Patterns are just learning from the best. You pick the brain of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kent_Beck\">Kent Beck<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Jim_Coplien\">James Coplien<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Grady_Booch\">Grady Booch<\/a>, or <a href=\"https:\/\/en.wikipedia.org\/wiki\/Erich_Gamma\">Eric Gamma<\/a>, to name a few.<\/p>\n<p>Patterns are &#8220;code reuse&#8221; on a high level. This is the kind of &#8220;code reuse&#8221; that worked best. A pattern describes a typical challenge in a specific context and its proven solution. A pattern also answers the following questions:<\/p>\n<ul>\n<li>When should you not use the pattern?<\/li>\n<li>Which patterns are and can be considered instead?<\/li>\n<li>Where is the pattern used?<\/li>\n<li>Which variations of the pattern exist?<\/li>\n<\/ul>\n<p>Just imagine, how nice it would be if you design new software, and you don&#8217;t fall into each pitfall.<\/p>\n<h2>&nbsp;<\/h2>\n<h2>&nbsp;<\/h2>\n<h2>What&#8217;s Next?<\/h2>\n<p>Most software developers assume that the terms patterns and design patterns are interchangeable. This is, of course, wrong. Patterns is a broader term and include design patterns. My historical detour and first classification of patterns in my next post should make my point.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before I write about patterns in my upcoming posts, I have to answer one question first. What are the advantages of patterns? As you may assume, I see many advantages, but I boil them down to three points: well-defined terminology, improved documentation, and learning from the best.<\/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-6391","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\/6391","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=6391"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6391\/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=6391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}