<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ruchit Surati&#039;s blog &#187; ooad</title>
	<atom:link href="http://www.ruchitsurati.net/index.php/tag/ooad/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ruchitsurati.net</link>
	<description>thoughts and ideas of a .net developer</description>
	<lastBuildDate>Wed, 23 Feb 2011 19:48:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Decorator Pattern with C#</title>
		<link>http://www.ruchitsurati.net/index.php/2010/07/23/decorator-pattern-with-c/</link>
		<comments>http://www.ruchitsurati.net/index.php/2010/07/23/decorator-pattern-with-c/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 17:34:47 +0000</pubDate>
		<dc:creator>Ruchit</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[design-patterns]]></category>
		<category><![CDATA[ooad]]></category>

		<guid isPermaLink="false">http://www.ruchitsurati.net/?p=431</guid>
		<description><![CDATA[Decorator pattern achieves a single objective of dynamically adding responsibilities to any object. Consider a case of a pizza shop. In the pizza shop they will sale a few pizza varieties and have toppings that customers can order additionally on top of a pizza. Customer can order a pizza and add as many as toppings [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Decorator pattern achieves a single objective of dynamically adding  responsibilities to any object.</strong></p></blockquote>
<p style="text-align: justify;">Consider a case of a pizza shop. In the pizza shop they will sale a few pizza varieties and have toppings that customers can order additionally on top of a pizza. Customer can order a pizza and add as many as toppings on the pizza. The cost of the ordered pizza will have to be calculated with pizza and all additional toppings. Now imagine a situation wherein if the pizza shop has to provide prices for each combination of pizza and topping. Even if there are four basic pizzas and 8 different toppings, the application would go crazy maintaining all concrete combination of pizzas and toppings. It is impractical to keep all combination in the menu. You&#8217;ll do enough to lose the customer when you expect him to go through such a long menu and do humongous brain-job.</p>
<p style="text-align: justify;">Here comes the decorator pattern.</p>
<p style="text-align: justify;">As per the decorator pattern, you will implement toppings as decorators and pizzas will be decorated by those toppings&#8217; decorators. Practically each customer would want toppings of his desire and final bill-amount will be composed of the base pizzas and additionally ordered toppings. Each topping decorator would know about the pizzas that it is decorating and it&#8217;s price. </p>
<p style="text-align: justify;">Here&#8217;s a code-example of explanation above.</p>
<pre class="brush: csharp; title: ;">

//All your pizzas will inherit from this BasePizza class to identify themselves as pizzas.
public abstract class BasePizza
{
    protected double myPrice;

    //This method will return the price of the pizza object.
    public virtual double GetPrice()
    {
        return this.myPrice;
    }
}

//All toppings will inherit from this ToppingsDecorator class to be able to be added to any pizza in the pizza-shop.
public abstract class ToppingsDecorator : BasePizza
{
    //Each topping will need to know to which pizza it is being added to.
    protected BasePizza pizza;
    public ToppingsDecorator(BasePizza pizzaToDecorate)
    {
        this.pizza = pizzaToDecorate;
    }

    //This method will return cumulative price of both pizza and the topping.
    public override double GetPrice()
    {
        return (this.pizza.GetPrice() + this.myPrice);
    }
}

class Program
{
    [STAThread]
    static void Main()
    {
        //Client-code

        //First - order your base pizza. We'll add toppings onto this pizza.
        Margherita pizza = new Margherita();
        Console.WriteLine(&quot;Plain Margherita: &quot; + pizza.GetPrice().ToString());

        //Add extra cheese.
        ExtraCheeseTopping moreCheese = new ExtraCheeseTopping(pizza);

        //Add some more cheese and make it double extra cheese.
        ExtraCheeseTopping someMoreCheese = new ExtraCheeseTopping(moreCheese);
        Console.WriteLine(&quot;Plain Margherita with double extra cheese: &quot; + someMoreCheese.GetPrice().ToString());

        //Like mushrroms ? Add that too.
        MushroomTopping moreMushroom = new MushroomTopping(someMoreCheese);
        Console.WriteLine(&quot;Plain Margherita with double extra cheese with mushroom: &quot; + moreMushroom.GetPrice().ToString());

        //Throw in some Jalapeno and make it sour and spicy.
        JalapenoToping moreJalapeno = new JalapenoToping(moreMushroom);

        //Add here's your final pizza.
        Console.WriteLine(&quot;Plain Margherita with double extra cheese with mushroom with Jalapeno: &quot; + moreJalapeno.GetPrice().ToString());

    }
}

public class Margherita : BasePizza
{
    public Margherita()
    {
        this.myPrice = 6.99;
    }
}

public class Gourmet : BasePizza
{
    public Gourmet()
    {
        this.myPrice = 7.49;
    }
}

public class ExtraCheeseTopping : ToppingsDecorator
{
    public ExtraCheeseTopping(BasePizza pizzaToDecorate)
        : base(pizzaToDecorate)
    {
        this.myPrice = 0.99;
    }
}

public class MushroomTopping : ToppingsDecorator
{
    public MushroomTopping(BasePizza pizzaToDecorate)
        : base(pizzaToDecorate)
    {
        this.myPrice = 1.49;
    }
}

public class JalapenoToping : ToppingsDecorator
{
    public JalapenoToping(BasePizza pizzaToDecorate)
        : base(pizzaToDecorate)
    {
        this.myPrice = 1.49;
    }
}
</pre>
<p>
Note that, <strong>GetPrice()</strong> method of Topping object would return cumulative price of both pizza and the topping.
</p>
<p><img src="http://ruchitsurati.net/myfiles/decorator.png" alt="alt text" /></p>
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-3020293614675538";
/* 300x250, created 14-July-2010 */
google_ad_slot = "4146580937";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ruchitsurati.net/index.php/2010/07/23/decorator-pattern-with-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

