What Is Design Pattern? : Just Another Article
Tags: cpp, design, programming, tutorial
Published: 2020-04-27 04:09:47

<!-- wp:paragraph -->
<p>After hitting a certain level of experience &amp; spending quite enough time in the industry, I have realised the importance of designing/architecting system &amp; software. So I have started looking into system/software design &amp; got to know nothing can better start than a Design Pattern. And the first thing I have done is googling "What is Design Pattern?" Hence got the idea of this article. </p>
<!-- /wp:paragraph -->


> /!\\\: This article has been originally published on my [blog](http://www.vishalchovatiya.com/what-is-design-pattern/). If you are interested in receiving my latest articles, [please sign up to my newsletter](http://eepurl.com/gDNybv).

<!-- wp:paragraph -->
<p>But as someone without a computer science background(I am from electronics background), learning them was a struggle. Every material, article, explanation or book was riddle with jargon to sift through. Some of them I still don't quite understand. I barely know how the <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/flyweight-design-pattern-in-modern-cpp/" target="_blank"><span style="color:#170cec" class="color">Flyweight</span></a> &amp; <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/double-dispatch-visitor-design-pattern-in-modern-cpp/" target="_blank"><span style="color:#170cec" class="color">Classical Visitor</span></a> pattern work and anyone who says they do is a liar.  </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So, after taking the online course, YouTube videos, lots of googling, tons compiling &amp; spaced repetition with learning &amp; unlearning. Here is what I have gained so far.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>What Is Design Pattern?</h2>
<!-- /wp:heading -->

![Software Design Architecture Stack](https://dev-to-uploads.s3.amazonaws.com/i/lil2tmv6sqigib7gab4a.png)

<!-- wp:paragraph -->
<p>From Wikipedia<a href="https://en.wikipedia.org/wiki/Software_design_pattern">:</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"very-light-gray"} -->
<p class="has-background has-very-light-gray-background-color">- In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. <br>- It is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations.<br>- Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Design Patterns establishes solutions to common problems which helps to keep code maintainable, extensible &amp; loosely coupled. </li><li>Developers have given a name to solutions which solve a particular type of problem. And this is how it all started. </li><li>The more one knows them, the easier it gets to solve all the problems we face.  </li><li>It is popularized by <a rel="noreferrer noopener" aria-label="Gang Of Four(1994) (opens in a new tab)" href="https://en.wikipedia.org/wiki/Design_Patterns" target="_blank"><span style="color:#170cec" class="color"><strong>G</strong>ang <strong>O</strong>f <strong>F</strong>our(1994)</span></a>  book.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>What Is Not Design Pattern?</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>It isn't code reuse, as it usually does not specify code. The actual implementation depends on the programming language and even the person that is doing it. </li><li>Design Pattern &amp; Principle(<a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/single-responsibility-principle-in-cpp-solid-as-a-rock/" target="_blank"><span style="color:#170cec" class="color">SOLID</span></a>) are two different things.</li><li>A Design Pattern is neither a static solution nor is it an algorithm, No hard rule of the coding standard.</li><li>Software Architecture is not a Design Pattern. Software Architecture dictates what's going to implemented &amp; where it will be put.  While Design Patterns states how it should be done.</li><li><strong>Design Patterns are not ready to code solutions they are more like a description of what the solution should look like</strong>.  What you should retain from Design Patterns is that the problem and the solution to the problem are closely related. They are both equally important to learn. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Why Do We Need Design Patterns?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As Software Developers, we often evaluate our code through some attributes like how clean, expressive, taking less memory footprint &amp; fast our code is. But the most important concern which we often neglect is that&nbsp;you should be able to easily change anything later. What you decide today could be irrelevant tomorrow. And your code should be flexible enough so that it's not too costly to alter. So Design Patterns are best practices to cover such attributes. For me, the core of Design Patterns consists of the following 6 rules:</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>They Are Proven Solutions</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Because Design Patterns often uses by many developers, you can be certain that they work. And not only that, you can be certain that they were revised multiple times and optimizations were probably implemented.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>They Are Easily Reusable</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Design Patterns document a reusable solution which can modify to solve multiple particular problems. As they are not tie-up to a specific problem.</li><li>For example, consider the <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/iterator-design-pattern-in-modern-cpp/" target="_blank"><span style="color:#170cec" class="color">Iterator Design Pattern</span></a>, it is reusable throughout STL despite container &amp; algorithm variation. Iterators are working like glue between container &amp; algorithm.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>They Are Expressive</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Design Patterns can explain a large solution quite elegantly. For instance, the <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/double-dispatch-visitor-design-pattern-in-modern-cpp/" target="_blank"><span style="color:#170cec" class="color">Visitor</span></a> pattern uses to perform a new operation on a range/group of classes. And thus, the standard library adopted such pattern with single function i.e. <a rel="noreferrer noopener" href="https://en.cppreference.com/w/cpp/utility/variant/visit" target="_blank"><code><span style="color:#170cec" class="color">std::visit</span></code></a> algorithm. Same goes for <code><a rel="noreferrer noopener" href="https://www.boost.org/doc/libs/1_62_0/libs/flyweight/doc/index.html" target="_blank"><span style="color:#170cec" class="color">boost::flyweight&lt;&gt;</span></a></code>.<br></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>They Ease Communication</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>When developers are familiar with Design Patterns, they can more easily communicate with one another about potential solutions to a given problem.</li><li>If you’re working with colleagues in a team of multiple developers, agree with them about the Design Patterns, as they can help you better with a problem. Also with regard to the maintenance of software, you should follow such procedures, as you make maintenance operations faster and more efficient.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>They Prevent the Need for Refactoring Code</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>If an application is written with Design Patterns in mind, it is often the case that you won’t need to refactor the code later on because applying the correct Design Pattern to a given problem is already an optimal solution. </li><li>If such solutions are then updates, they can seamlessly apply by any good software developer and do not cause any problems.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>They Lower the Size of the Codebase</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Because Design Patterns are usually elegant and optimal solutions, they usually require less code than other solutions. This does not always have to be the case as many developers write more code to improve understanding.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Why You Should Learn Design Patterns?</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>If you boil-down the definition of Object-Oriented Design, it combining data &amp; its operation into a context-bound entity(i.e. class/struct). And it stands true while designing an individual object.</li><li>But when you are designing complete software you need to take into the account that <ul><li><strong>Creational Design Patterns:</strong> How do those objects going to be instantiated/created?</li><li><strong>Structural Design Patterns:</strong> How do those objects combine with other object &amp; formalize bigger entity? which should also be scalable in future.</li><li><strong>Behavioural Design Patterns:</strong> You also need to think in terms of communication between those objects which can anticipate future changes easily &amp; with fewer side effects.</li></ul></li><li>Do you see where this lead us to? you need to <strong><em>think in terms of object everywhere considering maintainability, scalability, expressiveness &amp; stability</em></strong>. So in nutshell, this is a <strong><em>mindset for good coding</em></strong>.  And I am pretty sure if you are coming from C background, you don't have this mindset &amp; thought process.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Before Dive-Into the Design Patterns</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>But, before dive-into the Design Patterns you should learn some of the basic design principles called SOLID. SOLID is one of the most popular sets of design principles in object-oriented software development introduced by Robert C. Martin, popularly known as&nbsp;<a rel="noreferrer noopener" aria-label="Uncle Bob (opens in a new tab)" href="https://en.wikipedia.org/wiki/Robert_Cecil_Martin" target="_blank"><span style="color:#0007a3" class="color">Uncle Bob</span></a>. The SOLID principles comprise of these five principles:</p>
<!-- /wp:paragraph -->

1.  [**S**RP -- Single Responsibility Principle](http://www.vishalchovatiya.com/single-responsibility-principle-in-cpp-solid-as-a-rock/)
2.  [**O**CP -- Open/Closed Principle](http://www.vishalchovatiya.com/open-closed-principle-in-cpp-solid-as-a-rock/)
3.  [**L**SP -- Liskov Substitution Principle](http://www.vishalchovatiya.com/liskovs-substitution-principle-in-cpp-solid-as-a-rock/)
4.  [**I**SP -- Interface Segregation Principle](http://www.vishalchovatiya.com/interface-segregation-principle-in-cpp-solid-as-a-rock/)
5.  [**D**IP -- Dependency Inversion Principle](http://www.vishalchovatiya.com/dependency-inversion-principle-in-cpp-solid-as-a-rock/)

<!-- wp:list -->
<ul><li>Dev also refers to this SOLID design principle as "The First 5 Principles of Object-Oriented Design".</li><li>These principles also make it easy for developers to avoid code smells, easily refactor code, and are also a part of the agile or adaptive software development. </li></ul>
<!-- /wp:list -->

> <!-- wp:paragraph {"backgroundColor":"very-light-gray","className":"has-text-align-center has-cyan-bluish-gray-background-color"} -->
<p class="has-background has-very-light-gray-background-color has-text-align-center has-cyan-bluish-gray-background-color"><strong><em><span style="color:#313131" class="color">SOLID are "</span><span style="color:#000000" class="color">not principles to adopt</span>" but "<span style="color:#000000" class="color">frameworks to use</span>"</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2> Types of Design Patterns</h2>
<!-- /wp:heading -->

### ***Creational Design Patterns***

1.  **[Factory](http://www.vishalchovatiya.com/factory-design-pattern-in-modern-cpp/)**
2.  **[Builder](http://www.vishalchovatiya.com/builder-design-pattern-in-modern-cpp/)**
3.  **[Prototype](http://www.vishalchovatiya.com/prototype-design-pattern-in-modern-cpp/)**
4.  **[Singleton](http://www.vishalchovatiya.com/singleton-design-pattern-in-modern-cpp/)**

### ***Structural Design Patterns***

1.  **[Adapter](http://www.vishalchovatiya.com/adapter-design-pattern-in-modern-cpp/)**
2.  **[Bridge](http://www.vishalchovatiya.com/bridge-design-pattern-in-modern-cpp/)**
3.  **[Composite](http://www.vishalchovatiya.com/composite-design-pattern-in-modern-cpp/)**
4.  **[Decorator](http://www.vishalchovatiya.com/decorator-design-pattern-in-modern-cpp/)**
5.  **[Facade](http://www.vishalchovatiya.com/facade-design-pattern-in-modern-cpp/)**
6.  **[Flyweight](http://www.vishalchovatiya.com/flyweight-design-pattern-in-modern-cpp/)**
7.  **[Proxy](http://www.vishalchovatiya.com/proxy-design-pattern-in-modern-cpp/)**

### ***Behavioural Design Patterns***

1.  **[Chain of responsibility](http://www.vishalchovatiya.com/chain-of-responsibility-design-pattern-in-modern-cpp/)**
2.  **[Command](http://www.vishalchovatiya.com/command-design-pattern-in-modern-cpp/)**
3.  **[Interpreter](http://www.vishalchovatiya.com/interpreter-design-pattern-in-modern-cpp/)**
4.  **[Iterator](http://www.vishalchovatiya.com/iterator-design-pattern-in-modern-cpp/)**
5.  **[Mediator](http://www.vishalchovatiya.com/mediator-design-pattern-in-modern-cpp/)**
6.  **[Memento](http://www.vishalchovatiya.com/memento-design-pattern-in-modern-cpp/)**
7.  **[Observer](http://www.vishalchovatiya.com/observer-design-pattern-in-modern-cpp/)**
8.  **[State](http://www.vishalchovatiya.com/state-design-pattern-in-modern-cpp/)**
9.  **[Strategy](http://www.vishalchovatiya.com/strategy-design-pattern-in-modern-cpp/)**
10. **[Template Method](http://www.vishalchovatiya.com/template-method-design-pattern-in-modern-cpp/)**
11. **[Visitor](http://www.vishalchovatiya.com/double-dispatch-visitor-design-pattern-in-modern-cpp/)**

<!-- wp:heading -->
<h2>Benefits of Design Patterns</h2>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>Foresee &amp; rectify future problems easily.</li><li>Helps in maintaining binary compatibility with subsequent releases.</li><li>Just by following <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/single-responsibility-principle-in-cpp-solid-as-a-rock/" target="_blank"><span style="color:#170cec" class="color">SOLID Principles</span></a> helps greatly in agile or adaptive software development.</li><li>The solution facilitates the development of highly <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Cohesion_(computer_science)" target="_blank"><span style="color:#170cec" class="color">cohesive</span></a> modules with minimal coupling. Thus, increasing extensibility &amp; reusability.</li><li>There are some patterns like Facade, Proxy, etc which encapsulates the complexity in itself to provide easy &amp; intuitive interface to the client. Thus, making the overall system easier to understand &amp; reduce learning curve.</li><li>Design Patterns make communication between designers &amp; developers more crystal &amp; precise. A developer can immediately picture the high-level design in their heads when they refer to the name of the pattern used to solve a particular issue when discussing software design.</li></ol>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>What Next?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I'm not advocating to learn everything by heart, but you should try to understand as much as you can about these concepts as you will encounter them often in your work. By practising to implement them, you will understand better their use cases and the reasons behind them. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I hope to cover most of the classic Gang of Four Design Patterns throughout this series. I struggled to find beginner-friendly material while learning them, and hope these help others avoid the same fate. By the way, I will be using <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/21-new-features-of-modern-cpp-to-use-in-your-project/" target="_blank"><span style="color:#170cec" class="color">Modern C++</span></a> for all of the patterns. So you can also smell C++ from thought process &amp; some of the definitions(this line is hard for me to put it into the words). This is by no means that you can not apply these patterns to other languages. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>FAQs</h2>
<!-- /wp:heading -->

<!-- wp:yoast/faq-block {"questions":[{"id":"faq-question-1580442495058","question":["Do you need design pattern all the time? "],"answer":["Initially, you should not think about Design Pattern. ",{"type":"strong","props":{"children":[{"type":"em","props":{"children":["An expressive \u0026 less code is always the first line of defence"]}}]}},". You should not complicate the solution because complication is given by the problem."],"jsonQuestion":"Do you need design pattern all the time? ","jsonAnswer":"Initially, you should not think about Design Pattern. \u003cstrong\u003e\u003cem\u003eAn expressive \u0026amp; less code is always the first line of defence\u003c/em\u003e\u003c/strong\u003e. You should not complicate the solution because complication is given by the problem."},{"id":"faq-question-1580442498412","question":["Why you should learn the Design Patterns?"],"answer":["If you are a self-taught developer \u0026 does not expose to industry projects then you may not have thought process for using Object-Oriented Design. You can not think every aspect of design in terms of objects. In this case, Design Pattern will give you a new thought process of thinking everything in terms of objects. And if you follow it strictly, you will see your classes \u0026 software represent the Domain Specific Language."],"jsonQuestion":"Why you should learn the Design Patterns?","jsonAnswer":"If you are a self-taught developer \u0026amp; does not expose to industry projects then you may not have thought process for using Object-Oriented Design. You can not think every aspect of design in terms of objects. In this case, Design Pattern will give you a new thought process of thinking everything in terms of objects. And if you follow it strictly, you will see your classes \u0026amp; software represent the Domain Specific Language."}]} -->
<div class="schema-faq wp-block-yoast-faq-block"><div class="schema-faq-section" id="faq-question-1580442495058"><strong class="schema-faq-question">Do you need design pattern all the time? </strong> <p class="schema-faq-answer">Initially, you should not think about Design Pattern. <strong><em>An expressive &amp; less code is always the first line of defence</em></strong>. You should not complicate the solution because complication is given by the problem.</p> </div> <div class="schema-faq-section" id="faq-question-1580442498412"><strong class="schema-faq-question">Why you should learn the Design Patterns?</strong> <p class="schema-faq-answer">If you are a self-taught developer &amp; does not expose to industry projects then you may not have thought process for using Object-Oriented Design. You can not think every aspect of design in terms of objects. In this case, Design Pattern will give you a new thought process of thinking everything in terms of objects. And if you follow it strictly, you will see your classes &amp; software represent the Domain Specific Language.</p> </div> </div>
<!-- /wp:yoast/faq-block -->

[Have Any Suggestions, Query or Wants to Say `Hi`? Take the Pressure Off, You Are Just a Click Away.](http://www.vishalchovatiya.com/contact-2/)🖱️