<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sheikh Sajid on Medium]]></title>
        <description><![CDATA[Stories by Sheikh Sajid on Medium]]></description>
        <link>https://medium.com/@sheikhsajid?source=rss-968c4a4521b7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*7DQJqibH3lFlnE2q.</url>
            <title>Stories by Sheikh Sajid on Medium</title>
            <link>https://medium.com/@sheikhsajid?source=rss-968c4a4521b7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 01:28:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sheikhsajid/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Design Patterns in Python Part 1: The Strategy Pattern]]></title>
            <link>https://medium.com/@sheikhsajid/design-patterns-in-python-part-1-the-strategy-pattern-54b24897233e?source=rss-968c4a4521b7------2</link>
            <guid isPermaLink="false">https://medium.com/p/54b24897233e</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sheikh Sajid]]></dc:creator>
            <pubDate>Wed, 15 Nov 2017 13:18:25 GMT</pubDate>
            <atom:updated>2018-05-30T13:53:33.192Z</atom:updated>
            <content:encoded><![CDATA[<h3>What are design patterns?</h3><p>Most programmers are faced with a number of recurring problems as they write object oriented code. In an attempt to standardize the solutions to these problems, four software engineers, during the 90’s, got together and identified the most common patterns of problems that occur in object oriented programming. Then they formulated model solutions to these common problems in a book called <strong>“Design Patterns: Elements of Reusable Object-Oriented Software” aka “The Gang of Four (GoF)”</strong>. It is still the authoritative reference for everything relating to software design patterns.</p><blockquote>In layman&#39;s terms, A Design Pattern describes a problem and a general approach to solving it.</blockquote><p>If we use design patterns, our programs will be easily understandable and extensible by those who come after us.</p><h4>Classification of Design Patterns</h4><ol><li>Creational Patterns: These are concerned with creating objects</li><li>Structural Patterns: These patterns describe relationship between objects</li><li>Behavioral Patterns: Interaction between different objects</li></ol><h3>The Strategy Pattern</h3><p>The strategy pattern (aka “The Policy Pattern) is one of the most frequently used Behavioral Pattern out there. It is also one of the simplest.</p><p>The main goal of this pattern is to enable a client class to choose between different algorithms or procedures to complete the same task. This way, different algorithms can be swapped in and out without complicating things. To do this, a number of Strategy classes are created that have the same interface and contain the implementation details of the specific algorithms.</p><blockquote>It decouples the client class from the class that is actually implementing the details of the algorithm.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*-SnLBoi-xwH3RC6tWLyKug.png" /><figcaption>Photo — <a href="https://sourcemaking.com/design_patterns/strategy">sourcemaking.com</a></figcaption></figure><p>You can imitate this functionality using <strong>inheritance </strong>as long as the number of algorithms and sub classes are limited. But, more often than not, <strong>composing </strong>an object from different strategies is better than creating a complex inheritance hierarchy. This might result in a <strong>explosion of classes </strong>caused by subclassing for every possible combination of procedures or algorithms.</p><h4>Example</h4><p>Suppose, you have two types of ducks in your program, <strong>LoudDuck </strong>and <strong>GentleDuck</strong>. The <strong>LoudDuck </strong>class, in turn, has sub types <strong>VillageDuck </strong>and <strong>ToyDuck.</strong> And <strong>GentleDuck </strong>has a subtype <strong>CityDuck</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cac3bfe832320a643c2b4b24a5f0e726/href">https://medium.com/media/cac3bfe832320a643c2b4b24a5f0e726/href</a></iframe><p>Up until this point everything looks fine. But now, your client changes her mind and wants to you to include a new type of duck, <strong>RobotDuck. </strong>She further informs you that,</p><ol><li><strong>RobotDuck </strong>is-a <strong>LoudDuck. </strong>That’s fine, you can just inherit from the <strong>LoudDuck </strong>class.</li><li>It also needs to have a <strong>“lights-on”</strong> method like <strong>ToyDuck</strong>. Now this is the tricky part. There is no straightforward way of doing this using inheritance. You cannot inherit from <strong>ToyDuck </strong>because <strong>RobotDuck </strong>does not have a <em>is-a</em> relationship with <strong>ToyDuck. </strong>Even if it did, <strong>ToyDuck </strong><em>is-a</em> <strong>GentleDuck </strong>and <strong>RobotDuck </strong>needs to be a <strong>LoudDuck</strong>.</li></ol><p>It is not impossible to write the program using inheritance. However, you would have to introduce many more classes and create a relatively complex inheritance hierarchy just implement this simple new feature. Also, bear in mind that this is a very rudimentary example, real world applications will usually have much more complex structures which will complicate things even further. One other option is to just copy the method from <strong>ToyDuck </strong>to<strong> RobotDuck, </strong>which is even worse.</p><p>However, if you use the <strong>Strategy pattern</strong>, you can easily implement this program with minimal effort:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*xR_w3qDOpVDwfRgXs3YfiA.png" /><figcaption>Structure of the Duck class and its strategies</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/106d5c4e0ae6ce4ad308eee99d85f49c/href">https://medium.com/media/106d5c4e0ae6ce4ad308eee99d85f49c/href</a></iframe><p>As you can see, we are basically taking different strategy objects and “composing” other objects by using them as the building blocks. It is similar to using different types of Lego bricks to build structures. We can do this by having the strategy objects as attributes of the Duck class. They are passed to the constructor of Duck in <strong>main.py</strong>. The strategies and their abstract interfaces are defined in <strong>strategy.py</strong>.</p><p>One other way of doing this in Python is by implementing the strategies as functions and then passing these functions to the constructors. But using interfaces is the “standard” way of taking advantage of the Strategy Pattern.</p><h4>Other useful resources:</h4><ol><li><a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124">Head First Design Patterns</a></li><li><a href="https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612">Design Patterns: Elements of Reusable Object-Oriented Software</a></li><li>The <a href="https://m.youtube.com/watch?t=1291s&amp;v=v9ejT8FO-7I">Strategy Pattern — Design Patterns</a> video on YouTube by <a href="https://m.youtube.com/channel/UCbF-4yQQAWw-UnuCd2Azfzg">Christopher Okhravi</a></li><li><a href="https://sourcemaking.com/design_patterns/strategy">Source Making</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54b24897233e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>