


      <rss version="2.0">
         <channel>
            <title><![CDATA[Vinay Rao | Design Patterns]]></title>
            <link>http://www.simplyvinay.com/</link>
            <description>My Personal Website</description>
            <copyright>Copyright 2005 by Vinay Rao</copyright>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. All Links]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>Here are the links to all of the 23 GOF design patterns that I have been writing till now. Its in the order that I have been posting.</p>  <p>Structural Patterns</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/14/Design-Patterns-for-Dummies.-The-Decorator-Pattern.aspx">Decorator Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/15/Design-Patterns-for-Dummies.-The-Proxy-Pattern.aspx">Proxy Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/16/Design-Patterns-for-Dummies.-The-Bridge-pattern.aspx">Bridge Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/17/Design-Patterns-for-Dummies.-The-Composite-Pattern.aspx">Composite Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/18/Design-Patterns-for-Dummies.-The-Flyweight-Pattern.aspx">Flyweight Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/19/Design-Patterns-for-Dummies.-The-Adapter-Pattern.aspx">Adapter Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/21/Design-Patterns-for-Dummies.-The-Facade-Pattern.aspx">Façade Pattern</a></li> </ol>  <p>Creational Patterns</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/24/Design-Patterns-For-Dummies.-The-Prototype-Pattern.aspx">Prototype Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/25/Design-Patterns-For-Dummies.-The-Factory-Method-Pattern.aspx">Factory Method Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/26/Design-Patterns-for-Dummies.-The-Singleton-Pattern.aspx">Singleton Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/27/Design-Patterns-for-Dummies.-The-Abstract-Factory-Pattern.aspx">Abstract Factory Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/28/Design-Patterns-for-Dummies.-The-Builder-Pattern.aspx">Builder Pattern</a></li> </ol>  <p>Behavioral Patterns</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx">Command Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/35/Design-Patterns-for-Dummies.-The-Iterator-Pattern.aspx">Iterator Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/36/Design-Patterns-for-Dummies.-The-Mediator-Pattern.aspx">Mediator Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/37/Design-Patterns-for-Dummies.-The-Observer-Pattern.aspx">Observer Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/38/Design-Patterns-for-Dummies.-The-Visitor-Pattern.aspx">Visitor Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/39/Design-Patterns-for-Dummies.-The-Interpreter-Pattern.aspx">Interpreter Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/40/Design-Patterns-for-Dummies.-The-Memento-Pattern.aspx">Memento Pattern</a></li> </ol>  <p>These writings doesn't dwell into the patterns deeply and are written just to give an idea of what they are ( for dummies remember ). The real challenge is identifying the problem rather than the pattern implementations. Although there are as many patterns as discussed, it is not necessary to implement all of them in any project. We’ll have to contemplate on the problem and see what fits in to our project.</p>  <p>Its was fun going through all off these patterns and hope you readers have enjoyed and learnt a few things from it. </p>  <p>P.S You can find the UML class diagrams to all of these patterns from <a href="http://www.dofactory.com/Patterns/Patterns.aspx" target="_blank">dofactory</a> site</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/41/Design-Patterns-for-Dummies.-All-Links.aspx]]></link>
         <pubDate>Mon, 16 Mar 2009 00:00:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Memento Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>I have been writing about the GOF design patterns for quite some time now, and, this would be the last of the series. I will be writing about the Memento pattern today which is&#160; a behavioral pattern. You can read about the other patterns from the following links.</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx">Command Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/35/Design-Patterns-for-Dummies.-The-Iterator-Pattern.aspx">Iterator Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/36/Design-Patterns-for-Dummies.-The-Mediator-Pattern.aspx">Mediator Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/37/Design-Patterns-for-Dummies.-The-Observer-Pattern.aspx">Observer Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/38/Design-Patterns-for-Dummies.-The-Visitor-Pattern.aspx">Visitor Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/39/Design-Patterns-for-Dummies.-The-Interpreter-Pattern.aspx">Interpreter Pattern</a></li> </ol>  <p>You can read about the Structural patterns <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br />You can read about the Creational patterns <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p><strong>The Memento</strong></p>  <p>The memento pattern deals with capturing an object’s internal state and saving it externally so that the internal state of that object can be restored later. This pattern is usually used in computer games where the state of the game can be saved, and can be started from the same point. The following example illustrates the use of memento pattern.</p>  <p>The components that make up the memento pattern are the O<strong>riginator</strong>, for which the state has to be saved, the C<strong>aretaker</strong> which is a class that holds the mementos and the M<strong>emento</strong> which holds the state of the originator.</p>  <pre class="ncode"><div style="font-weight: bold; font-size: 10pt; background: #1e1e1e; color: silver; font-family: consolas"><p style="margin: 0px"><span style="color: #cc7832">namespace</span> PatternsConsole</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">class</span> <span style="color: #ffc66d">MementoPatter</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Originator</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Serializable</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">class</span> <span style="color: #ffc66d">PersonInfo</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">string</span> Name { <span style="color: #cc7832">get</span>; <span style="color: #cc7832">set</span>; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">float</span> Salary { <span style="color: #cc7832">get</span>; <span style="color: #cc7832">set</span>; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #ffc66d">Memento</span> SetMemento()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> memento = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">Memento</span>();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> memento.Save( <span style="color: #cc7832">this</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> GetMemento( <span style="color: #ffc66d">Memento</span> memento )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> info = memento.Restore() <span style="color: #cc7832">as</span> <span style="color: #ffc66d">PersonInfo</span>;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">if</span> ( info != <span style="color: #cc7832">null</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = info.Name;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Salary = info.Salary;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Memento</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Serializable</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">class</span> <span style="color: #ffc66d">Memento</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">readonly</span> <span style="color: #ffc66d">MemoryStream</span> stream = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">MemoryStream</span>();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">readonly</span> <span style="color: #ffc66d">BinaryFormatter</span> formatter = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">BinaryFormatter</span>();</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #ffc66d">Memento</span> Save( <span style="color: #cc7832">object</span> obj )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; formatter.Serialize( stream, obj );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> <span style="color: #cc7832">this</span>;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">object</span> Restore()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.Seek( <span style="color: #6897bb">0</span>, <span style="color: #6897bb">SeekOrigin</span><span style="font-weight: normal">.Begin );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> obj = formatter.Deserialize( stream );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.Close();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> obj;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// CareTaker</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">class</span> <span style="color: #ffc66d">CareTaker</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #ffc66d">Memento</span> Memento { <span style="color: #cc7832">get</span>; <span style="color: #cc7832">set</span>; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #cc7832">static</span> <span style="color: #cc7832">void</span> Main()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> personInfo = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">PersonInfo</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = <span style="color: #a5c25c">&quot;Person1&quot;</span>, Salary = <span style="color: #6897bb">5000</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Store internal state</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> careTaker = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">CareTaker</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Memento = personInfo.SetMemento()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Console</span><span style="font-weight: normal">.WriteLine( </span><span style="color: #a5c25c">&quot;Saved State : \nName = {0}, Salary = {1}&quot;</span>,</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; personInfo.Name, personInfo.Salary );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Change originator</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; personInfo.Name = <span style="color: #a5c25c">&quot;Person2&quot;</span>;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; personInfo.Salary = <span style="color: #6897bb">10000</span>;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Console</span><span style="font-weight: normal">.WriteLine( </span><span style="color: #a5c25c">&quot;Changed Sate : \nName = {0}, Salary = {1}&quot;</span>,</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; personInfo.Name, personInfo.Salary );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Restore saved state</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; personInfo.GetMemento( careTaker.Memento );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Console</span><span style="font-weight: normal">.WriteLine( </span><span style="color: #a5c25c">&quot;Restored State : \nName = {0}, Salary = {1}&quot;</span>,</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; personInfo.Name, personInfo.Salary );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Output :</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Saved State :</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Name = Person1, Salary = 5000</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Changed Sate :</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Name = Person2, Salary = 10000</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Restored State :</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #5eaeae">// Name = Person1, Salary = 5000</span></p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Console</span><span style="font-weight: normal">.ReadLine();</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<p>The memento pattern can be used when we need to save an object’s state and retrieve it later or when we don't want to expose an objects state directly.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/40/Design-Patterns-for-Dummies.-The-Memento-Pattern.aspx]]></link>
         <pubDate>Wed, 11 Mar 2009 13:47:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Interpreter Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>Following up on the series on design patterns, I will be writing about the Interpreter pattern today. Interpreter is&nbsp; a behavioral pattern. You can read about the other patterns from the following links.</p>  <ol>   <li><a href="/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a></li>    <li><a href="/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a></li>    <li><a href="/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a></li>    <li><a href="/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a></li>    <li><a href="/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx">Command Pattern</a></li>    <li><a href="/Post/35/Design-Patterns-for-Dummies.-The-Iterator-Pattern.aspx">Iterator Pattern</a></li>    <li><a href="/Post/36/Design-Patterns-for-Dummies.-The-Mediator-Pattern.aspx">Mediator Pattern</a></li>    <li><a href="/Post/37/Design-Patterns-for-Dummies.-The-Observer-Pattern.aspx">Observer Pattern</a></li>    <li><a href="/Post/38/Design-Patterns-for-Dummies.-The-Visitor-Pattern.aspx">Visitor Pattern</a></li> </ol>  <p>You can read about the Structural patterns <a href="/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br>You can read about the Creational patterns <a href="/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p><strong>The Interpreter</strong></p>  <p>The interpreter pattern deals with interpretation of instructions defined for some specific purpose. The instructions are given as notations which are precise and can be defined in terms of grammar. For the sake of example, the notation in the following example is taken to be “PCCCCPC”. We will have to parse this expression and interpret it. </p>  <p>The components that make up an interpreter pattern are Context which contains information that is used by the interpreter and Element ( Term ) which provides an interface for the structure and the default operation for the interpreter. </p>  <p><em>Parsing</em>    <br>    <br>The interpreter pattern itself does not specify how the objects are to be built. For this purpose parsing is used, in which the the input is matched against a grammar to create the object structure. Once this is created the objects can passed to an interpreter to do its work. </p><pre class="code"><div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: consolas;"><span style="color: blue;">namespace</span> PatternsConsole<p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">InterpreterPattern</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//Context</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Context</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Input { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Context( <span style="color: blue;">string</span> input )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Input = input;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Term ( Includes a Parse method to parse the input )</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Element</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">char</span> GetElement();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">void</span> Interpret();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Parse( <span style="color: rgb(43, 145, 175);">Context</span> context )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> ( context.Input.Length == 0 )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> character <span style="color: blue;">in</span> context.Input)</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> ( character.CompareTo( GetElement() ) ==0 )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Interpret();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Producer</span> : <span style="color: rgb(43, 145, 175);">Element</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">char</span> GetElement()</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: rgb(163, 21, 21);">'P'</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> Interpret()</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Console</span>.WriteLine( <span style="color: rgb(163, 21, 21);">"Interpreted Producer"</span> );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Consumer</span> : <span style="color: rgb(43, 145, 175);">Element</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">char</span> GetElement()</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: rgb(163, 21, 21);">'C'</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> Interpret()</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Console</span>.WriteLine( <span style="color: rgb(163, 21, 21);">"Interpreted Consumer"</span> );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main()</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expression = <span style="color: rgb(163, 21, 21);">"PCCCCPC"</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> context = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Context</span>( expression );</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> tree = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: rgb(43, 145, 175);">Element</span>&gt;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Producer</span>(), <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Consumer</span>()</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Console</span>.WriteLine( <span style="color: rgb(163, 21, 21);">"Exression : {0}\n"</span>, expression );</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> ( <span style="color: blue;">var</span> element <span style="color: blue;">in</span> tree )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; element.Parse( context );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Output:</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Exression : PCCCCPC</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Interpreted Producer</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Interpreted Producer</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Interpreted Consumer</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Interpreted Consumer</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Interpreted Consumer</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Interpreted Consumer</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Interpreted Consumer</span></p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Console</span>.Read();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">}</p></div></pre>

<p>This pattern has performance issues and is rarely used. We can use the Interpreter pattern when we have a grammar to be interpreted and the grammar is fairly small and efficiency is not critical. </p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/39/Design-Patterns-for-Dummies.-The-Interpreter-Pattern.aspx]]></link>
         <pubDate>Tue, 03 Mar 2009 00:00:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Visitor Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>Following on the series of posts on design patterns I will be talking about the Visitor Pattern today. Visitor is behavioral pattern. You can read about the other behavioral patterns from the following links.</p>  <ol>   <li><a href="/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a> </li>    <li><a href="/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a> </li>    <li><a href="/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a> </li>    <li><a href="/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a> </li>    <li><a href="/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx">Command Pattern</a> </li>    <li><a href="/Post/35/Design-Patterns-for-Dummies.-The-Iterator-Pattern.aspx">Iterator Pattern</a> </li>    <li><a href="/Post/36/Design-Patterns-for-Dummies.-The-Mediator-Pattern.aspx">Mediator Pattern</a> </li>    <li><a href="/Post/37/Design-Patterns-for-Dummies.-The-Observer-Pattern.aspx">Observer Pattern</a> </li> </ol>  <p>You can read about the Structural patterns <a href="/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br>You can read about the Creational patterns <a href="/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p><strong>The Visitor</strong></p>  <p>Suppose you need to add a new method to a structure of classes, you might end up damaging the design. This is where the visitor pattern comes into play. The visitor pattern deals with performing new operations on all elements of an existing hierarchy of classes without altering the the classes themselves.&nbsp; </p>  <p>The visitor pattern is mainly made up of 2 components, the classes that make up the hierarchy and the methods that are applied to these classes. These methods are called as visitors. In the following example there is one visitor viz. SalaryVisitor which increments a persons salary.</p>  <p>The following example is based on the example shown in the Dofactory <a href="http://www.dofactory.com">site</a></p>  <pre class="code"><div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: consolas;"><p style="margin: 0px;"><span style="color: blue;">namespace</span> PatternsConsole</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">VisitorPattern</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Visitor</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Visitor</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> ReflectiveVisit( <span style="color: rgb(43, 145, 175);">Element</span> element )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> types = <span style="color: blue;">new</span>[] { element.GetType() };</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> methodInfo = GetType().GetMethod( <span style="color: rgb(163, 21, 21);">"Visit"</span>, types );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> ( methodInfo != <span style="color: blue;">null</span> )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; methodInfo.Invoke( <span style="color: blue;">this</span>, <span style="color: blue;">new</span> <span style="color: blue;">object</span>[] { element } );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// Element</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Element</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">void</span> Accept( <span style="color: rgb(43, 145, 175);">Visitor</span> visitor );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// ConcreteElement</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Person</span> : <span style="color: rgb(43, 145, 175);">Element</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Designation { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">double</span> Salary { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Person( <span style="color: blue;">string</span> name, <span style="color: blue;">string</span> designation, <span style="color: blue;">double</span> salary )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name = name;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Designation = designation;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Salary = salary;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> Accept( <span style="color: rgb(43, 145, 175);">Visitor</span> visitor )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; visitor.ReflectiveVisit( <span style="color: blue;">this</span> );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// ElementType</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Developer</span> : <span style="color: rgb(43, 145, 175);">Person</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Developer( <span style="color: blue;">string</span> name, <span style="color: blue;">double</span> salary ) :</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">base</span>( name, <span style="color: rgb(163, 21, 21);">"Developer"</span>, salary )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// ElementType</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Tester</span> : <span style="color: rgb(43, 145, 175);">Person</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Tester( <span style="color: blue;">string</span> name, <span style="color: blue;">double</span> salary ) :</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">base</span>( name, <span style="color: rgb(163, 21, 21);">"Tester"</span>, salary )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// ConcreteVisitor</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">SalaryVisitor</span> : <span style="color: rgb(43, 145, 175);">Visitor</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Visit( <span style="color: rgb(43, 145, 175);">Developer</span> developer )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VisitMethod( developer );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Visit( <span style="color: rgb(43, 145, 175);">Tester</span> tester )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VisitMethod( tester );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> VisitMethod( <span style="color: rgb(43, 145, 175);">Element</span> element )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> employee = element <span style="color: blue;">as</span> <span style="color: rgb(43, 145, 175);">Person</span>;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> ( employee != <span style="color: blue;">null</span> )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; employee.Salary *= 1.50;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Console</span>.WriteLine( <span style="color: rgb(163, 21, 21);">"{0} {1}'s new income: {2:C}"</span>,</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; employee.GetType().Name, employee.Name,</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; employee.Salary );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main()</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// ObjectStructure</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> people = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: rgb(43, 145, 175);">Person</span>&gt;();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> developer = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Developer</span>( <span style="color: rgb(163, 21, 21);">"Foo"</span>, 1000 );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> tester = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Tester</span>( <span style="color: rgb(163, 21, 21);">"Bar"</span>, 1500 );</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; people.Add( developer );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; people.Add( tester );</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> ( <span style="color: blue;">var</span> person <span style="color: blue;">in</span> people )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; person.Accept( <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">SalaryVisitor</span>() );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//Output: Developer Foo's new income: $1,500.00</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Tester Bar's new income: $2,250.00</span></p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Console</span>.ReadLine();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">}</p></div></pre>

<p>The visitor pattern provides number of ways to modify the behavior of a hierarchy of classes without modifying the classes. We can use the visitor pattern when we have a hierarchy that is sealed or when there are many operations to perform on it or when we need the flexibility of defining new operations.</p>

<p>In my next post I will be writing about the Interpreter pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/38/Design-Patterns-for-Dummies.-The-Visitor-Pattern.aspx]]></link>
         <pubDate>Tue, 24 Feb 2009 00:00:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Observer Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Observer Pattern. You can read about rest of the patterns from the following links</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx">Command Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/35/Design-Patterns-for-Dummies.-The-Iterator-Pattern.aspx">Iterator Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/36/Design-Patterns-for-Dummies.-The-Mediator-Pattern.aspx">Mediator Pattern</a></li> </ol>  <p>You can read about the Structural patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br />You can read about the Creational patterns from <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p>The Observer pattern deals with providing a relation between objects such that when one objects changes its state, then all the other objects that have a relation to it are notified of the change. This could be achieved by using a single publisher of the state of object and many subscribers who may want the notification for the changed state.</p>  <p>The Observer is usually composed of 2 class viz. a Subject whose state may change periodically and the Observer itself who maybe notified of the change, if they wish to receive the state change information.</p>  <p>The following example is based on the example from Dofactory website</p>  <pre class="code"><div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">namespace</span> PatternsConsole</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">ObserverPattern</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">delegate</span> <span style="color: blue">void</span> <span style="color: #2b91af">ChangeEventHandler</span>&lt;TArg1, TArg2&gt;( TArg1 sender, TArg2 eventArgs );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 6</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ChangeEventArgs</span> : <span style="color: #2b91af">EventArgs</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Title { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 11</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> ChangeEventArgs( <span style="color: blue">string</span> name, <span style="color: blue">string</span> title )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = name;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Title = title;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 17</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 19</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Subject</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Blog</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">string</span> title;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 25</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Constructor</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Blog( <span style="color: blue">string</span> name, <span style="color: blue">string</span> title )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = name;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.title = title;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 32</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">event</span> <span style="color: #2b91af">ChangeEventHandler</span>&lt;<span style="color: #2b91af">Blog</span>, <span style="color: #2b91af">ChangeEventArgs</span>&gt; Change;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 34</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> OnChange( <span style="color: #2b91af">ChangeEventArgs</span> e )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( Change != <span style="color: blue">null</span> )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Change( <span style="color: blue">this</span>, e );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 42</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Attach( <span style="color: #2b91af">IObserver</span> observer )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Change += observer.Update;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 47</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Title</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">set</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; title = <span style="color: blue">value</span>;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnChange( <span style="color: blue">new</span> <span style="color: #2b91af">ChangeEventArgs</span>( Name, title ) );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 58</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Observer</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">interface</span> <span style="color: #2b91af">IObserver</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Update( <span style="color: #2b91af">Blog</span> sender, <span style="color: #2b91af">ChangeEventArgs</span> e );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 64</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// ConcreteObserver</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Observer</span> : <span style="color: #2b91af">IObserver</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 69</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Observer( <span style="color: blue">string</span> name )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = name;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 74</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Update( <span style="color: #2b91af">Blog</span> sender, <span style="color: #2b91af">ChangeEventArgs</span> e )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Notified {0} of {1}'s &quot;</span> + <span style="color: #a31515">&quot;post : {2}&quot;</span>, Name, e.Name, e.Title );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 80</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">static</span> <span style="color: blue">void</span> Main()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 83</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> rob = <span style="color: blue">new</span> <span style="color: #2b91af">Blog</span>( <span style="color: #a31515">&quot;Rob Conery&quot;</span>, <span style="color: #a31515">&quot;Weke Road&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 84</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rob.Attach( <span style="color: blue">new</span> <span style="color: #2b91af">Observer</span>( <span style="color: #a31515">&quot;Vinay&quot;</span> ) );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 85</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rob.Title = <span style="color: #a31515">&quot;Subsonic 3&quot;</span>;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 87</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rob.Title = <span style="color: #a31515">&quot;Storefront Finis&quot;</span>;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rob.Title = <span style="color: #a31515">&quot;Oxite Refactor&quot;</span>;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 89</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output : </span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 91</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Notified Vinay of Rob Conery's post : Subsonic 3</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 92</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 93</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Notified Vinay of Rob Conery's post : Storefront Finis</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 94</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 95</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Notified Vinay of Rob Conery's post : Oxite Refactor</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 96</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 97</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.ReadLine();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 98</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 99</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 100</span> }</p></div></pre>

<p>We can use the observer pattern when changes in one object needs to be propagated to other objects or when the object sending the changes does not need to know about the receivers.</p>

<p>In my next post I will be writing about the Visitor Pattern</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/37/Design-Patterns-for-Dummies.-The-Observer-Pattern.aspx]]></link>
         <pubDate>Tue, 17 Feb 2009 14:04:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Mediator Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Mediator Pattern. You can read about rest of the patterns from the following links</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx">Command Pattern</a></li>    <li><a href="http://www.simplyvinay.com/Post/35/Design-Patterns-for-Dummies.-The-Iterator-Pattern.aspx">Iterator Pattern</a></li> </ol>  <p>You can read about the Structural patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br />You can read about the Creational patterns from <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p>The Mediator patterns deals with providing a way for objects to communicate with each other without knowing each others identity. The participants involved in this pattern are a Mediator and Colleague. A concrete colleague is used to communicate with other colleagues through its mediator.</p>  <pre class="code"><div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">namespace</span> PatternsConsole</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">MediatorPattern</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">delegate</span> <span style="color: blue">void</span> <span style="color: #2b91af">Callback</span>( <span style="color: blue">string</span> message, <span style="color: blue">string</span> from );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 6</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Mediator</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Callback</span> respond;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 10</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Register( <span style="color: #2b91af">Callback</span> method )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; respond += method;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 15</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Send is implemented as a broadcast</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Send( <span style="color: blue">string</span> message, <span style="color: blue">string</span> from )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; respond( message, from );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 23</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">FooColleague</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Mediator</span> mediator;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">string</span> name;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 28</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> FooColleague( <span style="color: #2b91af">Mediator</span> mediator, <span style="color: blue">string</span> name )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.mediator = mediator;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.name = name;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mediator.Register( Receive );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 35</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> Receive( <span style="color: blue">string</span> message, <span style="color: blue">string</span> from )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( !<span style="color: blue">string</span>.Equals( from, name ) )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( name + <span style="color: #a31515">&quot; received from &quot;</span> + from + <span style="color: #a31515">&quot;: &quot;</span> + message );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 41</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Send( <span style="color: blue">string</span> message )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Send (From &quot;</span> + name + <span style="color: #a31515">&quot;): &quot;</span>);</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mediator.Send( message, name );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 47</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 49</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">BarColleague</span> : <span style="color: #2b91af">FooColleague</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> BarColleague( <span style="color: #2b91af">Mediator</span> mediator, <span style="color: blue">string</span> name )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>( mediator, name ) { }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 54</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Receive( <span style="color: blue">string</span> message, <span style="color: blue">string</span> from )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( !<span style="color: blue">string</span>.Equals( from, name ) )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( name + <span style="color: #a31515">&quot; received from &quot;</span> + from + <span style="color: #a31515">&quot;: &quot;</span> + message );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 61</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">static</span> <span style="color: blue">void</span> Main()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> mediator = <span style="color: blue">new</span> <span style="color: #2b91af">Mediator</span>();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 65</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> foo = <span style="color: blue">new</span> <span style="color: #2b91af">FooColleague</span>( mediator, <span style="color: #a31515">&quot;Foo&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> bar = <span style="color: blue">new</span> <span style="color: #2b91af">BarColleague</span>( mediator, <span style="color: #a31515">&quot;Bar&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 68</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foo.Send( <span style="color: #a31515">&quot;Hi Bar! This is a greeting through the mediator&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; bar.Send( <span style="color: #a31515">&quot;Hi Foo! Really!!&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 71</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output :</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Send (From Foo):</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 74</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Bar received from Foo: Hi Bar! This is a greeting through the mediator</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 75</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Send (From Bar):</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Foo received from Bar: Hi Foo! Really!!</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 78</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.ReadLine();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 81</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 82</span> }</p></div></pre>

<p>We can use the mediator pattern when we have objects are communicating in a complex way or when we have to protect the identities of objects who are communicating.</p>

<p>In my next post I will be writing about the Observer Pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/36/Design-Patterns-for-Dummies.-The-Mediator-Pattern.aspx]]></link>
         <pubDate>Mon, 09 Feb 2009 13:16:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Iterator Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Iterator Pattern. You can read about rest of the patterns from the following links</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx">Command Pattern</a> </li> </ol>  <p>You can read about the Structural patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br />You can read about the Creational patterns from <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p>The Iterator patterns deals with providing an easy way to access elements of a collection sequentially. To implement an iterator in C#, we can implement either IEnumerator or the IEnumerable interfaces.&#160; IEnumerator interface has 2 methods <em>GetEnumerator</em> and <em>Reset</em> which we would have to implement and an <em>Current</em> object which is used to maintain the position. The easier way of doing it is to implement the IEnumerable interface and implement the GetEnumerator method for it. In the following example I am using the yield return statement in the GetEnumerator method which saves us from maintaining the state.</p>  <pre class="code"><div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">namespace</span> PatternsConsole</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">IteratorPattern</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Collection</span>&lt;T&gt; : <span style="color: #2b91af">IEnumerable</span>&lt;T&gt;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">List</span>&lt;T&gt; items = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;T&gt;();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 8</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Add( T t )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; items.Add( t );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 13</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IEnumerator</span>&lt;T&gt; GetEnumerator()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">for</span> ( <span style="color: blue">int</span> i = 0; i &lt; Count; i++ )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">yield</span> <span style="color: blue">return</span> items[i];</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 21</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> Count</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span> { <span style="color: blue">return</span> items.Count; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 26</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IEnumerator</span> <span style="color: #2b91af">IEnumerable</span>.GetEnumerator()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> GetEnumerator();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 32</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Person</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 36</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Person( <span style="color: blue">string</span> name )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = name;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 42</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> people = <span style="color: blue">new</span> <span style="color: #2b91af">Collection</span>&lt;<span style="color: #2b91af">Person</span>&gt;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span>(<span style="color: #a31515">&quot;Person 1&quot;</span>),</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span>(<span style="color: #a31515">&quot;Person 2&quot;</span>),</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span>(<span style="color: #a31515">&quot;Person 3&quot;</span>),</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span>(<span style="color: #a31515">&quot;Person 4&quot;</span>),</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span>(<span style="color: #a31515">&quot;Person 5&quot;</span>)</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 53</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> ( <span style="color: blue">var</span> person <span style="color: blue">in</span> people )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( person.Name );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 58</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output :</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Person 1</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Person 2</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Person 3</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Person 4</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Person 5</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 65</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.ReadLine();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 68</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 69</span> }</p></div></pre>

<p>We can implement our iterator when we want iterations over a collection or we have many ways of traversing it or when there are many collections for traversing.</p>

<p>In my next post I will be writing about the Mediator Pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/35/Design-Patterns-for-Dummies.-The-Iterator-Pattern.aspx]]></link>
         <pubDate>Thu, 05 Feb 2009 03:18:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Command Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Command Pattern. You can read about rest of the patterns from the following links</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx">Chain of Responsibility Pattern</a> </li> </ol>  <p>You can read about the Structural patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br />You can read about the Creational patterns from <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p>The Command pattern deals with segregation of the client that requested an operation and the object that can perform that operation. The command pattern can support sending requests to different receivers, queuing of requests, redo and undo functionality.</p>  <pre class="code"><div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">namespace</span> PatternsConsole</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">CommandPattern</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Command</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">interface</span> <span style="color: #2b91af">ICommand</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Execute();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 10</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// ConcreteCommand</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">AddCommand</span> : <span style="color: #2b91af">ICommand</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Item</span> Itm { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Category</span> Cat { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 16</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Execute()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Itm.Add( Cat );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Cat.Add( Itm );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 23</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// ConcreteCommand</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">DeleteCommand</span> : <span style="color: #2b91af">ICommand</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Item</span> Itm { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Category</span> Cat { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 29</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Execute()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Itm.Delete( Cat );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Cat.Delete( Itm );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 36</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Invoker</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">ItemManager</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ICommand</span> Command { <span style="color: blue">set</span>; <span style="color: blue">get</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 41</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Process()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Command.Execute();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 47</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Receiver</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Item</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 51</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">IDictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">Category</span>&gt; categories;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 53</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Desc { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 55</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Item( <span style="color: blue">string</span> itemName )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Desc = itemName;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; categories = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">Category</span>&gt;();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 61</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Add( <span style="color: #2b91af">Category</span> cat )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; categories.Add( cat.Desc, cat );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 66</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Delete( <span style="color: #2b91af">Category</span> cat )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; categories.Remove( cat.Desc );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 71</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 73</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 74</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Receiver</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 77</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">IDictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">Item</span>&gt; items;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 79</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Desc { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 81</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Category( <span style="color: blue">string</span> catName )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 83</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 84</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Desc = catName;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; items = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">Item</span>&gt;();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 87</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Add( <span style="color: #2b91af">Item</span> item )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 89</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; items.Add( item.Desc, item );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 91</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Item '&quot;</span> + item.Desc + <span style="color: #a31515">&quot;' has been added to the '&quot;</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 92</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + Desc + <span style="color: #a31515">&quot;' Category &quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 93</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 94</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 95</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Delete( <span style="color: #2b91af">Item</span> item )</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 96</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 97</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; items.Remove( item.Desc );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 98</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Item '&quot;</span> + item.Desc</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160;&#160; 99</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color: #a31515">&quot;' has been deleted from the '&quot;</span> + Desc + <span style="color: #a31515">&quot;' Category &quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 100</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 101</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 102</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 103</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main()</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 104</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 105</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Create Receiver objects</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 106</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Item</span> DVD = <span style="color: blue">new</span> <span style="color: #2b91af">Item</span>( <span style="color: #a31515">&quot;The Fountain&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 107</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Category</span> catDVD = <span style="color: blue">new</span> <span style="color: #2b91af">Category</span>( <span style="color: #a31515">&quot;DVD&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 108</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 109</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// create the command object</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 110</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">ICommand</span> command = <span style="color: blue">new</span> <span style="color: #2b91af">AddCommand</span> { Itm = DVD, Cat = catDVD };</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 111</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 112</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// create the invoker</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 113</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">ItemManager</span> manager = <span style="color: blue">new</span> <span style="color: #2b91af">ItemManager</span> { Command = command };</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 114</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; manager.Process();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 115</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 116</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Add an item to the CD category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 117</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DVD = <span style="color: blue">new</span> <span style="color: #2b91af">Item</span>( <span style="color: #a31515">&quot;The Wrestler&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 118</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; catDVD = <span style="color: blue">new</span> <span style="color: #2b91af">Category</span>( <span style="color: #a31515">&quot;DVD&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 119</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; command = <span style="color: blue">new</span> <span style="color: #2b91af">AddCommand</span> { Itm = DVD, Cat = catDVD };</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 120</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; manager.Command = command;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 121</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; manager.Process();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 122</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 123</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Add an item to the Old Releases category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 124</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DVD = <span style="color: blue">new</span> <span style="color: #2b91af">Item</span>( <span style="color: #a31515">&quot;Pi&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 125</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; catDVD = <span style="color: blue">new</span> <span style="color: #2b91af">Category</span>( <span style="color: #a31515">&quot;Old Releases&quot;</span> );</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 126</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; command = <span style="color: blue">new</span> <span style="color: #2b91af">AddCommand</span> { Itm = DVD, Cat = catDVD };</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 127</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; manager.Command = command;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 128</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; manager.Process();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 129</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 130</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Delete an item from the Old Releases category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 131</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; command = <span style="color: blue">new</span> <span style="color: #2b91af">DeleteCommand</span> { Itm = DVD, Cat = catDVD };</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 132</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; manager.Command = command;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 133</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; manager.Process();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 134</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 135</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output:</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 136</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Item 'The Fountain' has been added to the 'DVD' Category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 137</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Item 'The Wrestler' has been added to the 'DVD' Category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 138</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Item 'Pi' has been added to the 'Old Releases' Category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 139</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Item 'Pi' has been deleted from the 'Old Releases' Category</span></p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 140</span>&#160;</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 141</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.ReadLine();</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 142</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 143</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: #c8c8c8; color: #1b5769">&#160; 144</span> }</p></div></pre>

<p>We can use the command pattern when we have different receivers and handle each in a different way or we want to specify and execute commands at different times or when we want to support undo functionality.</p>

<p>In my next post I will be writing about the Iterator pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/34/Design-Patterns-for-Dummies.-The-Command-Pattern.aspx]]></link>
         <pubDate>Mon, 02 Feb 2009 03:16:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Chain of Responsibility Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation with the series on design pattern, in which presently I going through Behavioral Patterns, today I will write about the Chain of Responsibility Pattern. You can read about rest of the patterns from the following links</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx">Template Method Pattern</a> </li> </ol>  <p>You can read about the Structural patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br />You can read about the Creational patterns from <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p>The Chain of Responsibility pattern deals with a list of Handler objects that limits based on the requests that they can deal with. If one object can not handle a request, then the request is passed to the next object in the chain which means by the end of the chain there can be an expected behavior or an exception.&#160; </p>  <p>The following example is based on the example provided in the DoFactory <a href="http://www.dofactory.com/Patterns/PatternChain.aspx">website</a></p>  <pre class="code"><div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">namespace</span> PatternsConsole</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">ChainOfResponsibilityPattern</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Delegate for withdrawal requests</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">delegate</span> <span style="color: blue">void</span> <span style="color: #2b91af">WithdrawHandler</span>&lt;T, E&gt;( T sender, E eventArgs );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 7</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">WithdrawArgs</span> : <span style="color: #2b91af">EventArgs</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">double</span> Amount { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> AccountNumber { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 13</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &quot;Handler&quot;</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">abstract</span> <span style="color: blue">class</span> <span style="color: #2b91af">Approver</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">event</span> <span style="color: #2b91af">WithdrawHandler</span>&lt;<span style="color: #2b91af">Approver</span>, <span style="color: #2b91af">WithdrawArgs</span>&gt; Withdraw;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Approver</span> Successor { <span style="color: blue">protected</span> <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 19</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Invoing the event</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> OnWithdraw( <span style="color: #2b91af">WithdrawArgs</span> e )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( Withdraw != <span style="color: blue">null</span> )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Withdraw( <span style="color: blue">this</span>, e );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 28</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> ProcessRequest( <span style="color: #2b91af">WithDrawal</span> withDrawal )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnWithdraw( <span style="color: blue">new</span> <span style="color: #2b91af">WithdrawArgs</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AccountNumber = withDrawal.AccountNumber,</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Amount = withDrawal.Amount</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 38</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &quot;ConcreteHandler&quot;</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Clerk</span> : <span style="color: #2b91af">Approver</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Clerk()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Hook up delegate to event</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Withdraw += ProcessByClerk;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 47</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> ProcessByClerk( <span style="color: #2b91af">Approver</span> approver, <span style="color: #2b91af">WithdrawArgs</span> e )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( e.Amount &lt; 10000 )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;{0} approved withdrawal for account : {1}&quot;</span>,</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; GetType().Name, e.AccountNumber );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">else</span> <span style="color: blue">if</span> ( Successor != <span style="color: blue">null</span> )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Successor.OnWithdraw( e );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 61</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &quot;ConcreteHandler&quot;</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Officer</span> : <span style="color: #2b91af">Approver</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Officer()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Hook up delegate to event</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Withdraw += ProcessByOfficer;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 70</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> ProcessByOfficer( <span style="color: #2b91af">Approver</span> approver,</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">WithdrawArgs</span> e )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 74</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( e.Amount &lt; 100000 )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;{0} approved withdrawal for account : {1}&quot;</span>,</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; GetType().Name, e.AccountNumber );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">else</span> <span style="color: blue">if</span> ( Successor != <span style="color: blue">null</span> )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Successor.OnWithdraw( e );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 83</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 84</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 85</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// &quot;ConcreteHandler&quot;</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 87</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Manager</span> : <span style="color: #2b91af">Approver</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 89</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Manager()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 91</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Hook up delegate to event</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 92</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Withdraw += ProcessByManager;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 93</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 94</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 95</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> ProcessByManager( <span style="color: #2b91af">Approver</span> approver, <span style="color: #2b91af">WithdrawArgs</span> e )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 96</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 97</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( e.Amount &lt; 1000000 )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 98</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 99</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;{0} approved withdrawal for account : {1}&quot;</span>,</p><p style="margin: 0px"><span style="background: silver">&#160; 100</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; GetType().Name, e.AccountNumber );</p><p style="margin: 0px"><span style="background: silver">&#160; 101</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 102</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">else</span></p><p style="margin: 0px"><span style="background: silver">&#160; 103</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160; 104</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Cannot be approved&quot;</span> );</p><p style="margin: 0px"><span style="background: silver">&#160; 105</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 106</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 107</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 108</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 109</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">WithDrawal</span></p><p style="margin: 0px"><span style="background: silver">&#160; 110</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160; 111</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">double</span> Amount { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160; 112</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> AccountNumber { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160; 113</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 114</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 115</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">static</span> <span style="color: blue">void</span> Main()</p><p style="margin: 0px"><span style="background: silver">&#160; 116</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160; 117</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Setup Chain of Responsibility</span></p><p style="margin: 0px"><span style="background: silver">&#160; 118</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> clerkAprover = <span style="color: blue">new</span> <span style="color: #2b91af">Clerk</span>();</p><p style="margin: 0px"><span style="background: silver">&#160; 119</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> officerApprover = <span style="color: blue">new</span> <span style="color: #2b91af">Officer</span>();</p><p style="margin: 0px"><span style="background: silver">&#160; 120</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> managerApprover = <span style="color: blue">new</span> <span style="color: #2b91af">Manager</span>();</p><p style="margin: 0px"><span style="background: silver">&#160; 121</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 122</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; clerkAprover.Successor = officerApprover;</p><p style="margin: 0px"><span style="background: silver">&#160; 123</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; officerApprover.Successor = managerApprover;</p><p style="margin: 0px"><span style="background: silver">&#160; 124</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 125</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> withDrawal = <span style="color: blue">new</span> <span style="color: #2b91af">WithDrawal</span> { AccountNumber = 1234, Amount = 1001 };</p><p style="margin: 0px"><span style="background: silver">&#160; 126</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; clerkAprover.ProcessRequest( withDrawal );</p><p style="margin: 0px"><span style="background: silver">&#160; 127</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 128</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; withDrawal = <span style="color: blue">new</span> <span style="color: #2b91af">WithDrawal</span> { AccountNumber = 2345, Amount = 10000 };</p><p style="margin: 0px"><span style="background: silver">&#160; 129</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; clerkAprover.ProcessRequest( withDrawal );</p><p style="margin: 0px"><span style="background: silver">&#160; 130</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 131</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; withDrawal = <span style="color: blue">new</span> <span style="color: #2b91af">WithDrawal</span> { AccountNumber = 3456, Amount = 100000 };</p><p style="margin: 0px"><span style="background: silver">&#160; 132</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; clerkAprover.ProcessRequest( withDrawal );</p><p style="margin: 0px"><span style="background: silver">&#160; 133</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 134</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output : Clerk approved withdrawal for account : 1234</span></p><p style="margin: 0px"><span style="background: silver">&#160; 135</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Officer approved withdrawal for account : 2345</span></p><p style="margin: 0px"><span style="background: silver">&#160; 136</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Manager approved withdrawal for account : 3456</span></p><p style="margin: 0px"><span style="background: silver">&#160; 137</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 138</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.Read();</p><p style="margin: 0px"><span style="background: silver">&#160; 139</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 140</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 141</span> }</p></div></pre>

<p>As seen in the example there are 3 approvers for withdrawal as&#160; a chain. The request is passed to the clerk and based on the amount the responsibility is transferred to the successive approver in the chain. We can use the chain of responsibility patter when we have more than one handler for a single request or we have a set of handler that vary dynamically.</p>

<p>In my next post I will be writing about the Command pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/33/Design-Patterns-for-Dummies.-The-Chain-of-Responsibility-Pattern.aspx]]></link>
         <pubDate>Thu, 22 Jan 2009 02:56:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Template Method Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Template Method Pattern. You can read about rest of the patterns from the following links</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx">Strategy Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx">State Pattern</a> </li> </ol>  <p>You can read about the Structural patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.     <br />You can read about the Creational patterns from <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p>The template method pattern deals with enabling algorithms to defer certain steps. It doesn't change the structure of the algorithm but a few of their operations are handled elsewhere.</p>  <pre class="code"><div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">namespace</span> PatternsConsole</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">TemplatePattern</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Interface for operations</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">interface</span> <span style="color: #2b91af">ISortStrategy</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Sort();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 10</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Algorithm with template method</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Algorithm</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> TemplateMethod(<span style="color: #2b91af">ISortStrategy</span> sortStrategy)</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sortStrategy.Sort();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 19</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Concrete Class</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">SortByAge</span> : <span style="color: #2b91af">ISortStrategy</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Sort()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PersonList.Sort( ( p1, p2 ) =&gt; p1.Age.CompareTo( p2.Age ) );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Sort By Age&quot;</span>);</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> ( <span style="color: blue">var</span> person <span style="color: blue">in</span> PersonList )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Name : &quot;</span> + person.Name + <span style="color: #a31515">&quot; Age :&quot;</span> + person.Age);</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 33</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Concrete Class</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">SortByName</span> : <span style="color: #2b91af">ISortStrategy</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Sort()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PersonList.Sort( ( p1, p2 ) =&gt; p1.Name.CompareTo( p2.Name ) );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;\nSort By Name&quot;</span> );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> ( <span style="color: blue">var</span> person <span style="color: blue">in</span> PersonList )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Name : &quot;</span> + person.Name + <span style="color: #a31515">&quot; Age :&quot;</span> + person.Age );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 47</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Person</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> Age { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 53</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Person</span>&gt; PersonList;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 55</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PersonList = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Person</span>&gt;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span> {Name = <span style="color: #a31515">&quot;Person1&quot;</span>, Age = 25},</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span> {Name = <span style="color: #a31515">&quot;Person3&quot;</span>, Age = 26},</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span> {Name = <span style="color: #a31515">&quot;Person4&quot;</span>, Age = 20},</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Person</span> {Name = <span style="color: #a31515">&quot;Person2&quot;</span>, Age = 28}</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 65</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> sortAlgo = <span style="color: blue">new</span> <span style="color: #2b91af">Algorithm</span>();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 67</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sortAlgo.TemplateMethod( <span style="color: blue">new</span> <span style="color: #2b91af">SortByAge</span>() );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output :&#160; Sort By Age</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person4 Age :20</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person1 Age :25</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person3 Age :26</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person2 Age :28</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 74</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sortAlgo.TemplateMethod( <span style="color: blue">new</span> <span style="color: #2b91af">SortByName</span>() );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output :&#160; Sort By Name </span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person1 Age :25</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person2 Age :28</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person3 Age :26</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name : Person4 Age :20</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 81</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.Read();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 83</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 84</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 85</span> }</p></div></pre>

<p>We can use the template method pattern when common behavior can be outside the algorithm and the the behavior itself varies based on the type of the subclass.</p>

<p>In my next post I will be writing about the Chain of Responsibility pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/32/Design-Patterns-for-Dummies.-The-Template-Method-Pattern.aspx]]></link>
         <pubDate>Mon, 19 Jan 2009 03:44:00 GMT</pubDate>
      </item>
   
         </channel>
      </rss>  
   
