<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Design-Patterns on Ravi Vats</title>
    <link>https://ravivats.github.io/tags/design-patterns/</link>
    <description>Recent content in Design-Patterns on Ravi Vats</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Sun, 16 Aug 2020 00:00:00 +0800</lastBuildDate>
    <atom:link href="https://ravivats.github.io/tags/design-patterns/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>HeadStart System Design — Part 1 — Design Pattern Intuitions</title>
      <link>https://ravivats.github.io/blog/headstart-system-design-p01-design-pattern-intuitions/</link>
      <pubDate>Sun, 16 Aug 2020 00:00:00 +0800</pubDate>
      <guid>https://ravivats.github.io/blog/headstart-system-design-p01-design-pattern-intuitions/</guid>
      <description>Intuitions about which design pattern to use based on the input you provide and the state and functionality you want.</description>
      <content:encoded><![CDATA[<p><img alt="headstart-system-design" loading="lazy" src="/images/headstart-system-design.png#center"></p>
<p>The other day, a friend asked me on how I go about choosing a design pattern in a given scenario, because it was not always very clear.</p>
<p>Some design patterns are similar in terms of functionality (i.e. factory vs. builder)</p>
<p>I presented my intuitions via chat, and at the end of the discussion we both realized that with these intuitions for some of the design patterns in mind, choosing next time among them will be easier.</p>
<p>Hence, I thought of documenting and sharing it with everyone.</p>
<h2 id="factory"><a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory</a></h2>
<p>Say that you are shopping from a factory outlet of a clothing company. You will only get t-shirts based on the size grades i.e. S, M, L, XL etc.</p>
<p>Hence, single input from our side i.e. S, M, L, XL etc. will define the whole t-shirt.</p>
<blockquote>
<p>Similarly, when you want to specify a single input argument like a keyword or an <strong><em>ENum</em></strong>, and get a variation of the main <strong><em>abstract class/interface TShirt</em></strong> created for you like <strong><em>SmallSizeTShirt</em></strong>, then you should go with <strong><em>Factory</em></strong> pattern.</p>
</blockquote>
<p>Similar example is covered here, with <strong><em>abstract class/interface Person</em></strong> and its custom variations <strong><em>Villager</em></strong> and <strong><em>CityPerson</em></strong> created for use that are created on the basic of a single input, the <strong><em>enum PersonType</em></strong>.</p>
<p><img alt="headstart-system-design-factory-design-pattern-example" loading="lazy" src="/images/2020-08-16/factory-design-pattern.png#center"></p>
<p>Factory Design Pattern in C#. Source: <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Wikipedia</a></p>
<h2 id="builder"><a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder</a></h2>
<p>Now say that instead of going to a factory outlet, you want to have something that fits you better, so you went to a tailor for custom tailored shirt.</p>
<p>Now, the tailor will take note of various other inputs like your chest size, your arm length, your shoulder length etc.</p>
<p>Hence, you can see that the tailor doesn’t have grades based on only one input like size, instead he has a number of input parameters and is makes unique shirts for each customer based on the input values for each of them.</p>
<p>Here, tailor is synonymous with a <strong><em>builder</em></strong>.</p>
<blockquote>
<p>Hence, when there are multiple inputs from your side to construct a variation of the main <strong><em>abstract class/interface Shirt</em></strong>, use <strong><em>ShirtBuilder</em></strong> to return a <strong><em>CustomShirt</em></strong> for you based on various inputs.</p>
</blockquote>
<p>Similar example is covered here, with ICarBuilder using various inputs like <strong><em>NumDoors</em></strong>, <strong><em>Colour</em></strong>, <strong><em>BrandName</em></strong> and <strong><em>ModelName</em></strong> and some code logic to construct a Ferrari 488 Spider Car.</p>
<p><img alt="headstart-system-design-builder-design-pattern-example" loading="lazy" src="/images/2020-08-16/builder-design-pattern.png#center"></p>
<p>Builder Design Pattern in C#. Source: <a href="https://en.wikipedia.org/wiki/Builder_pattern">Wikipedia</a></p>
<h2 id="singleton"><a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a></h2>
<p>Suppose, you have a DB connection to initialize, and you want this DB connection to be established with your DB, and then every time you want to query for some data in a function, you get the <strong><em>same Connection object in establishedConnection state</em></strong> returned, to quickly query data.</p>
<blockquote>
<p>In such scenarios where, not only do you want the same type of object to be returned, but you also want it to have a given state always, and for it to be a <strong><em>static method call</em></strong> to get that object, then you use the <strong><em>Singleton</em></strong> pattern.</p>
</blockquote>
<blockquote>
<p><strong><em>Additional feature</em></strong>: <em>A thread safe singleton can be created so that singleton property is maintained even in multi-threaded environment.</em>
<em>To make a singleton class thread-safe,</em> <strong><em>getInstance()</em></strong> <em>method is made synchronized so that one one thread can access it at a time.</em></p>
</blockquote>
<p><img alt="headstart-system-design-singleton-design-pattern-example" loading="lazy" src="/images/2020-08-16/singleton-design-pattern.png#center"></p>
<p>Singleton Design Pattern supporting multi-threading in Java. Source: <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Wikipedia</a></p>
<h2 id="mixins"><a href="https://en.wikipedia.org/wiki/Mixin">Mixins</a></h2>
<p>Suppose you are being told to make a system to accept pizza orders for a restaurant.</p>
<p>Here, <strong><em>class Pizza</em></strong> itself has many variations like <strong><em>ThinCrust</em></strong>, <strong><em>ThickCrust</em></strong>, <strong><em>ExtraCheese</em></strong> etc.</p>
<p>And, on-top of pizza you have a extra class like <strong><em>class Topping</em></strong> which has variations like <strong><em>Mushroom</em></strong>, <strong><em>BlackOlives</em></strong>, <strong><em>Pineapple</em></strong> etc.</p>
<p>Here, any <strong><em>Topping</em></strong> can go with any <strong><em>Pizza</em></strong> and vice versa.</p>
<blockquote>
<p>Hence, for this kind of multiple inheritance requirement where:</p>
<ol>
<li>You want to provide a lot of optional features for a class.</li>
<li>You want to use one particular feature in a lot of different classes.</li>
</ol>
<p>For cases like these, you can use the <strong><em>Mixin</em></strong> pattern.</p>
<p><strong>Note</strong>: <em>Mixins only exist in languages that support multiple-inheritance. You can’t do a mixin in Java or C#.</em></p>
</blockquote>
<p><img alt="headstart-system-design-mixin-design-pattern-example" loading="lazy" src="/images/2020-08-16/mixin-design-pattern.png#center"></p>
<p>Mixin Design Pattern (Pizza-Topping Example) in Python</p>
<p><img alt="headstart-system-design-mixin-design-pattern-example-output" loading="lazy" src="/images/2020-08-16/mixin-design-pattern-output.png#center"></p>
<p>Output for above example</p>
<h2 id="adapter"><a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a></h2>
<p>Suppose that you have to build an app interface for users to orders food from different restaurants. Here, menu shown to the user on your app is same for every restaurant, but the order information that each restaurant gets in slightly different.</p>
<blockquote>
<p>In cases like these, where one common input has to be mapped to different kinds to input or actions taken we should use the <strong><em>Adapter</em></strong> Pattern.</p>
<p><strong><em>Intuition</em></strong> is that adapters like charging adapter in real world do exactly the same thing. They send normal AC current coming at a fixed voltage-current range from the main socket into different charger pins for different devices like camera, phone, usb charger pin etc., all with their own voltage-current range.</p>
</blockquote>
<p>Similar example is covered here, where different charging styles for <strong><em>LightningPhone</em></strong> and <strong><em>MicroUsbPhone</em></strong> are defined via interfaces.</p>
<p>Class <strong><em>IPhone</em></strong> implements only <strong><em>LightningPhone</em></strong> interface (it can only be charged via Lightning) and class <strong><em>Android</em></strong> implements only <strong><em>MicroUsbPhone</em></strong> interface (it can only be charged via Micro USB).</p>
<p>Now, the class <strong><em>LightningToMicroUsbAdapter</em></strong> helps to implement <strong><em>MicroUsbPhone</em></strong> interface and helps charge class <strong><em>Android</em></strong> with that, which otherwise was not possible.</p>
<p><img alt="headstart-system-design-adapter-design-pattern-example" loading="lazy" src="/images/2020-08-16/adapter-design-pattern.png#center"></p>
<p>Adapter Design Pattern in Java. Source: <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Wikipedia</a></p>
<h2 id="reference-links">Reference Links</h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory pattern</a></li>
<li><a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder pattern</a></li>
<li><a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton pattern</a></li>
<li><a href="https://en.wikipedia.org/wiki/Mixin">Mixin Pattern</a>: <a href="https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful">Forum Discussion</a>, <a href="https://www.residentmar.io/2019/07/07/python-mixins.html">Blog</a></li>
<li><a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter Pattern</a></li>
<li>Other design patterns and their categories:
<ul>
<li><a href="https://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional-ebook/dp/B000SEIBB8">GangOfFour</a> (Classical book on Design Patterns)</li>
<li><a href="https://refactoring.guru/design-patterns/catalog">Blog</a></li>
</ul>
</li>
</ul>
<p>Thanks for reading till the last bit! Cheers to learning! :)</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
