{"id":6416,"date":"2022-07-28T09:15:58","date_gmt":"2022-07-28T09:15:58","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/classification-of-patterns\/"},"modified":"2022-07-28T09:15:58","modified_gmt":"2022-07-28T09:15:58","slug":"classification-of-patterns","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/classification-of-patterns\/","title":{"rendered":"Classification of Design Patterns"},"content":{"rendered":"<p>Patterns can be classified in various ways. The most prominent ones are the ones used in the books &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221;<\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pattern-Oriented_Software_Architecture\">&#8220;<span id=\"productTitle\" class=\"a-size-extra-large\">Pattern-Oriented Software Architecture, Volume 1<\/span><\/a>&#8220;.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6412\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/classification.png\" alt=\"classification\" width=\"650\" height=\"330\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/classification.png 1233w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/classification-300x152.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/classification-1024x520.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/classification-768x390.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Let me start in chronological order with the classification in the book <a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software<\/a>.<\/p>\n<h2>Design Patterns: Elements of Reusable Object-Oriented Software<\/h2>\n<p>The following table provides the first overview of the 23 patterns presented in the book.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6413\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/GoFOverview.jpg\" alt=\"GoFOverview\" width=\"600\" height=\"312\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/GoFOverview.jpg 921w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/GoFOverview-300x156.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/GoFOverview-768x399.jpg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>When you study the table, you can observe two classifications. First, creational patterns, structural patterns, and behavioral patterns, and second, class patterns and object patterns. The first classification is obvious, but not the second one.<\/p>\n<h3>Creational Patterns, Structural Patterns, and Behavioral Patterns<\/h3>\n<ul>\n<li><strong>Creational patterns<\/strong> deal with object creation in a well-defined way.<\/li>\n<li><strong>Structural patterns<\/strong> provide mechanisms to organize class and objects for larger structures.<\/li>\n<li><strong>Behavioral patterns<\/strong> deal with communication patterns between objects.<\/li>\n<\/ul>\n<p>The patterns that are written in bold fonts are the ones I used heavily in my past. Consequentially, I will write about them in future posts explicitly.<\/p>\n<h3>Destructional Patterns<\/h3>\n<p>You may see an asymmetry in this classification? Right! The book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software<\/a>&#8221; presents creational patterns, but not destructional patterns. So, what can you do?<\/p>\n<ul>\n<li>One of the authors of the Design Patterns book Jon Vlissides wrote about the destruction of a singleton in his book &#8220;<a href=\"https:\/\/www.amazon.de\/Pattern-Hatching-Design-Patterns-Applied\/dp\/0201432935\">Pattern Hatching: Design Patterns Applied<\/a>&#8221; (1998).<\/li>\n<li>You can study the mind-blowing book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Modern_C%2B%2B_Design\">Modern C++ Design: Generic Programming and Design Principle Applied<\/a>&#8221; (2001) by Andrei Alexandrescu to learn how to destroy a singleton.<\/li>\n<li>The excellent book &#8220;<a href=\"https:\/\/www.smallmemory.com\/\">Small Memory Software: Patterns for systems with limited memory<\/a>&#8221; (2000) by James Noble and Charles Weir dedicates an entire chapter to allocation.<\/li>\n<\/ul>\n<p>Now, I come to the not-so-obvious classification. You can distinguish the scope of a pattern.<\/p>\n<\/p>\n<h3>Class Patterns, and Object Patterns<\/h3>\n<p>I call class patterns and object patterns meta patterns in my patterns classes. I have two meta patterns in my mind when I want to solve a design challenge: inheritance versus composition. All 23 Design Patterns are only variations of the two key principles. Let me be more concrete. Inheritance is a class pattern, and composition is an object pattern.<\/p>\n<h4>Class Patterns<\/h4>\n<p>Class patterns apply classes and their subclass. They use the separation of interface and implementation and runtime dispatch with virtual function calls. Its functionality is hard-coded and available at compile time. They provide less flexibility and dynamic behavior, such as object patterns.<\/p>\n<h4>Object Patterns<\/h4>\n<p>Object patterns use the relationship of objects.<\/p>\n<p>You build your abstraction by composing it out of basic building blocks. This composition can be done at runtime. Consequentially, object patterns are more flexible and delay the decision until runtime.<\/p>\n<p>Honestly, inheritance is way too often used. Most of the time, the composition is the better choice.<\/p>\n<h3>Composition<\/h3>\n<p>When I gave my first design patterns classes around 2006, I gave them to the German automotive industry. To motivate composition, I created a generic car. Here it is.<\/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: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;memory&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> CarPart{\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Wheel<span style=\"color: #555555;\">:<\/span> CarPart{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Motor<span style=\"color: #555555;\">:<\/span> CarPart{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Body<span style=\"color: #555555;\">:<\/span> CarPart{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Trabi<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> TrabiWheel<span style=\"color: #555555;\">:<\/span> Wheel{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">30<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> TrabiMotor<span style=\"color: #555555;\">:<\/span> Motor{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">350<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> TrabiBody<span style=\"color: #555555;\">:<\/span> Body{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">550<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ VW<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> VWWheel<span style=\"color: #555555;\">:<\/span> Wheel{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">100<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> VWMotor<span style=\"color: #555555;\">:<\/span> Motor{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">500<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> VWBody<span style=\"color: #555555;\">:<\/span> Body{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">850<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ BMW<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> BMWWheel<span style=\"color: #555555;\">:<\/span> Wheel{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">300<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> BMWMotor<span style=\"color: #555555;\">:<\/span> Motor{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">850<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> BMWBody<span style=\"color: #555555;\">:<\/span> Body{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">1250<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Generic car<\/span>\r\n    \r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Car{\r\n    Car(std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Wheel<span style=\"color: #555555;\">&gt;<\/span> wh, std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Motor<span style=\"color: #555555;\">&gt;<\/span> mo, std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Body<span style=\"color: #555555;\">&gt;<\/span> bo)<span style=\"color: #555555;\">:<\/span> \r\n         myWheel(std<span style=\"color: #555555;\">::<\/span>move(wh)), myMotor(std<span style=\"color: #555555;\">::<\/span>move(mo)), myBody(std<span style=\"color: #555555;\">::<\/span>move(bo)){}\r\n         \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getPrice(){\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">4<\/span> <span style=\"color: #555555;\">*<\/span> myWheel<span style=\"color: #555555;\">-&gt;<\/span>getPrice() <span style=\"color: #555555;\">+<\/span> myMotor<span style=\"color: #555555;\">-&gt;<\/span>getPrice() <span style=\"color: #555555;\">+<\/span> myBody<span style=\"color: #555555;\">-&gt;<\/span>getPrice();\r\n    }\r\n\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Wheel<span style=\"color: #555555;\">&gt;<\/span> myWheel;\r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Motor<span style=\"color: #555555;\">&gt;<\/span> myMotor;\r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Body<span style=\"color: #555555;\">&gt;<\/span> myBody;\r\n\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    Car trabi(std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>TrabiWheel<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>TrabiMotor<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>TrabiBody<span style=\"color: #555555;\">&gt;<\/span>());\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Offer Trabi: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> trabi.getPrice() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    Car vw(std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>VWWheel<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>VWMotor<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>VWBody<span style=\"color: #555555;\">&gt;<\/span>());\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Offer VW: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> vw.getPrice() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    Car bmw(std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>BMWWheel<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>BMWMotor<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>BMWBody<span style=\"color: #555555;\">&gt;<\/span>());\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Offer BMW: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> bmw.getPrice() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    Car fancy(std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>TrabiWheel<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>VWMotor<span style=\"color: #555555;\">&gt;<\/span>(), std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>BMWBody<span style=\"color: #555555;\">&gt;<\/span>());\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Offer Fancy: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> fancy.getPrice() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}   \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Okay, I know from international discussion in my design patterns classes that you know a BMW and a VW, but may have no idea of a Trabi. The same holds for many young people in Germany. Trabi is short for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Trabant\">Trabant <\/a>and is stands for small cars produced in the former East German.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6414\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/model-car-2017764_1280.jpg\" alt=\"model car 2017764 1280\" width=\"400\" height=\"287\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/model-car-2017764_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/model-car-2017764_1280-300x215.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/model-car-2017764_1280-1024x734.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/model-car-2017764_1280-768x550.jpg 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Executing the program gives the expected result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6415\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/car.png\" alt=\"car\" width=\"300\" height=\"232\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/car.png 340w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/07\/car-300x232.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>It&#8217;s pretty straightforward to explain the program. The generic <code>Car<\/code> is a composition of four wheels, a motor, and a body. Each component is derived from the abstract base class CarPart and, therefore, has to implement the member function<code> getPrice. <\/code>The abstract base classes<code> Wheel, Motor, <\/code>and <code>Body <\/code>are not necessary but improve the structure of the car parts. When a customer wants to have a special car, the generic class Car delegates the <code>getPrice<\/code> call to its car parts.&nbsp;<\/p>\n<p>Of course, I applied bot meta patterns inheritance and composition together in this class to make the structure more type-safe and car parts easily pluggable.<\/p>\n<h3>A Thought Experiment<\/h3>\n<p>Now, let me reason more about composition and inheritance by answering the following questions:<\/p>\n<ol>\n<li>How many different cars can you make from existing vehicle components?<\/li>\n<li>How many classes do you need to solve the same complexity with inheritance?<\/li>\n<li>How easy\/complex is it to use inheritance\/composition to support a new car like Audi? For this, assume that all parts are at your disposal.<\/li>\n<li>How easy is it to change the price of a car part?<\/li>\n<li>Let&#8217;s say a customer wants a new, fancy car assembled from existing car components. When do you need to decide to assemble the new car based on inheritance or composition? Which strategy is applied at compile time and which at run time?<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Here is my reasoning:<\/p>\n<ol>\n<li>You can create 3 * 3 * 3 = 27 different cars out of the 14 components.<\/li>\n<li>You need 27 + 1 = 28 different classes to build 27 different cars. Each class has to encode its car parts into its class name, such as <code>TrabiWheelVWMotorBMWBody, TrabiWheelVWMotorVWBody, TrabiWheelVWMotorTrabiBody,<\/code> &#8230; . This becomes pretty fast unmaintainable. The same complexity holds when you apply multiple inheritances and give <code>TrabiWheelVWMotorBMWBody<\/code> three base classes. In this case you would have to derive from <code>TrabiWheel, VWMotor<\/code>, and <code>BMWBody<\/code>. Additionally, you would have to rename the member function<code> getPrice.<\/code><\/li>\n<li>In the composition strategy, you simply have to implement the three car parts for auto. This gives you the power to create 4 * 4&nbsp; * 4 = 64 different cars from 17 components. On the contrary, with inheritance, you have to extend the inheritance tree in all necessary branches.<\/li>\n<li>It&#8217;s pretty easy to change the price of a car part using composition. For inheritance, you have to traverse the entire inheritance tree and change the price in each spot.<\/li>\n<li>This is my main point. Thanks to composition, you can assemble the car parts during run time. In contrast, the inheritance strategy configures the car at compile time. Being a car seller means storing the car parts to assemble them when the customer comes. Using inheritance, you have to preproduce all configurations of your car.<\/li>\n<\/ol>\n<p>Of course, this was only my thought experiment. But this should make one point clear.&nbsp;<strong>To master combinatorial complexity, you have to compose your solution out of basic pluggable components.<\/strong> <strong> I call this the Lego principle.<\/strong><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Also, the book <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pattern-Oriented_Software_Architecture\">&#8220;<span id=\"productTitle\" class=\"a-size-extra-large\">Pattern-Oriented Software Architecture, Volume 1<\/span><\/a>&#8221; provides a very interesting classification of patterns. I will present it in my next post.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Patterns can be classified in various ways. The most prominent ones are the ones used in the books &#8220;Design Patterns: Elements of Reusable Object-Oriented Software&#8221; and &#8220;Pattern-Oriented Software Architecture, Volume 1&#8220;.<\/p>\n","protected":false},"author":21,"featured_media":6412,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6416","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\/6416","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=6416"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6416\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6412"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}