<?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 Gautham Chowdary on Medium]]></title>
        <description><![CDATA[Stories by Gautham Chowdary on Medium]]></description>
        <link>https://medium.com/@gauthamchowdary?source=rss-10167691ceae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*rSFlp3iuxqHbxXxtBctpCA.jpeg</url>
            <title>Stories by Gautham Chowdary on Medium</title>
            <link>https://medium.com/@gauthamchowdary?source=rss-10167691ceae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:55:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gauthamchowdary/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[Decorator Design Pattern(C#)]]></title>
            <link>https://medium.com/@gauthamchowdary/decorator-design-pattern-c-95061b2d7797?source=rss-10167691ceae------2</link>
            <guid isPermaLink="false">https://medium.com/p/95061b2d7797</guid>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[decorator-pattern]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Gautham Chowdary]]></dc:creator>
            <pubDate>Sun, 05 Apr 2020 06:11:29 GMT</pubDate>
            <atom:updated>2020-04-06T15:02:34.164Z</atom:updated>
            <content:encoded><![CDATA[<p>Consider this use case. We are writing an application for a restaurant. In this restaurant you can either order an Entree by itself or an Entree with additional options. These additional options, for now, include either a drink or a side. The owner of this restaurant is experimenting with his business model and has asked us to be flexible about these options. There can be new additions or removal of existing options based on how people respond to it. But one thing the owner is sure about, every transaction require that you buy an Entree(Can’t have fries for a meal at this place 😒).</p><p>Here, we know that there are certain components which are standard and based on the situation, we need to dynamically <strong>decorate</strong> these standard components. A decorator pattern is well suited for this type of use-cases.</p><p>An Entree here is that standard component. Based on the situation, we can decorate the order with either a drink or a side. Let us start designing our classes with an interface.</p><pre>public interface IFood<br>{<br>    string GetDescription();<br>    double GetTotalCost();   <br>}</pre><p>The IFood interface requires two things. Get description method returns a description of items ordered. Get total cost method will returns the total cost of the order. Below is a default implementation of this interface.</p><pre>public class FoodImplemetation:IFood<br>{<br>   private string Description = &quot;Entree&quot;;<br>   private double Cost = 4;</pre><pre>   public string GetDescription()<br>   {<br>      return Description;<br>   }<br>   <br>   public double GetTotalCost()<br>   {<br>      return Cost;<br>   }<br>}</pre><p>At this point we have taken care of the fixed assumptions for our application. For the dynamic parts, we need to write an abstract decorator class which can be used by the specific decorators we desire to implement. Below is how our abstract decorator will look like.</p><pre>public abstract class Decorator<br>{<br>    private IFood Food;</pre><pre>    public Decorator(IFood food)<br>    {<br>        Food = food;<br>    }</pre><pre>    public string GetDescription()<br>    {<br>       return Food.GetDescription();<br>    }</pre><pre>    public double GetTotalCost()<br>    {<br>        return Food.GetTotalCost();<br>    }<br>}</pre><p>In the class above, the constructor accepts any concrete decoration we desire to implement. Below are the two implementations that we decided to implement for now. These two classes will add additional details to the description and update the cost of the order based on additional items customers chose to order. First, the addition of a drink which is shown below.</p><pre>public class FoodWithDrink : Decorator<br>{<br>    private string description = &quot; and a drink&quot;;<br>    private double cost = 1.5;<br>    <br>    public FoodWithDrink(IFood food):base(food)<br>    {<br>    }</pre><pre>    public string GetDescription()<br>    {<br>        return base.GetDescription() + description;<br>    }</pre><pre>    public double GetTotalCost()<br>    {<br>        return base.GetTotalCost() + cost;<br>    }<br>}</pre><p>Second, the addition of a side. Which, as shown below, will look very similar to the class above</p><pre>public class FoodWithSide : Decorator<br>{<br>    private string description = &quot; and a side&quot;;<br>    private double cost = 2.5;<br>    <br>    public FoodWithSide(IFood food):base(food)<br>    {<br>    }</pre><pre>    public string GetDescription()<br>    {<br>        return base.GetDescription() + description;<br>    }</pre><pre>    public double GetTotalCost()<br>    {<br>        return base.GetTotalCost() + cost;<br>    }<br>}</pre><p>The usefulness of the pattern comes from the fact that we can add any number of these decorators based on the dynamic needs of our requirements. Finally, let us tie all these components together in our Runner class.</p><pre>using System;</pre><pre>public class Runner<br>{<br>    public static void Main(string[] args)<br>    {<br>        FoodImplemetation Food = new FoodImplemetation();<br>        Console.WriteLine(String.Format(&quot;Total cost for {0} is {1}&quot;,Food.GetDescription(),Food.GetTotalCost()));</pre><pre>        FoodWithSide FoodWithSide = new FoodWithSide(Food);<br>        Console.WriteLine(String.Format(&quot;Total cost for {0} is   {1}&quot;,FoodWithSide.GetDescription(),FoodWithSide.GetTotalCost()));</pre><pre>        FoodWithDrink FoodWithDrink = new FoodWithDrink(Food); <br>        Console.WriteLine(String.Format(&quot;Total cost for {0} is {1}&quot;,FoodWithDrink.GetDescription(),FoodWithDrink.GetTotalCost()));<br>    }<br>}</pre><p>Let us take a close look at what’s happening in the Runner class. First, we created an instance of the default implementation of our interface. It does not have any decorators. So it should print the description and cost of nothing but our Entree. Second, we created an instance of FoodWithSide class and to this class we are passing our default implementation. The FoodWithSide class will decorate the object of our default implementation with addition details to reflect that a side was added to the order. The FoodWithDrink class will do the same. Below is the output when I run the program.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*kpegkWvOn1hSiunOWg_32Q.png" /></figure><p>Complete code can be found <a href="https://github.com/gauthambrahma/design-patterns">here</a>.</p><p>Hope you enjoyed this article about design patterns! This is my first article on medium🥳. If you have any feedback please leave me a comment.😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95061b2d7797" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>