


      <rss version="2.0">
         <channel>
            <title><![CDATA[Vinay Rao | RSS Feed]]></title>
            <link>http://www.simplyvinay.com/</link>
            <description>My Personal Website</description>
            <copyright>Copyright 2005 by Vinay Rao</copyright>
   
      <item>
         <title><![CDATA[MPBlog Implementation. Part 5]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p><u><em></em></u></p>  <p><em><font size="3">Setting up StructureMap and The Unit Of Work ActionFilter</font></em></p>  <p><u><em>UnitOfWork ActionFilter</em></u></p>  <p>In my previous <a href="http://www.simplyvinay.com/Post/50/MPBlog-Implementation.-Part-4.aspx" target="_blank">post</a> I had created the repository implementations along with unit of work. But how do we use this unit of work in our application. ASP.NET MVC has a concept of ActionFilters. These could be applied to an action or a controller in general. The two main methods that concern us are <em>OnActionExecuting</em> and the <em>OnActionExecuted</em>. As seen in the code below we call the initialize method of the unit of work which would create a new transaction for us and this is held until the action is complete after which we commit the transaction. We are getting a new instance of _unitOfWork through an IoC container which I will talk about in a bit.</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> MPBlog.Web.UI.ActionFilters</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Web.Mvc;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.BaseClasses;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> StructureMap;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">UseUnitOfWOrkAttribute</span> : <span style="color: #ffc66d">ActionFilterAttribute</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #cc7832">readonly</span> <span style="color: #6897bb">IUnitOfWork</span> _unitOfWork;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> UseUnitOfWOrkAttribute()</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; _unitOfWork = <span style="color: #ffc66d">ObjectFactory</span><span style="font-weight: normal">.GetInstance&lt;</span><span style="color: #6897bb">IUnitOfWork</span><span style="font-weight: normal">&gt;();</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">public</span> UseUnitOfWOrkAttribute( <span style="color: #6897bb">IUnitOfWork</span> unitOfWork )</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; _unitOfWork = unitOfWork;</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">public</span> <span style="color: #cc7832">override</span> <span style="color: #cc7832">void</span> OnActionExecuting( <span style="color: #ffc66d">ActionExecutingContext</span> filterContext )</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; _unitOfWork.Initialize();</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">public</span> <span style="color: #cc7832">override</span> <span style="color: #cc7832">void</span> OnActionExecuted( <span style="color: #ffc66d">ActionExecutedContext</span> filterContext )</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">try</span></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; _unitOfWork.Commit();</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; <span style="color: #cc7832">catch</span>( <span style="color: #ffc66d">Exception</span> )</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; _unitOfWork.Rollback();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">throw</span>;</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; <span style="color: #cc7832">finally</span></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; _unitOfWork.Dispose();</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;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<p>We use this action filter either by decoration individual action with the attribute or if we want all actions to use the unit of work, then we can decorate the class itself with the attribute. In the following code I am using the <em>UseUnitOfWork</em> filter on the Index action.</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> MPBlog.Web.UI.Controllers</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Web.Mvc;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> ActionFilters;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.Services;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #ffc66d">HandleError</span>]</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">HomeController</span> : <span style="color: #ffc66d">Controller</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #cc7832">readonly</span> <span style="color: #6897bb">IBlogService</span> _blogService;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> HomeController(<span style="color: #6897bb">IBlogService</span> blogService)</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; _blogService = blogService;</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: #ffc66d">UseUnitOfWOrk</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #ffc66d">ActionResult</span> Index()</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> posts = _blogService.GetAllPosts();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> View(posts);</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>Attributes may look a bit intrusive (atleast for me). I like how <a href="http://code.google.com/p/fubumvc/" target="_blank">FubuMVC</a> handles this. In <a href="http://code.google.com/p/fubumvc/" target="_blank">FubuMVC</a>, we have something called behaviors and this can be configured in the application start. So if we want <strong>all</strong> our actions to use the unit of work, then we can set the behavior as shown below and that's it, no more attributes on the action or controller. In the following code the <em>access_the_database_through_a_unit_of_work </em>has the same implementation as the filter we created.</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: #ffc66d">ControllerConfig</span><span style="font-weight: normal">.Configure = x =&gt;</span></p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #5eaeae">// Default Behaviors for all actions -- ordered as they're executed</span></p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #5eaeae">/////////////////////////////////////////////////</span></p><p style="margin: 0px">&#160;&#160;&#160; x.ByDefault.EveryControllerAction(d =&gt; d</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Will&lt;<span style="color: #ffc66d">access_the_database_through_a_unit_of_work</span><span style="font-weight: normal">&gt;()</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; );</p><p style="margin: 0px">};</p></div></pre>

<p><u><em>Services</em></u></p>

<p>Instead of accessing the repository directly from the controllers, I have created a separate service which delegates the work to the repository to the operations related. Following is an example of a blog service. I have kept the methods to be minimal for brevity.</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> MPBlog.Core.Services</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Collections.Generic;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Domain;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">IBlogService</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #6897bb">IEnumerable</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt; GetAllPosts();</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #6897bb">IEnumerable</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt; GetPostsByTag(</span><span style="color: #cc7832">string</span> tag);</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Post</span> GetPostById( <span style="color: #cc7832">int</span> postID );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Post</span> GetPostBySlug( <span style="color: #cc7832">string</span> slug );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">void</span> SavePost( <span style="color: #ffc66d">Post</span> post );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">void</span> UpdatePost( <span style="color: #ffc66d">Post</span> post );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">void</span> DeletePost( <span style="color: #ffc66d">Post</span> post );</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<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> MPBlog.Core.Services</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Collections.Generic;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Linq;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> BaseClasses;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Domain;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">BlogService</span> : <span style="color: #6897bb">IBlogService</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #cc7832">readonly</span> <span style="color: #6897bb">IRepository</span> _repository;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> BlogService( <span style="color: #6897bb">IRepository</span> repository )</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; _repository = repository;</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">public</span> <span style="color: #6897bb">IEnumerable</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt; GetAllPosts()</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">return</span> _repository.Query&lt;<span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt;().AsEnumerable();</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">public</span> <span style="color: #6897bb">IEnumerable</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt; GetPostsByTag( </span><span style="color: #cc7832">string</span> tag )</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">return</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _repository.Query&lt;<span style="color: #ffc66d">Tag</span><span style="font-weight: normal">&gt;( t =&gt; t.Description.Equals( tag ) ).</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; SingleOrDefault().Posts;</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">public</span> <span style="color: #ffc66d">Post</span> GetPostById( <span style="color: #cc7832">int</span> postId )</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">return</span> _repository.Load&lt;<span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt;( postId );</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">public</span> <span style="color: #ffc66d">Post</span> GetPostBySlug( <span style="color: #cc7832">string</span> slug )</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">return</span> _repository.Query&lt;<span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt;( p =&gt; p.PostSlug.Equals( slug ) ).SingleOrDefault();</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">public</span> <span style="color: #cc7832">void</span> SavePost( <span style="color: #ffc66d">Post</span> post )</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; _repository.Save( post );</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">public</span> <span style="color: #cc7832">void</span> UpdatePost( <span style="color: #ffc66d">Post</span> post )</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; _repository.Save( post );</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">public</span> <span style="color: #cc7832">void</span> DeletePost( <span style="color: #ffc66d">Post</span> post )</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; _repository.Delete( post );</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><u><em>Dependency Injection using StructureMap</em></u></p>

<p>If we look at the all the code that has been shown till now, we see that, there is almost always an interface declared and we can nowhere see a direct instantiation of the object that is required. So how do we get a new instance of an object. This is where an Inversion Of Control (IoC/DI)&#160; container comes into play. Basically what it does is to give you a concrete objects whenever we require.</p>

<p>I have used constructor injection where ever possible. How it works is, by defining our own ControllerFactory and create the controller ourselves instead of ASP.NET MVC doing it for us. In the below code I have a StrucutreMapControllerFactory from where I am getting an instance of the controller. This factory inherits from the DefaultControllerFactory for ASP.NET MVC and we override the controller building method.</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> MPBlog.Web.UI.Controllers</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Web.Mvc;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> StructureMap;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">StructureMapControllerFactory</span> : <span style="color: #ffc66d">DefaultControllerFactory</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">protected</span> <span style="color: #cc7832">override</span> <span style="color: #6897bb">IController</span> GetControllerInstance(<span style="color: #ffc66d">Type</span> controllerType)</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: #6897bb">IController</span> result = <span style="color: #cc7832">null</span>;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">if</span> (controllerType != <span style="color: #cc7832">null</span>)</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">try</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; result = <span style="color: #ffc66d">ObjectFactory</span><span style="font-weight: normal">.GetInstance(controllerType) </span><span style="color: #cc7832">as</span> <span style="color: #ffc66d">Controller</span>;</p><p style="margin: 0px">&#160;</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; <span style="color: #cc7832">catch</span> (<span style="color: #ffc66d">StructureMapException</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; System.Diagnostics.<span style="color: #ffc66d">Debug</span><span style="font-weight: normal">.WriteLine(</span><span style="color: #ffc66d">ObjectFactory</span><span style="font-weight: normal">.WhatDoIHave());</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; <span style="color: #cc7832">throw</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; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> result;</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>We set this controller factory in our Application_Start method in the global.asax file like so. </p>

<pre class="ncode"><div style="font-weight: bold; font-size: 10pt; background: #1e1e1e; color: silver; font-family: consolas"><p style="margin: 0px">RegisterRoutes( <span style="color: #ffc66d">RouteTable</span><span style="font-weight: normal">.Routes );</span></p><p style="margin: 0px"><span style="color: #ffc66d">Bootstrapper</span><span style="font-weight: normal">.Bootstrap();</span></p><p style="margin: 0px"><span style="color: #ffc66d">ControllerBuilder</span><span style="font-weight: normal">.Current.SetControllerFactory(</span></p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">new</span> <span style="color: #ffc66d">StructureMapControllerFactory</span>());</p></div></pre>
Now that we have controllerfactory ready, we can configure our dependencies. The way to configure our dependency is through a registry which provides a fluent interface. The following code shows all the dependencies being loaded into the container through the MPBlogRegistry.&#160; <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> MPBlog.Web.UI</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> StructureMap;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">Bootstrapper</span> : <span style="color: #6897bb">IBootstrapper</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> BootstrapStructureMap()</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: #ffc66d">ObjectFactory</span><span style="font-weight: normal">.Initialize( x =&gt; x.AddRegistry( </span><span style="color: #cc7832">new</span> <span style="color: #ffc66d">MPBlogRegistry</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">public</span> <span style="color: #cc7832">static</span> <span style="color: #cc7832">void</span> Bootstrap()</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">new</span> <span style="color: #ffc66d">Bootstrapper</span>().BootstrapStructureMap();</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>

<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">using</span> System.Configuration;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px"><span style="color: #cc7832">namespace</span> MPBlog.Web.UI</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.BaseClasses;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.Services;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Persistence;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Persistence.Config;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Persistence.RepositoryImpl;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Persistence.UnitOfWork;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> StructureMap.Attributes;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> StructureMap.Configuration.DSL;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">MPBlogRegistry</span> : <span style="color: #ffc66d">Registry</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> MPBlogRegistry()</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; ForRequestedType&lt;<span style="color: #6897bb">ISessionSource</span><span style="font-weight: normal">&gt;().AsSingletons()</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .TheDefault.Is.ConstructedBy( context =&gt;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; context.GetInstance&lt;<span style="color: #6897bb">ISessionSourceConfiguration</span><span style="font-weight: normal">&gt;()</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; .CreateSessionSource( <span style="color: #cc7832">new</span> <span style="color: #ffc66d">MPBlogPersistenceModel</span>() ) );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ForRequestedType&lt;<span style="color: #6897bb">ISessionSourceConfiguration</span><span style="font-weight: normal">&gt;().AsSingletons()</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .TheDefault.Is.OfConcreteType&lt;<span style="color: #ffc66d">SQLSessionSourceConfiguration</span><span style="font-weight: normal">&gt;()</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithCtorArg( <span style="color: #a5c25c">&quot;connectionString&quot;</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; .EqualTo( <span style="color: #ffc66d">ConfigurationManager</span><span style="font-weight: normal">.ConnectionStrings[</span><span style="color: #a5c25c">&quot;MPBlog&quot;</span>].ConnectionString )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithCtorArg( <span style="color: #a5c25c">&quot;resetDb&quot;</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; .EqualToAppSetting( <span style="color: #a5c25c">&quot;reset&quot;</span> );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ForRequestedType&lt;<span style="color: #6897bb">IUnitOfWork</span><span style="font-weight: normal">&gt;()</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .TheDefault.Is.ConstructedBy( context =&gt; context.GetInstance&lt;<span style="color: #6897bb">INHibernateUnitOfWork</span><span style="font-weight: normal">&gt;() );</span></p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ForRequestedType&lt;<span style="color: #6897bb">INHibernateUnitOfWork</span><span style="font-weight: normal">&gt;().CacheBy( </span><span style="color: #6897bb">InstanceScope</span><span style="font-weight: normal">.Hybrid )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .TheDefault.Is.OfConcreteType&lt;<span style="color: #ffc66d">NHibernateUnitOfWork</span><span style="font-weight: normal">&gt;();</span></p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ForRequestedType&lt;<span style="color: #6897bb">IRepository</span><span style="font-weight: normal">&gt;().AsSingletons()</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .TheDefault.Is.OfConcreteType&lt;<span style="color: #ffc66d">NHRepository</span><span style="font-weight: normal">&gt;();</span></p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ForRequestedType&lt;<span style="color: #6897bb">IBlogService</span><span style="font-weight: normal">&gt;().AsSingletons()</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .TheDefault.Is.OfConcreteType&lt;<span style="color: #ffc66d">BlogService</span><span style="font-weight: normal">&gt;();</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>Once this is done whenever we ask for a instance of an object, the IoC toll will lookup and give us the concerned concrete object.</p>

<p>Thats it for now.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/51/MPBlog-Implementation.-Part-5.aspx]]></link>
         <pubDate>Tue, 23 Jun 2009 13:57:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[MPBlog Implementation. Part 4]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p><em><font size="3">NHibernate </font></em><em><font size="3">Repository and Unit Of Work</font></em></p>  <p>In my previous <a href="/Post/49/MPBlog-Implementation.-Part-3.aspx" target="_blank">post</a> we looked at the domain model and the class maps associated with it. In this post lets look at the Repository and Unit Of Work implementations.</p>  <p><em>Note:</em> The following is based on the FubuMVC contrib <a href="http://code.google.com/p/fubumvc-contrib/">project</a>.</p>  <p><u><em>IRepository</em></u></p>  <p>A repository is an abstraction layer which gives your application an in memory domain object collection. This layer helps in making you application agnostic about where the data is coming from or where it is persisted into. It could well be a flat file or any other RDBMS. Preferably each of your domain object would have its own repository. But for the sake of keeping it simple, I am using a generic repository in the following code.</p>  <pre class="ncode"><div style="background: rgb(30, 30, 30) none repeat scroll 0% 0%; font-weight: bold; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: silver; font-family: consolas;"><p style="margin: 0px;"><span style="color: rgb(204, 120, 50);">namespace</span> MPBlog.Core.BaseClasses</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System.Linq;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System.Linq.Expressions;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">interface</span> <span style="color: rgb(104, 151, 187);">IRepository</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T Load&lt;T&gt;( <span style="color: rgb(204, 120, 50);">int</span> id ) <span style="color: rgb(204, 120, 50);">where</span> T : <span style="color: rgb(255, 198, 109);">Entity</span><span style="font-weight: normal;">&lt;T&gt;;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">void</span> Delete&lt;T&gt;( T target );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">void</span> Save&lt;T&gt;( T target );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(104, 151, 187);">IQueryable</span><span style="font-weight: normal;">&lt;T&gt; Query&lt;T&gt;() </span><span style="color: rgb(204, 120, 50);">where</span> T : <span style="color: rgb(255, 198, 109);">Entity</span><span style="font-weight: normal;">&lt;T&gt;;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(104, 151, 187);">IQueryable</span><span style="font-weight: normal;">&lt;T&gt; Query&lt;T&gt;( </span><span style="color: rgb(255, 198, 109);">Expression</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">Func</span><span style="font-weight: normal;">&lt;T, </span><span style="color: rgb(204, 120, 50);">bool</span><span style="font-weight: normal;">&gt;&gt; whereQuery ) </span><span style="color: rgb(204, 120, 50);">where</span> T : <span style="color: rgb(255, 198, 109);">Entity</span><span style="font-weight: normal;">&lt;T&gt;;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">}</p></div></pre>

<p>The Query methods here will be implemented using <a href="http://sourceforge.net/projects/nhcontrib/" target="_blank">Linq to NHibernate</a>. Now that we have the interface, lets look at the implementation.</p>

<p><u><em>NHRepository</em></u></p>

<pre class="ncode"><div style="background: rgb(30, 30, 30) none repeat scroll 0% 0%; font-weight: bold; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: silver; font-family: consolas;"><p style="margin: 0px;"><span style="color: rgb(204, 120, 50);">namespace</span> MPBlog.Persistence.RepositoryImpl</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System.Linq;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System.Linq.Expressions;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> Core.BaseClasses;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> NHibernate.Linq;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> UnitOfWork;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">class</span> <span style="color: rgb(255, 198, 109);">NHRepository</span> : <span style="color: rgb(104, 151, 187);">IRepository</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(104, 151, 187);">INHibernateUnitOfWork</span> _unitOfWork;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> NHRepository( <span style="color: rgb(104, 151, 187);">INHibernateUnitOfWork</span> unitOfWork )</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; _unitOfWork = unitOfWork;</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: rgb(204, 120, 50);">public</span> T Load&lt;T&gt;( <span style="color: rgb(204, 120, 50);">int</span> id ) <span style="color: rgb(204, 120, 50);">where</span> T : <span style="color: rgb(255, 198, 109);">Entity</span><span style="font-weight: normal;">&lt;T&gt;</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: rgb(204, 120, 50);">return</span> _unitOfWork.Session.Load&lt;T&gt;( id );</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Delete&lt;T&gt;(T target)</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; _unitOfWork.Session.Delete( target );</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Save&lt;T&gt;(T target)</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; _unitOfWork.Session.Save( target );</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(104, 151, 187);">IQueryable</span><span style="font-weight: normal;">&lt;T&gt; Query&lt;T&gt;() </span><span style="color: rgb(204, 120, 50);">where</span> T : <span style="color: rgb(255, 198, 109);">Entity</span><span style="font-weight: normal;">&lt;T&gt;</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: rgb(204, 120, 50);">return</span> _unitOfWork.Session.Linq&lt;T&gt;();</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(104, 151, 187);">IQueryable</span><span style="font-weight: normal;">&lt;T&gt; Query&lt;T&gt;(</span><span style="color: rgb(255, 198, 109);">Expression</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">Func</span><span style="font-weight: normal;">&lt;T, </span><span style="color: rgb(204, 120, 50);">bool</span><span style="font-weight: normal;">&gt;&gt; whereQuery) </span><span style="color: rgb(204, 120, 50);">where</span> T : <span style="color: rgb(255, 198, 109);">Entity</span><span style="font-weight: normal;">&lt;T&gt;</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: rgb(204, 120, 50);">return</span> _unitOfWork.Session.Linq&lt;T&gt;().Where( whereQuery );</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>If you notice the above code, we are accessing the <em>session </em>through the unitofwork. A unit of work helps in keeping track of all the objects that are affect by a business transaction. Following is the implementation of the UnitOfWork.</p>

<p><u><em>IUnitOfWork and INHibernateUnitOfWork</em></u></p>

<pre class="ncode"><div style="background: rgb(30, 30, 30) none repeat scroll 0% 0%; font-weight: bold; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: silver; font-family: consolas;"><p style="margin: 0px;"><span style="color: rgb(204, 120, 50);">namespace</span> MPBlog.Core.BaseClasses</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">interface</span> <span style="color: rgb(104, 151, 187);">IUnitOfWork</span> : <span style="color: rgb(104, 151, 187);">IDisposable</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">void</span> Initialize();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">void</span> Commit();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">void</span> Rollback();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">}</p></div></pre>

<pre class="ncode"><div style="background: rgb(30, 30, 30) none repeat scroll 0% 0%; font-weight: bold; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: silver; font-family: consolas;"><p style="margin: 0px;"><span style="color: rgb(204, 120, 50);">namespace</span> MPBlog.Persistence.UnitOfWork</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> Core.BaseClasses;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> NHibernate;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">interface</span> <span style="color: rgb(104, 151, 187);">INHibernateUnitOfWork</span> : <span style="color: rgb(104, 151, 187);">IUnitOfWork</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(104, 151, 187);">ISession</span> Session { <span style="color: rgb(204, 120, 50);">get</span>; }</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">}</p></div></pre>

<p><u><em>NHibernateUnitOfWork</em></u></p>

<pre class="ncode"><div style="background: rgb(30, 30, 30) none repeat scroll 0% 0%; font-weight: bold; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: silver; font-family: consolas;"><p style="margin: 0px;"><span style="color: rgb(204, 120, 50);">namespace</span> MPBlog.Persistence.UnitOfWork</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> FluentNHibernate;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> NHibernate;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">class</span> <span style="color: rgb(255, 198, 109);">NHibernateUnitOfWork</span> : <span style="color: rgb(104, 151, 187);">INHibernateUnitOfWork</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(104, 151, 187);">ITransaction</span> _transaction;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(204, 120, 50);">readonly</span> <span style="color: rgb(104, 151, 187);">ISessionSource</span> _source;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(204, 120, 50);">bool</span> _isDisposed;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(204, 120, 50);">bool</span> _isInitialized;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(104, 151, 187);">ISession</span> Session { <span style="color: rgb(204, 120, 50);">get</span>; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(204, 120, 50);">set</span>; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> NHibernateUnitOfWork( <span style="color: rgb(104, 151, 187);">ISessionSource</span> source )</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; _source = source;</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Initialize()</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; CheckUoWSanity();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session = _source.CreateSession();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StartNewTransaction();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _isInitialized = <span style="color: rgb(204, 120, 50);">true</span>;</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Commit()</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; CheckUoWSanity();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CheckUoWInitialization();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Commit();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StartNewTransaction();</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Rollback()</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; CheckUoWSanity();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CheckUoWInitialization();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Rollback();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StartNewTransaction();</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: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Dispose()</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: rgb(204, 120, 50);">if</span>( _isDisposed || !_isInitialized )</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">return</span>;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Dispose();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session.Dispose();</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _isDisposed = <span style="color: rgb(204, 120, 50);">true</span>;</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: rgb(204, 120, 50);">private</span> <span style="color: rgb(204, 120, 50);">void</span> CheckUoWSanity()</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: rgb(204, 120, 50);">if</span>( _isDisposed )</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(204, 120, 50);">throw</span> <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">ObjectDisposedException</span>( <span style="color: rgb(165, 194, 92);">"Trying to use disposed object"</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: rgb(204, 120, 50);">private</span> <span style="color: rgb(204, 120, 50);">void</span> CheckUoWInitialization()</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: rgb(204, 120, 50);">if</span>( !_isInitialized )</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(204, 120, 50);">throw</span> <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">InvalidOperationException</span>( <span style="color: rgb(165, 194, 92);">"NHibernateUnitOfWork is not initialized"</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: rgb(204, 120, 50);">private</span> <span style="color: rgb(204, 120, 50);">void</span> StartNewTransaction()</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: rgb(204, 120, 50);">if</span>( _transaction != <span style="color: rgb(204, 120, 50);">null</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Dispose();</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; _transaction = Session.BeginTransaction();</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><u><em>Tests</em></u></p>

<p>Following are the test that were written for both the repository and unitofwork. I am using <a href="http://code.google.com/p/moq/" target="_blank">MoQ</a> as my mocking framework so that I don't have to connect to the actual database when testing.</p>

<pre class="ncode"><div style="background: rgb(30, 30, 30) none repeat scroll 0% 0%; font-weight: bold; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: silver; font-family: consolas;"><p style="margin: 0px;"><span style="color: rgb(204, 120, 50);">namespace</span> MPBlog.Tests.Persistence.Tests</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> System.Linq;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> Core.Domain;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> Moq;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> MPBlog.Persistence.RepositoryImpl;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> MPBlog.Persistence.UnitOfWork;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> NHibernate;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> Xunit;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">class</span> <span style="color: rgb(255, 198, 109);">RepositoryTests</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ISession</span><span style="font-weight: normal;">&gt; _session;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">INHibernateUnitOfWork</span><span style="font-weight: normal;">&gt; _uow;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">private</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(255, 198, 109);">NHRepository</span><span style="font-weight: normal;">&gt; _repo;</span></p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> RepositoryTests()</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; _session = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ISession</span><span style="font-weight: normal;">&gt;();</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _uow = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">INHibernateUnitOfWork</span><span style="font-weight: normal;">&gt;();</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _uow.Setup( u =&gt; u.Session ).Returns( _session.Object );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _repo = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(255, 198, 109);">NHRepository</span><span style="font-weight: normal;">&gt;( _uow.Object );</span></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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_load_should_load_object_from_the_session()</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: rgb(204, 120, 50);">var</span> postId = <span style="color: rgb(104, 151, 187);">1</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _repo.Object.Load&lt;<span style="color: rgb(255, 198, 109);">Post</span><span style="font-weight: normal;">&gt;( postId );</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _session.Verify( s =&gt; s.Load&lt;<span style="color: rgb(255, 198, 109);">Post</span><span style="font-weight: normal;">&gt;( postId ), </span><span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.AtLeastOnce() );</span></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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_save_should_save_object_on_the_session()</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: rgb(204, 120, 50);">var</span> post = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Post</span>();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _repo.Object.Save( post );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _session.Verify( s =&gt; s.SaveOrUpdate( post ), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.AtLeastOnce() );</span></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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_delete_should_delete_object_from_the_session()</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: rgb(204, 120, 50);">var</span> post = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Post</span>();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _repo.Object.Delete( post );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _session.Verify( s =&gt; s.Delete( post ), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.AtLeastOnce() );</span></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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_query_on_session_should_return_an_IQueryable_object()</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: rgb(255, 198, 109);">Assert</span><span style="font-weight: normal;">.IsAssignableFrom( </span><span style="color: rgb(204, 120, 50);">typeof</span>( <span style="color: rgb(104, 151, 187);">IQueryable</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(255, 198, 109);">Post</span><span style="font-weight: normal;">&gt; ), _repo.Object.Query&lt;</span><span style="color: rgb(255, 198, 109);">Post</span><span style="font-weight: normal;">&gt;() );</span></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>

<pre class="ncode"><div style="background: rgb(30, 30, 30) none repeat scroll 0% 0%; font-weight: bold; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: silver; font-family: consolas;"><p style="margin: 0px;"><span style="color: rgb(204, 120, 50);">namespace</span> MPBlog.Tests.Persistence.Tests</p><p style="margin: 0px;">{</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> FluentNHibernate;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> Moq;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> MPBlog.Persistence.UnitOfWork;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> NHibernate;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">using</span> Xunit;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">class</span> <span style="color: rgb(255, 198, 109);">UnitOfWorkTests</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">protected</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ISession</span><span style="font-weight: normal;">&gt; _session;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">protected</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ITransaction</span><span style="font-weight: normal;">&gt; _transaction;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">protected</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ISessionSource</span><span style="font-weight: normal;">&gt; _sessionSource;</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">protected</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(255, 198, 109);">NHibernateUnitOfWork</span><span style="font-weight: normal;">&gt; _uow;</span></p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> UnitOfWorkTests()</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; _session = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ISession</span><span style="font-weight: normal;">&gt;();</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ITransaction</span><span style="font-weight: normal;">&gt;();</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _sessionSource = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(104, 151, 187);">ISessionSource</span><span style="font-weight: normal;">&gt;();</span></p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _sessionSource.Setup( s =&gt; s.CreateSession() ).Returns( _session.Object );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _session.Setup( s =&gt; s.BeginTransaction() ).Returns( _transaction.Object );</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _uow = <span style="color: rgb(204, 120, 50);">new</span> <span style="color: rgb(255, 198, 109);">Mock</span><span style="font-weight: normal;">&lt;</span><span style="color: rgb(255, 198, 109);">NHibernateUnitOfWork</span><span style="font-weight: normal;">&gt;( _sessionSource.Object );</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _uow.Object.Initialize();</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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Can_create_new_transaction()</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; _session.Verify( s =&gt; s.BeginTransaction(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.AtLeastOnce() );</span></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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Can_dispose_the_transaction()</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; _uow.Object.Dispose();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Verify( s =&gt; s.Dispose(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.Exactly( </span><span style="color: rgb(104, 151, 187);">1</span> ) );</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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Can_dispose_the_session()</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; _uow.Object.Dispose();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _session.Verify( s =&gt; s.Dispose(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.Exactly( </span><span style="color: rgb(104, 151, 187);">1</span> ) );</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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_commit_should_commit_the_transaction()</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; _uow.Object.Commit();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Verify( t =&gt; t.Commit(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.AtLeastOnce() );</span></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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_commit_should_dispose_the_transaction_and_start_a_new_transaction()</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; _uow.Object.Commit();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Verify( t =&gt; t.Dispose(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.Exactly( </span><span style="color: rgb(104, 151, 187);">1</span> ) );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _session.Verify( s =&gt; s.BeginTransaction(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.Exactly( </span><span style="color: rgb(104, 151, 187);">2</span> ) );</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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_rollback_should_rollback_the_transaction()</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; _uow.Object.Rollback();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Verify( t =&gt; t.Rollback(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.AtLeastOnce() );</span></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: rgb(255, 198, 109);">Fact</span>]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(204, 120, 50);">public</span> <span style="color: rgb(204, 120, 50);">void</span> Call_to_rollback_should_dispose_the_transaction_and_start_a_new_transaction()</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; _uow.Object.Rollback();</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _transaction.Verify( t =&gt; t.Dispose(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.Exactly( </span><span style="color: rgb(104, 151, 187);">1</span> ) );</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _session.Verify( s =&gt; s.BeginTransaction(), <span style="color: rgb(255, 198, 109);">Times</span><span style="font-weight: normal;">.Exactly( </span><span style="color: rgb(104, 151, 187);">2</span> ) );</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>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/50/MPBlog-Implementation.-Part-4.aspx]]></link>
         <pubDate>Mon, 18 May 2009 00:00:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[MPBlog Implementation. Part 3]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p><em><font size="3">Mapping The Classes And Session Source Configuration</font></em></p>  <p>As mentioned in my previous <a href="http://www.simplyvinay.com/Post/48/A-Brief-Intermission.aspx" target="_blank">post</a>, this series will no longer be a talk about DDD as a blog app is too trivial to show ( learn ) the power of it. I will be using some of the patterns of DDD though. So I have changed the name of the app I am building. It will be called MyPersonalBlog ( MPBlog ) henceforth.</p>  <p>The project structure has changed a bit from what I had shown <a href="http://www.simplyvinay.com/Post/46/DDDBlog-Implementation.-Part-1.aspx" target="_blank">here</a>. There is no longer separate layers for infrastructure and domain. I have clubbed these 2 layers into a single Core layer. The other layers are just the same. </p>  <p>Lets get started by creating our domain objects.</p>  <p><u><em>The domain objects</em></u></p>  <p>So far we have created a entity base class from which all our domain ( models ) will inherit from. In a blog application, we’ll have posts, tags and comments. So lets create classes for each of these. I have created these classes as shown in the class diagram below.</p>  <p><a href="http://www.simplyvinay.com/images/blogimages/MPBlogImplementation.Part3_8497/ClassDiagram.png"><img title="ClassDiagram" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="332" alt="ClassDiagram" src="http://www.simplyvinay.com/images/blogimages/MPBlogImplementation.Part3_8497/ClassDiagram_thumb.png" width="450" border="0" /></a></p>  <p>From the diagram you can see that there is a many-to-many relation between Tag and Post class..</p>  <p><u><em>Domain mappings</em></u></p>  <p>I am using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> to configure the mappings of these classes to the database. Although you can map all the classes using the <a href="http://wiki.fluentnhibernate.org/show/AutoMapping" target="_blank">automapping</a> feature which has a convention based API, I will use the mapping per class feature. Lets look at the mappings.</p>  <p><u><em>PostMap</em></u></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> MPBlog.Persistence.DomainMap</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.Domain;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate.Mapping;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">PostMap</span> : <span style="color: #ffc66d">ClassMap</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt;</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> PostMap()</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; WithTable( <span style="color: #a5c25c">&quot;Posts&quot;</span> );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Id( p =&gt; p.Id, <span style="color: #a5c25c">&quot;PostId&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithUnsavedValue( <span style="color: #6897bb">0</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Access.AsReadOnlyPropertyThroughCamelCaseField()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .GeneratedBy.Identity();</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( p =&gt; p.PostTitle ).Not.Nullable();</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">//will be created as nvarchar(max)</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( p =&gt; p.PostText ).Not.Nullable()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithLengthOf( <span style="color: #6897bb">4001</span> );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( p =&gt; p.ExcerptText ).Not.Nullable()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithLengthOf( <span style="color: #6897bb">1000</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( p =&gt; p.PostSlug ).Unique();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( p =&gt; p.AddedBy ).Not.Nullable();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( p =&gt; p.PublishDate );</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">// Many to many with an intermediate table ( PostsToTags )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; HasManyToMany( p =&gt; p.Tags )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithTableName( <span style="color: #a5c25c">&quot;PostsToTags&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Cascade.SaveUpdate();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; HasMany( p =&gt; p.Comments )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithTableName( <span style="color: #a5c25c">&quot;Comments&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Cascade.All().Inverse();</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>We create a PostMap class which inherits from the ClassMap&lt;T&gt;. The constructor of this PostMap class is where all out mappings go. As can be seen, first we specify the table name for our Posts, Then we specify the Id property which is mapped to the PostId column in our database. The Id in our case is the id that we had declared in the Entity base class ( I have renamed the Key property to Id ). If you recall from my previous <a href="http://www.simplyvinay.com/Post/47/DDDBlog-Implementation.-Part-2.aspx" target="_blank">post</a>, it had only the getter property. The value is assigned by NHibernate. We specify a many to many relation between posts and tags through an intermediate table PostToTags. We have also specified that a post has many comments thought the Comments table The other mappings are self explanatory.</p>

<p>Out Tag and Comment mapping classes look similar to the one above. Following are the two mapping files for these.</p>

<p><u><em>TagMap</em></u></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> MPBlog.Persistence.DomainMap</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.Domain;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate.Mapping;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">TagMap</span> : <span style="color: #ffc66d">ClassMap</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Tag</span><span style="font-weight: normal">&gt;</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> TagMap()</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; WithTable( <span style="color: #a5c25c">&quot;Tags&quot;</span> );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Id( t =&gt; t.Id, <span style="color: #a5c25c">&quot;TagId&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithUnsavedValue( <span style="color: #6897bb">0</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Access.AsReadOnlyPropertyThroughCamelCaseField()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .GeneratedBy.Identity();</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( t =&gt; t.Description ).Not.Nullable();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( t =&gt; t.CreatedDate );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; HasManyToMany( t =&gt; t.Posts )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithTableName( <span style="color: #a5c25c">&quot;PostsToTags&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Cascade.SaveUpdate();</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><u><em>CommentMap</em></u></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> MPBlog.Persistence.DomainMap</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.Domain;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate.Mapping;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">CommentMap</span> : <span style="color: #ffc66d">ClassMap</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Comment</span><span style="font-weight: normal">&gt;</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> CommentMap()</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; WithTable( <span style="color: #a5c25c">&quot;Comments&quot;</span> );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Id( c =&gt; c.Id )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WithUnsavedValue( <span style="color: #6897bb">0</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Access.AsReadOnlyPropertyThroughCamelCaseField()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .GeneratedBy.Identity();</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( c =&gt; c.Body ).Not.Nullable();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( c =&gt; c.AuthorName ).Not.Nullable();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( c =&gt; c.AuthorIP ).Not.Nullable();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( c =&gt; c.AuthorEmail ).Not.Nullable();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( c =&gt; c.AuthorUrl ).Not.Nullable();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Map( c =&gt; c.AddedDate ).Not.Nullable();</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; References( c =&gt; c.Post )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Not.Nullable()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Cascade.All();</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>Now that our mappings are done, lets create a SessionSource to access the NHibernate Session.</p>

<p>We can create a session through the ISessionSource interface in Fluent NHibernate. Lets create an interface for the SessionSourceConfiguration.</p>

<p><em>Note:</em> The following is based on the FubuMVC contrib <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">project</a>.</p>

<p><u><em>ISessionSourceConfiguration and <u><em>SessionSourceConfiguration</em></u></em></u></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> MPBlog.Persistence.Config</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">ISessionSourceConfiguration</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">bool</span> ResetDatabase { <span style="color: #cc7832">get</span>; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #6897bb">ISessionSource</span> CreateSessionSource( <span style="color: #ffc66d">PersistenceModel</span> model );</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<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> MPBlog.Persistence.Config</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Collections.Generic;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate.Cfg.Db;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">SessionSourceConfiguration</span> : <span style="color: #6897bb">ISessionSourceConfiguration</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #cc7832">readonly</span> <span style="color: #cc7832">string</span> _connectionString;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">bool</span> ResetDatabase { <span style="color: #cc7832">get</span>; <span style="color: #cc7832">private</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; <span style="color: #cc7832">public</span> SessionSourceConfiguration( <span style="color: #cc7832">string</span> connectionString, <span style="color: #cc7832">bool</span> resetDb )</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; _connectionString = connectionString;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ResetDatabase = resetDb;</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">public</span> <span style="color: #6897bb">ISessionSource</span> CreateSessionSource( <span style="color: #ffc66d">PersistenceModel</span> model )</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> properties = GetProperties( _connectionString );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> source = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">SessionSource</span>( properties, model );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateDbWithSchema( source );</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">return</span> source;</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">public</span> <span style="color: #6897bb">IDictionary</span><span style="font-weight: normal">&lt;</span><span style="color: #cc7832">string</span>, <span style="color: #cc7832">string</span><span style="font-weight: normal">&gt; GetProperties( </span><span style="color: #cc7832">string</span> connectionString )</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">return</span> <span style="color: #ffc66d">MsSqlConfiguration</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .MsSql2005</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .ConnectionString( c =&gt; c.Is( connectionString ) )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .UseOuterJoin()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .ShowSql()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .ToProperties();</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">void</span> CreateDbWithSchema( <span style="color: #6897bb">ISessionSource</span> source )</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">if</span>( ResetDatabase )</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; source.BuildSchema();</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;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<p>The ResetDatabase property is used to either create a new database from our mappings or reset the database. We have a method called GetProperties from which we generate the connection properties.</p>

<p>As you can see in the CreateSessionSource method, we are passing a PersistenceModel object. This object is used to add the mapping from a specified assembly, in our case, where we have defined the Post, Tag and Comment maps. Lets call it MPBlogPersistenceModel. Here is what it looks like.</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">using</span> FluentNHibernate;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px"><span style="color: #cc7832">namespace</span> MPBlog.Persistence</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">MPBlogPersistenceModel</span> : <span style="color: #ffc66d">PersistenceModel</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> MPBlogPersistenceModel()</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; addMappingsFromThisAssembly();</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 PresistenceModel object, connectionString, and resetDB parameters are injected using an IoC container which we’ll have a look in a later post.</p>

<p>Following are the tests I have written to check my mapping are correct. I am using an in memory SQLite database and the PersistenceSpecification&lt;T&gt; provided by Fluent NHibernate to check my mapping.</p>

<p><u><em>Mapping Tests</em></u></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> MPBlog.Tests.Persistence.Tests</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Core.Domain;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate.Cfg.Db;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> FluentNHibernate.Testing;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> MPBlog.Persistence;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> NHibernate;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Xunit;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">PersistenceTests</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #6897bb">ISession</span> session;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> PersistenceTests()</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> persistenceModel = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">MPBlogPersistenceModel</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">var</span> config = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">SQLiteConfiguration</span>()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .InMemory()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .ConnectionString( c =&gt; c.Is( <span style="color: #a5c25c">&quot;Data Source=:memory:;Version=3;New=True;&quot;</span> ) )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .ShowSql();</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">var</span> sessionSource = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">SessionSource</span>( config.ToProperties(), persistenceModel );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; session = sessionSource.CreateSession();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sessionSource.BuildSchema( session );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Post_mapping_test()</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">new</span> <span style="color: #ffc66d">PersistenceSpecification</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Post</span><span style="font-weight: normal">&gt;( session )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( p =&gt; p.AddedBy, <span style="color: #a5c25c">&quot;vinay&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( p =&gt; p.PostTitle, <span style="color: #a5c25c">&quot;title&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( p =&gt; p.PostText, <span style="color: #a5c25c">&quot;text&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( p =&gt; p.ExcerptText, <span style="color: #a5c25c">&quot;text&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( p =&gt; p.PostSlug, <span style="color: #a5c25c">&quot;slug&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( p =&gt; p.PublishDate, <span style="color: #6897bb">DateTime</span><span style="font-weight: normal">.Today )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .VerifyTheMappings();</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: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Tag_mapping_test()</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">new</span> <span style="color: #ffc66d">PersistenceSpecification</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Tag</span><span style="font-weight: normal">&gt;( session )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( t =&gt; t.Description, <span style="color: #a5c25c">&quot;tag1&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( t =&gt; t.CreatedDate, <span style="color: #6897bb">DateTime</span><span style="font-weight: normal">.Today )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .VerifyTheMappings();</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: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Comment_mapping_test()</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">new</span> <span style="color: #ffc66d">PersistenceSpecification</span><span style="font-weight: normal">&lt;</span><span style="color: #ffc66d">Comment</span><span style="font-weight: normal">&gt;( session )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( c =&gt; c.AuthorEmail, <span style="color: #a5c25c">&quot;vinay@simplyvinay.com&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( c =&gt; c.AuthorIP, <span style="color: #a5c25c">&quot;127.0.0.1&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( c =&gt; c.AuthorName, <span style="color: #a5c25c">&quot;vinay&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( c =&gt; c.AuthorUrl, <span style="color: #a5c25c">&quot;www.simplyvinay.com&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( c =&gt; c.Body, <span style="color: #a5c25c">&quot;comment&quot;</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckProperty( c =&gt; c.AddedDate, <span style="color: #6897bb">DateTime</span><span style="font-weight: normal">.Today )</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CheckReference( c =&gt; c.Post,</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; <span style="color: #cc7832">new</span> <span style="color: #ffc66d">Post</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;&#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;&#160;&#160;&#160; AddedBy = <span style="color: #a5c25c">&quot;vinay&quot;</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostTitle = <span style="color: #a5c25c">&quot;title&quot;</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostText = <span style="color: #a5c25c">&quot;text&quot;</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ExcerptText = <span style="color: #a5c25c">&quot;text&quot;</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSlug = <span style="color: #a5c25c">&quot;slug&quot;</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishDate = <span style="color: #6897bb">DateTime</span><span style="font-weight: normal">.Today</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;&#160;&#160;&#160; } )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .VerifyTheMappings();</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>That's it for now. In my next post, I’ll discuss about the repository and the unit of work implementations. Any sort of feedback will be appreciated.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/49/MPBlog-Implementation.-Part-3.aspx]]></link>
         <pubDate>Mon, 11 May 2009 03:27:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[A Brief Intermission]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>I got a (few) comment by <a href="http://colinjack.blogspot.com/" target="_blank">Colin</a> on my previous <a href="http://www.simplyvinay.com/Post/46/DDDBlog-Implementation.-Part-1.aspx" target="_blank">post</a> on the <a href="http://www.simplyvinay.com/Post/42/My-foray-into-DDD,-TDD,-MVC-and-NHibernate.aspx" target="_blank">series</a> that I had started.</p>  <p>He goes like this for me saying that I am the domain expert for the sample application I was building.</p>  <blockquote>My main feedback would be that a DDD example done without an external domain expert probably isn't a good idea. A few of us have tried this over the years and decided it wasn't a great idea, I'd at the least indicate your using *some* of the patterns from DDD but not others (including the working with domain experts) and that this isn't necessarily the sort of problem that suits a full DDD approach</blockquote>  <p>And for the application itself…</p>  <blockquote>I don't think it suits full DDD no, because you need domain experts and a complex enough problem domain to justify it. Doesn't mean you shouldn't do an example of the patterns and continue on as you are. If you learn from it and teach others then superb, however I'd just make sure to mention that DDD is about more than the patterns (repository/aggregate/value object/specification/service) and related technologies/practices (ORMs/TDD/POCO) that most people use. </blockquote>  <p>Even I had a few doubts about the application when I had planned for it, so from my next post ( for the series ), I wont be talking about DDD anymore. So what changes? Nothing really, from the project structure to the tools, nothing changes. Having a domain layer doesn't mean that the application uses DDD and segregating the project structure as I have gives a clear understanding with separation of concerns. Although I might have have to change a few things that I had thought of using like the Aggregates and a few. I don’t want that sort of complexity as of now.</p>  <p>And about DDD, I really like the way it helps you build a software. I have barely touched the surface of it and will surely blog about it in the future.</p>  <p>P.S. The sample application will no longer be called as DDDBlog. This site is running on a custom blog I wrote which I had named MPS ( MyPersonalSite ), so may be I will call the sample application as MPBlog.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/48/A-Brief-Intermission.aspx]]></link>
         <pubDate>Wed, 15 Apr 2009 12:19:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[DDDBlog Implementation. Part 2]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In my previous post, we looked at setting up the project structure and the tools and libraries that we’ll need to build the application. In this post we’ll look at creating a few base classes.</p>  <p><u><em>Layer SuperType: The Entity Base Class</em></u></p>  <p>Before we start of creating the domain objects, lets first create the base entity object. As discussed before, an <a href="http://www.simplyvinay.com/Post/43/Aspects-Of-DDD.-Part-I.aspx" target="_blank">entity</a> is something that has an identity. This would be our layer supertype because this class will be inherited by all our domain objects that needs to have an identity.</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> DDDBlog.Infrastructure.BaseClasses</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">IEntity</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">int</span> Key { <span style="color: #cc7832">get</span>; }</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">abstract</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">Entity</span> : <span style="color: #6897bb">IEntity</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #cc7832">int</span> key;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">int</span> Key</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">get</span> { <span style="color: #cc7832">return</span> key; }</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">protected</span> Entity()</p><p style="margin: 0px">&#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: #cc7832">protected</span> Entity(<span style="color: #cc7832">int</span> key)</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">this</span><span style="font-weight: normal">.key = key;</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">public</span> <span style="color: #cc7832">bool</span> Equals( <span style="color: #ffc66d">Entity</span> other )</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">return</span> Equals( <span style="color: #cc7832">this</span>, other );</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">public</span> <span style="color: #cc7832">override</span> <span style="color: #cc7832">bool</span> Equals( <span style="color: #cc7832">object</span> obj )</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">return</span> Equals( <span style="color: #cc7832">this</span>, obj <span style="color: #cc7832">as</span> <span style="color: #ffc66d">Entity</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">public</span> <span style="color: #cc7832">static</span> <span style="color: #cc7832">bool</span> Equals( <span style="color: #ffc66d">Entity</span> obj1, <span style="color: #ffc66d">Entity</span> obj2 )</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">if</span> ( <span style="color: #ffc66d">Object</span><span style="font-weight: normal">.Equals( obj1, </span><span style="color: #cc7832">null</span> ) || <span style="color: #ffc66d">Object</span><span style="font-weight: normal">.Equals( obj2, </span><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; obj1.GetType() != obj2.GetType() )</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">false</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">if</span> ( ReferenceEquals( obj1, obj2 ) ) <span style="color: #cc7832">return</span> <span style="color: #cc7832">true</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">return</span> obj1.Key == obj2.Key;</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">public</span> <span style="color: #cc7832">override</span> <span style="color: #cc7832">int</span> GetHashCode()</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">return</span> Key.GetHashCode();;</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 Entity base class is defined in the Infrastructure layer as the definition of entity is not part of the domain itself but it will be used by the domain. As you can see the Entity class has a key property which would be used as an identity. Also I am taking the key to be integer for simplicity. I have also implemented the equality comparison on the entity object. This is used to compare the entities. </p>

<p><u><em>Entity Tests</em></u></p>

<p>Now lets see some tests on the Entity class. A thing to remember is that in TDD, we would have to write the tests firsts and then come at the object.</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> DDDBlog.Tests.Infrastructure.Tests</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> DDDBlog.Infrastructure.BaseClasses;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Xunit;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">FakeEntity</span> : <span style="color: #ffc66d">Entity</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> FakeEntity()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: #cc7832">base</span>( <span style="color: #6897bb">0</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; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> FakeEntity( <span style="color: #cc7832">int</span> key )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: #cc7832">base</span>( key )</p><p style="margin: 0px">&#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;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">EntityTests</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Can_Create_An_Entity()</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> entity = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.NotNull( entity );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><blockquote><p style="margin: 0px">  [<span style="color: #ffc66d">Fact</span>]</p></blockquote><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Newly_Created_Entity_Id_Should_Be_Zero()</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> entity = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity.Key, <font color="#6897bb">0</font> );</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: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Two_Entities_With_Same_Key_Should_Be_Equal()</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> entity1 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity2 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</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">Assert</span><span style="font-weight: normal">.Equal( entity1, entity2 );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity1.GetHashCode(), entity2.GetHashCode() );</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: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Two_Entities_With_Diffrent_Key_Should_Not_Be_Equal()</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> entity1 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity2 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">3</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">Assert</span><span style="font-weight: normal">.NotEqual( entity1, entity2 );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.NotEqual( entity1.GetHashCode(), entity2.GetHashCode() );</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;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Reference_To_Same_Entity_Should_Be_Equal()</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> entity1 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity2 = entity1;</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">Assert</span><span style="font-weight: normal">.Equal( entity1, entity2 );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity1.GetHashCode(), entity2.GetHashCode() );</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><u><em>AggregateRoot And IRepository&lt;T&gt;</em></u></p>

<p>Along with the Entity base class, we also need a repository base, and a way to mark an entity as an aggregate root. These are shown in the following snippets</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> DDDBlog.Infrastructure.BaseClasses</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">IAggregateRoot</span> : <span style="color: #6897bb">IEntity</span> { }</p><p style="margin: 0px">}</p></div></pre>

<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> DDDBlog.Infrastructure.Repository</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Linq;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> BaseClasses;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">IRepository</span><span style="font-weight: normal">&lt;T&gt; </span><span style="color: #cc7832">where</span> T : <span style="color: #6897bb">IAggregateRoot</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; T FindBy( <span style="color: #cc7832">int</span> key );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #6897bb">IQueryable</span><span style="font-weight: normal">&lt;T&gt; FindAll();</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">void</span> Save( T entity );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">void</span> Delete( T entity );</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<p>As you can see our Aggregate root base is just an interface that inherits from IEntity. This is done because a repository is ( should be ) provided only on the Aggregate root. You can read a bit on Aggregates and Repositories <a href="http://www.simplyvinay.com/Post/44/Aspects-Of-DDD.-Part-II.aspx" target="_blank">here</a>.</p>

<p>Now that our base classes are ready, we can look at the domain objects of our sample application, which I will be talking about in the next post.</p>

<p>You must know that, I am by no means an expert in DDD, TDD. The code shown above might be wrong as I am just stepping into this part of the world. Please leave a comment/ suggestion if you find something to be wrong or if it could be done better.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/47/DDDBlog-Implementation.-Part-2.aspx]]></link>
         <pubDate>Wed, 08 Apr 2009 04:05:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[DDDBlog Implementation. Part 1]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In my pervious posts, I wrote about the aspects of TDD and DDD a bit, and following up on the series, I will start building the sample application from this post onwards. For the lack of better name, I will be calling the application DDDBlog. </p>  <p><u><em>Setting up the project</em></u></p>  <p>Lets create the project structure as shown below.</p>  <p><a href="http://www.simplyvinay.com/images/blogimages/DDDBlogImplementation.Part1_F101/solution.png"><img title="solution" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="238" alt="solution" src="http://www.simplyvinay.com/images/blogimages/DDDBlogImplementation.Part1_F101/solution_thumb.png" width="246" border="0" /></a> </p>  <p><em><u>DDDBlog.Domain</u></em> : Domain layer is where the Plain-Old CLR Objects reside. These POCOs are entities that form the domain objects for our project. This layer would also contain the repository interface declarations for the domain objects. From a DDD perspective, the Domain layer should be unaware of persistence ( <em>Persistence Ignorance</em> ), so this layer will not have references to the Persistence layer</p>  <p><u><em>DDDBlog.Infrastructure</em></u> : Infrastructure layer provides the bridging for other layers involved in the application. This layer will be used by all other layers. This layer would contain all the base classes required for all other projects. This layer would also define the top level repository interface. I am not sure where the mapping files should fall under this layer or the persistence layer, but for now I think I will go with this layer.</p>  <p><u><em>DDDBlog.Persistence</em></u> :&#160; This layer would contain the actual data access code. Here I will write the actual implementations for the repositories.</p>  <p><u><em>DDDBlog.Tests</em></u> : This layer consists all the tests for all the layers. Its not necessary for us to have single test layer. We can add individual test layer for each of the layers in our application. I however am going with a single test layer.</p>  <p><u><em>DDDBlog.Web</em></u> : This is the web presentation layer for our application.</p>  <p><u><em>Tools and libraries used</em></u></p>  <p><a href="http://www.asp.net/mvc">ASP.NET MVC</a> – Used for the presentation layer. I had thoughts of using Monorail, but preferred ASP.NET MVC as it has come a long way from the CTP’s and I think its pretty good as of now.</p>  <p><a href="http://www.hibernate.org/343.html" target="_blank">NHibernate</a> and <a href="http://fluentnhibernate.org/" target="_blank">FluentNHibernate</a> – These are used for persistence and for the mappings.</p>  <p><a href="http://structuremap.sourceforge.net/Default.htm" target="_blank">StructureMap</a> – This is used for Dependency Injection.</p>  <p><a href="http://www.codeplex.com/xunit" target="_blank">xUnit.Net</a> – This is used for unity testing.</p>  <p><a href="http://code.google.com/p/moq/" target="_blank">MoQ</a> – This is used for mocking.</p>  <p>In this post we have seen how to layer our application and the tools/libraries that’s needed for our project. In my next post we’ll start coding the base classes for our application.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/46/DDDBlog-Implementation.-Part-1.aspx]]></link>
         <pubDate>Mon, 06 Apr 2009 03:30:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Aspects of TDD]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>This post was supposed to be about the layering in my sample project, but I thought I would talk a bit about TDD as its new to me. This is by no means an exhaustive post on TDD </p>  <p><u><em>Test Driven Development</em></u></p>  <p>TDD could be told as an approach in development, where we are writing tests to know the area for which we are building the software. Tests written during the development phase helps you in understanding the domain, and, after development tests helps us document that system. Following are a few things about TDD that I have understood</p>  <p><u><em>The Steps</em></u></p>  <p>1) We write a failing test. We have to identity whether the test is testing what is intended in this step.    <br />2) We make sure the test written in step 1 passes.     <br />3) We refactor the code written in step 2.</p>  <p>We can also segregate a single test into 3 sections viz. <em>Arrange </em>– Where we setup everything that is there to test, <em>Act </em>– We do everything there is to do with the test, <em>Assert </em>– We check our assertions to be true or not.</p>  <p><u><em>But its hard to test</em></u></p>  <p>There are certain areas that are hard to test, like UI ( Its all the more reason to split the UI aspects from the domain model ). Then there is the database which makes it even harder to test. Why? because working with actual db is cumbersome, we have to maintain the state of database for every run of the test. This could be done for integration test, but while in development stages, stubs or mocks could be used, and lastly there is the code of the tests themselves which we’ll have to maintain, but that should be ok, because the tests helps a lot in doing maintenance work.</p>  <p><u><em>Stubs and Mocks</em></u></p>  <p>There will certain areas in our development which when tested might be too slow or difficult to setup for example the UI or the database operation. To overcome these difficulties we can use stubs or mocks.    <br />    <br />A stub is a light weight implementation of a real object. It is a way to setup the expected values that can be used within the test. In tests stubs we create a class that impersonates the actual class that we have to test. For example if we are to test a class that does database operations, we create a stub class that is free of the database and use this stub class in our tests. The thing to note here is that we are testing the database operation methods and not the actual database call and also we provide the input and the output via the stubs. A mock is also an implementation of a real object, but unlike stubs we can set our expectations in mocks on how it should be used and it also provides self validations on the expected results. Mocks are usually created frameworks.</p>  <p>You can read about the differences between mocks and stubs <a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank">here</a></p>  <p>I will be writing more on TDD as I dwell more into the development of the sample application.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/45/Aspects-of-TDD.aspx]]></link>
         <pubDate>Mon, 30 Mar 2009 13:10:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Aspects Of DDD. Part II]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>This is a continuation post. You can read the first part <a href="http://www.simplyvinay.com/Post/43/Aspects-Of-DDD.-Part-I.aspx">here</a>.</p>  <p><u><em>Aggregates and Aggregate Root</em></u></p>  <p>In a software there can invariably be many objects, and some of these objects are bound to be related to each other in some way. These objects that are related to each other are known as aggregates and an aggregate root is the object that holds them together. Aggregate root is always an entity. Say for example in a blog application a comment would be related to a post. So post and comments become an aggregate and post becomes the aggregate root as there cant be any comments without a post.</p>  <p>Aggregates are to be considered as one unit with regard to any data manipulation and these aggregates can be accessed from outside only through their aggregate root. This mechanism of accessing the objects only by the root helps in maintain data integrity as individual objects can not be changed directly.</p>  <p><u><em>Factories</em></u></p>  <p>When we are to create an aggregate or an entity, it might become too complex for us to create it in the constructor of the root entity. This is where the factory fits in. The role of the factory is to create an object. These factories are helpful when we are creating an aggregate because when the root is created, all the associated aggregates under the root is also created for us.</p>  <p>The process of object creation by a factory needs to be atomic. This is required because we don’t want to have an aggregate root created without creating the the associated aggregates which might leave the system in a unstable state.</p>  <p>You can read about the factory pattern from my earlier posts <a href="http://www.simplyvinay.com/Post/25/Design-Patterns-For-Dummies.-The-Factory-Method-Pattern.aspx" target="_blank">here</a> and <a href="http://www.simplyvinay.com/Post/27/Design-Patterns-for-Dummies.-The-Abstract-Factory-Pattern.aspx" target="_blank">here</a>.</p>  <p><u><em>Repositories</em></u></p>  <p>In any application, domain objects will need other objects for their operation. So these domain objects could be made to hold references to the other needed objects. If we are to give the&#160; domain objects this behavior of maintaining references, then they become tightly coupled as they are holding on to all the references they need at any given time. </p>  <p>This could be avoided by the using repositories. Repositories are used to maintain the references of objects. Using repositories the domain objects are not involved in maintaining the references to the needed object, instead they can get it from the repository.</p>  <p>Repositories are just a storage place where we can store the data and retrieve it later. These repositories are based on strategy, meaning,&#160; based on different strategies it may use different storage locations for different type of objects. </p>  <p><u><em>Next Steps</em></u></p>  <p>What I have covered in this and my previous post is just the starting point of DDD. There are still many factors into DDD, which probably I will be writing about as I build the application. </p>  <p>In my next post I will be writing about the project structure, why the project is split as it is, and what is the role of each layer.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/44/Aspects-Of-DDD.-Part-II.aspx]]></link>
         <pubDate>Wed, 25 Mar 2009 03:05:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Aspects Of DDD. Part I]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p><u><em>What is DDD?</em></u></p>  <p>DDD could be called as a methodology or a set of patterns which help us in developing a software pertaining to the domain. Here what domain means, is the actual system for which we are building the software. DDD is not breaking away from the core Object Oriented design principles, but to utilize them in the right way. There are many aspects in DDD which we need to consider when building an application. Following are the aspects and their brief explanations from what I have understood.</p>  <p><u><em>The Ubiquitous Language ( UL )</em></u></p>  <p>Developers when building a software tend to wholly dwell into the technical aspects of the the application unlike the domain experts who have an extensive knowledge of the problem domain rather than the technical solution for it. When a domain expert is sharing his expertise, he will using a lot of terms that may not be understood by the development team, thus brining in a communication gap between the two.&#160; </p>  <p>Ubiquitous language in DDD addresses this communication gap between the developers and the business experts. The function of the UL is to connect all parts of the design which would have been led by the domain experts and developers need to follow this. The key point in UL is to identify the the concepts which defines the domain and come up with common terms for those. </p>  <p><u><em>Layered Architecture</em></u></p>  <p>When we develop a software in the OO way, a lot of UI and data specific code is mixed with the domain objects. The problem with this is a change in the UI or the data specific code might change the domain behavior and tracking these changes becomes difficult because to arrive at the domain behavior we’ll have to check the UI/data components which changed the behavior in the first place. </p>  <p>The solution for this is to segregate the software into layers such that the layers are loosely coupled and one layer being dependent only on the layers below it. The main criteria in the layering is to free up the domain objects from the UI/data specific logic. </p>  <p>I will talk about the different layers in a future post where I’ll come up with the initial project structure for the blog application.</p>  <p><u><em>Entities and Value Objects</em></u></p>  <p>Entities and Value Objects form the underlying principles in DDD. Entities are objects which have an identity and this identity is maintained throughout the lifetime of the application. The identity can be represented&#160; differently in different applications, for example an store application can have an Customer entity which can be identified from the CustomerID. </p>  <p>Value Objects are objects that have no identity, meaning that we are representing an object by its attributes alone. Usually Value Objects acts as a part of an entity, for example the Customer entity can have an Address Value object which in itself cannot be identified other than belonging to a customer.</p>  <p><u><em>Services</em></u></p>  <p>When developing a software there are certain behavior which might not fit into entities or value objects, meaning if we add these behavior into the objects then they might give a different meaning to the object, which was not intended. But in OO languages we would need an object to represent this functionality, These objects are declared as services. For example in a store application shipping a product to a customer can be declared as a service.</p>  <p>Services are interfaces which are used in performing some operations on the objects ( entities / value objects ). These operations are the ones that doesn't fit in the objects. </p>  <p>This is the the end of Part I. I will continue with the basics of DDD in my next post.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/43/Aspects-Of-DDD.-Part-I.aspx]]></link>
         <pubDate>Mon, 23 Mar 2009 03:42:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[My foray into DDD, TDD, MVC and NHibernate]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>If you are closely following the things that are happening around the .NET development arena, there is a lot of interest in DDD ( Domain Driven Design ) along with other <a href="http://en.wikipedia.org/wiki/Three-letter_abbreviation" target="_blank">TLA</a>’s like TDD ( Test Driven Development), MVC ( Model-View-Controller ) etc. It was hard for me not to notice the buzz around DDD and got me interested in it. As the title says, I would want to see what these things mean and how to develop an application around or rather using these technologies. </p>  <p>So I am going to start a “Learn as you build” series in which I build a blog engine using these technologies ( or methodologies ). This is not a full fledged blog engine mind you. This is a process of learning new things and blog engine is just right to start of with ( along the lines from this <a href="http://ifacethoughts.net/2007/09/19/want-to-learn-web-programming-write-a-blog-engine/" target="_blank">post</a>). If you are looking for a full featured blog engine, you can have a look at <a href="http://www.dotnetblogengine.net/" target="_blank">BlogEngine.NET</a> or <a href="http://www.dasblog.info/" target="_blank">DasBlog</a> or <a href="http://www.subtextproject.com/" target="_blank">SubText</a> and many more I guess. </p>  <p>Along with these things I would also want to explore NHibernate which is an ORM ( another TLA!!! ) and MoQ ( a mocking framework ). Well I think I am getting into a lot of things, probably more than I can handle, but lets see where these things lead me.</p>  <p>I would appreciate any sort of suggestions that might help me in diving into these things.</p>  <p>More blogs to follow on these lines. </p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/42/My-foray-into-DDD,-TDD,-MVC-and-NHibernate.aspx]]></link>
         <pubDate>Tue, 17 Mar 2009 11:17:00 GMT</pubDate>
      </item>
   
      <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>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The State 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 State 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> </ol>  <p>You can read about Structural Patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.</p>  <p>You can read about Creational Patterns from <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>.</p>  <p>State pattern in essence is a dynamic version of the strategy pattern, meaning, when the sate inside an object is changed, it could change its behavior. </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">StatePattern</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: blue">interface</span> <span style="color: #2b91af">IState</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Deposit( <span style="color: blue">double</span> amount );</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> Withdraw( <span style="color: blue">double</span> amount );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 9</span>&#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">double</span> Balance { <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: #2b91af">Account</span> Account { <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">// State 1</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">class</span> <span style="color: #2b91af">InsufficientState</span> : <span style="color: #2b91af">IState</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> InsufficientState( <span style="color: blue">double</span> balance, <span style="color: #2b91af">Account</span> account )</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; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Balance = balance;</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;&#160;&#160;&#160;&#160; Account = account;</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 22</span>&#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> Deposit( <span style="color: blue">double</span> amount )</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; Balance += amount;</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; CheckState();</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> Withdraw( <span style="color: blue">double</span> amount )</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; Balance -= amount;</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; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Insufficient fund&quot;</span> );</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 34</span>&#160;</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; <span style="color: blue">private</span> <span style="color: blue">void</span> CheckState()</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( Balance &gt; 100 )</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;&#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;&#160;&#160;&#160;&#160; Account.State = <span style="color: blue">new</span> <span style="color: #2b91af">NormalState</span>( Balance, Account );</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; }</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">else</span> <span style="color: blue">if</span> ( Balance &gt; 300 )</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; Account.State = <span style="color: blue">new</span> <span style="color: #2b91af">GoldState</span>( Balance, Account );</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;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 47</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> Balance { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</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: #2b91af">Account</span> Account { <span style="color: blue">get</span>; <span style="color: blue">set</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;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// State 2</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">NormalState</span> : <span style="color: #2b91af">IState</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</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; <span style="color: blue">public</span> NormalState( <span style="color: blue">double</span> balance, <span style="color: #2b91af">Account</span> account )</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; {</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; Balance = balance;</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; Account = account;</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 59</span>&#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; <span style="color: blue">public</span> <span style="color: blue">void</span> Deposit( <span style="color: blue">double</span> amount )</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; {</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; Balance += amount;</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; CheckState();</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; }</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">public</span> <span style="color: blue">void</span> Withdraw( <span style="color: blue">double</span> amount )</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; {</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; Balance -= amount;</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;&#160;&#160;&#160;&#160; CheckState();</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 71</span>&#160;</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: blue">private</span> <span style="color: blue">void</span> CheckState()</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> ( Balance &lt; 100 )</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; Account.State = <span style="color: blue">new</span> <span style="color: #2b91af">InsufficientState</span>( Balance, Account );</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; }</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; <span style="color: blue">else</span> <span style="color: blue">if</span> ( Balance &gt; 300 )</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; {</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;&#160;&#160;&#160;&#160; Account.State = <span style="color: blue">new</span> <span style="color: #2b91af">GoldState</span>( Balance, Account );</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; }</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 83</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 84</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> Balance { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Account</span> Account { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 87</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// State 3</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 89</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">GoldState</span> : <span style="color: #2b91af">IState</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 90</span>&#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; <span style="color: blue">public</span> GoldState( <span style="color: blue">double</span> balance, <span style="color: #2b91af">Account</span> account )</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; {</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;&#160;&#160;&#160;&#160; Balance = balance;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 94</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Account = account;</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 96</span>&#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; <span style="color: blue">public</span> <span style="color: blue">void</span> Deposit( <span style="color: blue">double</span> amount )</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; {</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; Balance += amount;</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; CheckState();</p><p style="margin: 0px"><span style="background: silver">&#160; 101</span>&#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;</p><p style="margin: 0px"><span style="background: silver">&#160; 103</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> Withdraw( <span style="color: blue">double</span> amount )</p><p style="margin: 0px"><span style="background: silver">&#160; 104</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</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; Balance -= amount;</p><p style="margin: 0px"><span style="background: silver">&#160; 106</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CheckState();</p><p style="margin: 0px"><span style="background: silver">&#160; 107</span>&#160;&#160;&#160;&#160;&#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;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> CheckState()</p><p style="margin: 0px"><span style="background: silver">&#160; 110</span>&#160;&#160;&#160;&#160;&#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;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> ( Balance &lt; 100 )</p><p style="margin: 0px"><span style="background: silver">&#160; 112</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; 113</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Account.State = <span style="color: blue">new</span> <span style="color: #2b91af">InsufficientState</span>( Balance, Account );</p><p style="margin: 0px"><span style="background: silver">&#160; 114</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; 115</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> ( Balance &lt; 300 )</p><p style="margin: 0px"><span style="background: silver">&#160; 116</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; 117</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Account.State = <span style="color: blue">new</span> <span style="color: #2b91af">NormalState</span>( Balance, Account );</p><p style="margin: 0px"><span style="background: silver">&#160; 118</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; 119</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 120</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 121</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> Balance { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160; 122</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Account</span> Account { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160; 123</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</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; <span style="color: green">// Context</span></p><p style="margin: 0px"><span style="background: silver">&#160; 126</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Account</span></p><p style="margin: 0px"><span style="background: silver">&#160; 127</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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; <span style="color: blue">public</span> <span style="color: #2b91af">IState</span> State { <span style="color: blue">set</span>; <span style="color: blue">get</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160; 129</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 130</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Construcstor</span></p><p style="margin: 0px"><span style="background: silver">&#160; 131</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Account()</p><p style="margin: 0px"><span style="background: silver">&#160; 132</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160; 133</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; State = <span style="color: blue">new</span> <span style="color: #2b91af">NormalState</span>( 0, <span style="color: blue">this</span> );</p><p style="margin: 0px"><span style="background: silver">&#160; 134</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 135</span>&#160;</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: blue">public</span> <span style="color: blue">void</span> Deposit( <span style="color: blue">double</span> amount )</p><p style="margin: 0px"><span style="background: silver">&#160; 137</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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;&#160;&#160;&#160;&#160; State.Deposit( amount );</p><p style="margin: 0px"><span style="background: silver">&#160; 139</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;Deposited {0:C} &quot;</span>, amount );</p><p style="margin: 0px"><span style="background: silver">&#160; 140</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; Balance = {0:C}&quot;</span>, State.Balance );</p><p style="margin: 0px"><span style="background: silver">&#160; 141</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; Status&#160; = {0}&quot;</span>, State.GetType().Name );</p><p style="margin: 0px"><span style="background: silver">&#160; 142</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;&quot;</span> );</p><p style="margin: 0px"><span style="background: silver">&#160; 143</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 144</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 145</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> Withdraw( <span style="color: blue">double</span> amount )</p><p style="margin: 0px"><span style="background: silver">&#160; 146</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160; 147</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; State.Withdraw( amount );</p><p style="margin: 0px"><span style="background: silver">&#160; 148</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;Withdrew {0:C} &quot;</span>, amount );</p><p style="margin: 0px"><span style="background: silver">&#160; 149</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; Balance = {0:C}&quot;</span>, State.Balance );</p><p style="margin: 0px"><span style="background: silver">&#160; 150</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; Status&#160; = {0}\n&quot;</span>, State.GetType().Name );</p><p style="margin: 0px"><span style="background: silver">&#160; 151</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 152</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 153</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 154</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; 155</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160; 156</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> account = <span style="color: blue">new</span> <span style="color: #2b91af">Account</span>();</p><p style="margin: 0px"><span style="background: silver">&#160; 157</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 158</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; account.Deposit( 100 );</p><p style="margin: 0px"><span style="background: silver">&#160; 159</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; account.Deposit( 100 );</p><p style="margin: 0px"><span style="background: silver">&#160; 160</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; account.Deposit( 300 );</p><p style="margin: 0px"><span style="background: silver">&#160; 161</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; account.Withdraw( 200 );</p><p style="margin: 0px"><span style="background: silver">&#160; 162</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; account.Withdraw( 200 );</p><p style="margin: 0px"><span style="background: silver">&#160; 163</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; account.Withdraw( 100 );</p><p style="margin: 0px"><span style="background: silver">&#160; 164</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 165</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: silver">&#160; 166</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Deposited $100.00 </span></p><p style="margin: 0px"><span style="background: silver">&#160; 167</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Balance = $100.00</span></p><p style="margin: 0px"><span style="background: silver">&#160; 168</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Status&#160; = NormalState</span></p><p style="margin: 0px"><span style="background: silver">&#160; 169</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 170</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Deposited $100.00 </span></p><p style="margin: 0px"><span style="background: silver">&#160; 171</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Balance = $200.00</span></p><p style="margin: 0px"><span style="background: silver">&#160; 172</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Status&#160; = NormalState</span></p><p style="margin: 0px"><span style="background: silver">&#160; 173</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 174</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Deposited $300.00 </span></p><p style="margin: 0px"><span style="background: silver">&#160; 175</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Balance = $500.00</span></p><p style="margin: 0px"><span style="background: silver">&#160; 176</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Status&#160; = GoldState</span></p><p style="margin: 0px"><span style="background: silver">&#160; 177</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 178</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Withdrew $200.00 </span></p><p style="margin: 0px"><span style="background: silver">&#160; 179</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Balance = $300.00</span></p><p style="margin: 0px"><span style="background: silver">&#160; 180</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Status&#160; = GoldState</span></p><p style="margin: 0px"><span style="background: silver">&#160; 181</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 182</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Withdrew $200.00 </span></p><p style="margin: 0px"><span style="background: silver">&#160; 183</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Balance = $100.00</span></p><p style="margin: 0px"><span style="background: silver">&#160; 184</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Status&#160; = NormalState</span></p><p style="margin: 0px"><span style="background: silver">&#160; 185</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 186</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Withdrew $100.00 </span></p><p style="margin: 0px"><span style="background: silver">&#160; 187</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Balance = $0.00</span></p><p style="margin: 0px"><span style="background: silver">&#160; 188</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Status&#160; = InsufficientState</span></p><p style="margin: 0px"><span style="background: silver">&#160; 189</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 190</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; 191</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 192</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 193</span> }</p></div></pre>

<p>As you can see in the example, the behavior of our code is changing based on the state which internally is changed based on the amount balance. We can use the state pattern when we have objects that may change their behavior at runtime based on some context or when the objects are becoming complex with many conditional branches.</p>

<p>In my next post I will be writing about the Template Method Pattern</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/31/Design-Patterns-for-Dummies.-The-State-Pattern.aspx]]></link>
         <pubDate>Thu, 15 Jan 2009 03:05:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Strategy Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>Continuing with the series on Design patterns, I will take up Behavioral Patterns from today. I have written about <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">Structural Patterns</a> and <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">Creational Patterns</a> in my previous posts. You can get all the links for them <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a> and <a href="http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx">here</a>. </p>  <p><strong>Behavioral Patterns</strong></p>  <p>Behavioral patterns are concerned with algorithms and communication between them. An algorithm that may have been spilt into complex classes could be hard to maintain. Behavioral patterns helps in identifying the basis for this split and deals with communication between these classes.</p>  <p><strong>Strategy Pattern</strong></p>  <p>The strategy pattern deals with removing algorithms from its host and placing them separately. The client program is given a context to select the desired algorithm in a simple way.</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">StrategyPattern</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">// Strategy Interface</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( <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Person</span>&gt; list );</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: blue">class</span> <span style="color: #2b91af">Person</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;&#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; 14</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; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 16</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Concrete Strategies</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">SelectionSort</span> : <span style="color: #2b91af">ISortStrategy</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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: blue">public</span> <span style="color: blue">void</span> Sort( <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Person</span>&gt; list )</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; {</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;&#160;&#160;&#160;&#160; <span style="color: blue">for</span> (<span style="color: blue">var</span> i = 0; i &lt; list.Count - 1; i++)</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; {</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;&#160;&#160;&#160;&#160; <span style="color: blue">for</span> ( <span style="color: blue">var</span> j = 1 + 1 ; j &lt; list.Count; j++ )</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; {</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span>(list[i].Age &gt; list[j].Age)</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Swap(i, list, j);</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;&#160;&#160;&#160;&#160; }</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;&#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; }</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; }</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;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Swap(<span style="color: blue">int</span> i, <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Person</span>&gt; list, <span style="color: blue">int</span> j)</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; {</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;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> temp = list[i];</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;&#160;&#160;&#160;&#160; list[i] = list[j];</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;&#160;&#160;&#160;&#160; list[j] = temp;</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 41</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">LINQSort</span> : <span style="color: #2b91af">ISortStrategy</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 43</span>&#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; <span style="color: blue">public</span> <span style="color: blue">void</span> Sort( <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Person</span>&gt; list )</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; list.Sort( ( p1, p2 ) =&gt; p1.Age.CompareTo( p2.Age ) );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 49</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Context</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">SortedList</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;&#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;<span style="color: #2b91af">Person</span>&gt; list = <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; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ISortStrategy</span> SortStrategy { <span style="color: blue">private</span> <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</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;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Add( <span style="color: #2b91af">Person</span> person )</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; {</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; list.Add( person );</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;</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; <span style="color: blue">public</span> <span style="color: blue">void</span> Sort()</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; {</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; SortStrategy.Sort( list );</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; <span style="color: #2b91af">Console</span>.WriteLine( SortStrategy.GetType().Name);</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;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> ( <span style="color: blue">var</span> person <span style="color: blue">in</span> list )</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;&#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;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot; &quot;</span> + person.Name );</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; }</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;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine();</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 72</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 73</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; 74</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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; <span style="color: blue">var</span> list = <span style="color: blue">new</span> <span style="color: #2b91af">SortedList</span>();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 76</span>&#160;</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; list.Add( <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; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; list.Add( <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; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; list.Add( <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; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; list.Add( <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; 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; list.SortStrategy = <span style="color: blue">new</span> <span style="color: #2b91af">SelectionSort</span>();</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; list.Sort();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 84</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; list.SortStrategy = <span style="color: blue">new</span> <span style="color: #2b91af">LINQSort</span>();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; list.Sort();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 87</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output : SelectionSort</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 88</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; Person4</span></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: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Person1</span></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; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Person3</span></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; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Person2</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 92</span>&#160;</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; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LINQSort</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 94</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; Person4</span></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: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Person1</span></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; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Person3</span></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; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Person2</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 98</span>&#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; <span style="color: #2b91af">Console</span>.Read();</p><p style="margin: 0px"><span style="background: silver">&#160; 100</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 101</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 102</span> }</p></div></pre>

<p>As you can see in the above example, we have 2 strategies to sort a list. There is a context class which is exposed to the client where it can select a particular strategy. We can use the strategy pattern when we have different classes only differing in behavior or when there are different algorithms and we have to give the selection to the client.</p>

<p>In my next post I will write about the State Pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/30/Design-Patterns-for-Dummies.-The-Strategy-Pattern.aspx]]></link>
         <pubDate>Mon, 12 Jan 2009 02:57:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Creational Design Patterns]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In my last five posts, I have written about the Creational Design Patterns as described by the GOF. Below are the links for all the Creational patterns.</p>  <ol>   <li><a href="/Post/24/Design-Patterns-For-Dummies.-The-Prototype-Pattern.aspx">Prototype Pattern</a></li>    <li><a href="/Post/25/Design-Patterns-For-Dummies.-The-Factory-Method-Pattern.aspx">Factory Method Pattern</a></li>    <li><a href="/Post/26/Design-Patterns-for-Dummies.-The-Singleton-Pattern.aspx">Singleton Pattern</a></li>    <li><a href="/Post/27/Design-Patterns-for-Dummies.-The-Abstract-Factory-Pattern.aspx">Abstract Factory Pattern</a></li>    <li><a href="/Post/28/Design-Patterns-for-Dummies.-The-Builder-Pattern.aspx">Builder Pattern</a></li> </ol>  <p>You can read about Structural Patterns from <a href="/Post/23/Structural-Design-Patterns.aspx">here</a>.</p>  <p>In the following posts to come, I will write about the Behavioral Patterns.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/29/Creational-Design-Patterns.aspx]]></link>
         <pubDate>Mon, 12 Jan 2009 00:00:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Builder Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation of my series on Creational design patterns, I will be discussing about the Builder pattern in this post. You can read about the other patterns from the following links. </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> </ol>  <p>You can read about Structural Patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.</p>  <p>The Builder pattern deals with separation of the specification of a complex object from its actual construction. This construction process can create different representations of the class.</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">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">BuilderPattern</span></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;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: green">// Interface Car</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ICar</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> Details { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 9</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 10</span>&#160;&#160;&#160;&#160; <span style="color: green">// All concrete Cars</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 11</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Car</span> : <span style="color: #2b91af">ICar</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 12</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Details { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 14</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 15</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 16</span>&#160;&#160;&#160;&#160; <span style="color: green">// Directors</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 17</span>&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IModel</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 18</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">ICar</span> CreateCar();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 20</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 21</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; <span style="color: green">//Individual car model</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 23</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Benz</span> : <span style="color: #2b91af">IModel</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 24</span>&#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; <span style="color: blue">public</span> <span style="color: #2b91af">ICar</span> CreateCar()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 26</span>&#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; <span style="color: blue">var</span> car = <span style="color: blue">new</span> <span style="color: #2b91af">Car</span>();</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; DoWork(<span style="color: #a31515">&quot;Build chassis&quot;</span>);</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; DoWork(<span style="color: #a31515">&quot;Fit Engine&quot;</span>);</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; DoWork(<span style="color: #a31515">&quot;Paint&quot;</span>);</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; car.Details = <span style="color: #a31515">&quot;Merc&quot;</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; <span style="color: blue">return</span> car;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 34</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 35</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 36</span>&#160;&#160;&#160;&#160; <span style="color: green">//Individual car model</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 37</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">BMW</span> : <span style="color: #2b91af">IModel</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 38</span>&#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; <span style="color: blue">public</span> <span style="color: #2b91af">ICar</span> CreateCar()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</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; <span style="color: blue">var</span> car = <span style="color: blue">new</span> <span style="color: #2b91af">Car</span>();</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; DoWork( <span style="color: #a31515">&quot;Build chassis&quot;</span> );</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; DoWork( <span style="color: #a31515">&quot;Fit Engine&quot;</span> );</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; DoWork( <span style="color: #a31515">&quot;Paint&quot;</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; car.Details = <span style="color: #a31515">&quot;BMW&quot;</span>;</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; <span style="color: blue">return</span> car;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 48</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 49</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 50</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> DoWork( <span style="color: blue">string</span> workitem )</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 51</span>&#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; <span style="color: #2b91af">Console</span>.Write(<span style="color: #a31515">&quot;&quot;</span> + workitem + <span style="color: #a31515">&quot;: 0%&quot;</span>);</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.Write(<span style="color: #a31515">&quot;....25%&quot;</span>);</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.Write(<span style="color: #a31515">&quot;....50%&quot;</span>);</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;....100%&quot;</span>);</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 56</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 57</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 58</span>&#160;&#160;&#160;&#160; <span style="color: green">//Builder that creates the car</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 59</span>&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IBuilder</span>&lt;T&gt; <span style="color: blue">where</span> T : <span style="color: #2b91af">IModel</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 60</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">ICar</span> CreateCar();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 62</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 63</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 64</span>&#160;&#160;&#160;&#160; <span style="color: green">// Concrete Builder</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 65</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Builder</span>&lt;T&gt; : <span style="color: #2b91af">IBuilder</span>&lt;T&gt; <span style="color: blue">where</span> T : <span style="color: #2b91af">IModel</span>, <span style="color: blue">new</span>()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 66</span>&#160;&#160;&#160;&#160; {</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; T myModel;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Builder()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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; myModel = <span style="color: blue">new</span> T();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 73</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 74</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ICar</span> CreateCar()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 75</span>&#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; <span style="color: blue">return</span> myModel.CreateCar();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 78</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 79</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 80</span>&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Client</span>&lt;T&gt; <span style="color: blue">where</span> T : <span style="color: #2b91af">IModel</span>, <span style="color: blue">new</span>()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 81</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> ClientMain()</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IBuilder</span>&lt;T&gt; builder = <span style="color: blue">new</span> <span style="color: #2b91af">Builder</span>&lt;T&gt;();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Building a car&quot;</span>);</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> car = builder.CreateCar();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 87</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">&quot;Car Built : &quot;</span> + car.Details );</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 89</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 90</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 91</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 92</span>&#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; 93</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 94</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Client</span>&lt;<span style="color: #2b91af">Benz</span>&gt;().ClientMain();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 95</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Client</span>&lt;<span style="color: #2b91af">BMW</span>&gt;().ClientMain();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 96</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 97</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output : Building a car</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 98</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Build chassis: 0%....25%....50%....100%</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 99</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Fit Engine: 0%....25%....50%....100%</span></p><p style="margin: 0px"><span style="background: silver">&#160; 100</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Paint: 0%....25%....50%....100%</span></p><p style="margin: 0px"><span style="background: silver">&#160; 101</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Car Built : Merc</span></p><p style="margin: 0px"><span style="background: silver">&#160; 102</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p><p style="margin: 0px"><span style="background: silver">&#160; 103</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Building a car</span></p><p style="margin: 0px"><span style="background: silver">&#160; 104</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Build chassis: 0%....25%....50%....100%</span></p><p style="margin: 0px"><span style="background: silver">&#160; 105</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Fit Engine: 0%....25%....50%....100%</span></p><p style="margin: 0px"><span style="background: silver">&#160; 106</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Paint: 0%....25%....50%....100%</span></p><p style="margin: 0px"><span style="background: silver">&#160; 107</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Car Built : BMW</span></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: #2b91af">Console</span>.ReadLine();</p><p style="margin: 0px"><span style="background: silver">&#160; 110</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 111</span> }</p></div></pre>

<p>The Builder pattern is based on Directors and Builders. Builder classes can confirm to an IBuilder interface, and they can be called by a director to produce a product according to specification. Builders can be found in applications that create complex structures. We can use the builder pattern when The object to be assembled might have different representations. or when you need fine control over the construction process.</p>

<p>In my next post I will start with the Behavioral patterns.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/28/Design-Patterns-for-Dummies.-The-Builder-Pattern.aspx]]></link>
         <pubDate>Thu, 08 Jan 2009 05:04:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Abstract Factory Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>After a brief hiatus for the holidays, I will continue my series on design patterns. Currently I am going through Creational design patterns, I will be discussing about the Abstract Factory pattern in this post. You can read about the other patterns from the following links. </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> </ol>  <p>You can read about Structural Patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.</p>  <p>Abstract factory pattern deals with creation of objects that are designed to be produced together. Abstract factory can be defined to create different objects of different types and in different combinations. This pattern isolates the object definitions and their class names so that only a factory can return an object when a client requires it.</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">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">AbstractFactoryPattern</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 2</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// An interface for all Cars</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IModel</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">int</span> Cost { <span style="color: blue">get</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160;&#160; 9</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Individual car model</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 11</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">Benz</span> : <span style="color: #2b91af">IModel</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;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> Cost</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; {</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;&#160;&#160;&#160;&#160; <span style="color: blue">get</span> { <span style="color: blue">return</span> 20000; }</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 17</span>&#160;</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: blue">string</span> Name</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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;&#160;&#160;&#160;&#160; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: #a31515">&quot;Merc&quot;</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; }</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;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Individual car model</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 25</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">BMW</span> : <span style="color: #2b91af">IModel</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 26</span>&#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; <span style="color: blue">public</span> <span style="color: blue">int</span> Cost</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; {</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; <span style="color: blue">get</span> { <span style="color: blue">return</span> 21000; }</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;</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; <span style="color: blue">public</span> <span style="color: blue">string</span> Name</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; {</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; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: #a31515">&quot;BMW&quot;</span>; }</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; }</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;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Interface Car</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ICar</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</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; <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; }</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">string</span> Cost { <span style="color: blue">get</span>; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 44</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// All concrete Cars</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Car</span>&lt;T&gt; : <span style="color: #2b91af">ICar</span> <span style="color: blue">where</span> T : <span style="color: #2b91af">IModel</span>, <span style="color: blue">new</span>()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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">private</span> <span style="color: blue">readonly</span> T model;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 49</span>&#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> Car()</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; {</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; model = <span style="color: blue">new</span> T();</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 54</span>&#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; <span style="color: blue">public</span> <span style="color: blue">string</span> Name</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; {</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; <span style="color: blue">get</span> { <span style="color: blue">return</span> model.Name; }</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 59</span>&#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; <span style="color: blue">public</span> <span style="color: blue">string</span> Cost</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; {</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; <span style="color: blue">get</span> { <span style="color: blue">return</span> model.Cost.ToString(); }</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; }</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;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Factory that creates the car</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IFactory</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</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: #2b91af">ICar</span> CreateCar();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 71</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Concrete Factories</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">CarFactory</span>&lt;T&gt; : <span style="color: #2b91af">IFactory</span> <span style="color: blue">where</span> T : <span style="color: #2b91af">IModel</span>, <span style="color: blue">new</span>()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 74</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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; <span style="color: blue">public</span> <span style="color: #2b91af">ICar</span> CreateCar()</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; {</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; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">Car</span>&lt;T&gt;();</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 80</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Client</span>&lt;T&gt; <span style="color: blue">where</span> T : <span style="color: #2b91af">IModel</span>, <span style="color: blue">new</span>()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 82</span>&#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; <span style="color: blue">public</span> <span style="color: blue">void</span> ClientMain()</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 84</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IFactory</span> factory = <span style="color: blue">new</span> <span style="color: #2b91af">CarFactory</span>&lt;T&gt;();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> car = factory.CreateCar();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 87</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 88</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;My car is &quot;</span> + car.Name + <span style="color: #a31515">&quot; and its cost is &quot;</span> + car.Cost );</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; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 91</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 92</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; 93</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 94</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">Client</span>&lt;<span style="color: #2b91af">Benz</span>&gt;().ClientMain();</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">new</span> <span style="color: #2b91af">Client</span>&lt;<span style="color: #2b91af">BMW</span>&gt;().ClientMain();</p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 96</span>&#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; <span style="color: green">//Output: My car is Merc and its cost is 20000</span></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; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160; My car is BMW and its cost is 21000</span></p><p style="margin: 0px"><span style="background: silver">&#160;&#160; 99</span>&#160;</p><p style="margin: 0px"><span style="background: silver">&#160; 100</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: silver">&#160; 101</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: silver">&#160; 102</span>&#160;&#160;&#160;&#160; }</p></div></pre>

<p>As we can see from the above example, the abstract factory handles creation of objects and keeps their details hidden from the client. We can use the Abstract Factory pattern when a system should be independent of how its objects are created and represented or when a system can be configured with one of many families of products or when the importance is on revealing interfaces and not implementations.</p>

<p>In my next post I will writing about the Builder pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/27/Design-Patterns-for-Dummies.-The-Abstract-Factory-Pattern.aspx]]></link>
         <pubDate>Mon, 05 Jan 2009 03:10:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns for Dummies. The Singleton Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p> In continuation of my posts on Creational design patterns, I will be discussing about the Singleton pattern in this post. You can read about the other patterns from the following links. </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> </ol>  <p>You can read about Structural Patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.</p>  <p>Singleton pattern deals with ensuring that there is only one instance of a class and providing a single global access point to that object. It also ensures that the object is not created until its actually needed. </p>  <pre class="code"><div style="font-size: 10pt; background: #f8f8f8; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 1</span>&#160;<span style="color: navy">class</span> <span style="color: #a65300">SingletonPattern</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: navy">class</span> <span style="color: #a65300">SingletonPerson</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">private</span> <span style="color: navy">static</span> <span style="color: #a65300">SingletonPerson</span> <span style="color: maroon">instance</span> = <span style="color: navy">null</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 6</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">Name</span> { <span style="color: navy">get</span>; <span style="color: navy">private</span> <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">int</span> <span style="color: maroon">Age</span> { <span style="color: navy">get</span>; <span style="color: navy">private</span> <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">Address</span> { <span style="color: navy">get</span>; <span style="color: navy">private</span> <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 10</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">private</span> <span style="color: #a65300">Random</span> <span style="color: maroon">random</span> =&#160; <span style="color: navy">new</span> <span style="color: #a65300">Random</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 12</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">SingletonPerson</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Name</span> = <span style="background: #ffffe6">&quot;Person&quot;</span> + <span style="color: maroon">random</span>.<span style="color: maroon">Next</span>( <span style="background: #e6ffff">1</span>, <span style="background: #e6ffff">10</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Age</span> = <span style="background: #e6ffff">27</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Address</span> = <span style="background: #ffffe6">&quot;Address&quot;</span> + <span style="color: maroon">random</span>.<span style="color: maroon">Next</span>(<span style="background: #e6ffff">10</span>, <span style="background: #e6ffff">20</span>);</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 19</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">static</span> <span style="color: #a65300">SingletonPerson</span> <span style="color: maroon">Instance</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">get</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="color: #a65300">Nested</span>.<span style="color: maroon">Instance</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 27</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">private</span> <span style="color: navy">class</span> <span style="color: #a65300">Nested</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">internal</span> <span style="color: navy">static</span> <span style="color: navy">readonly</span> <span style="color: #a65300">SingletonPerson</span> <span style="color: maroon">Instance</span> =</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">new</span> <span style="color: #a65300">SingletonPerson</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 32</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">static</span> <span style="color: maroon">Nested</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#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; </p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 37</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 38</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 39</span>&#160;&#160;&#160;&#160; <span style="color: navy">static</span> <span style="color: navy">void</span> <span style="color: maroon">Main</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 40</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">SingletonPerson</span> <span style="color: maroon">person1</span> = <span style="color: #a65300">SingletonPerson</span>.<span style="color: maroon">Instance</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="background: #ffffe6">&quot;Name&#160; : &quot;</span> + <span style="color: maroon">person1</span>.<span style="color: maroon">Name</span> + <span style="background: #ffffe6">&quot; Age : &quot;</span> + <span style="color: maroon">person1</span>.<span style="color: maroon">Age</span> </p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="background: #ffffe6">&quot; Address : &quot;</span> + <span style="color: maroon">person1</span>.<span style="color: maroon">Address</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output : Name&#160; : Person7 Age : 27 Address : Address19</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 45</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">SingletonPerson</span> <span style="color: maroon">person2</span> = <span style="color: #a65300">SingletonPerson</span>.<span style="color: maroon">Instance</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="background: #ffffe6">&quot;Name&#160; : &quot;</span> + <span style="color: maroon">person2</span>.<span style="color: maroon">Name</span> + <span style="background: #ffffe6">&quot; Age : &quot;</span> + <span style="color: maroon">person2</span>.<span style="color: maroon">Age</span> </p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="background: #ffffe6">&quot; Address : &quot;</span> + <span style="color: maroon">person2</span>.<span style="color: maroon">Address</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output : Name&#160; : Person7 Age : 27 Address : Address19</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 50</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Confirm person1 and person2 are same</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">if</span> ( <span style="color: maroon">person1</span> == <span style="color: maroon">person2</span> )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="background: #ffffe6">&quot;Same instance\n&quot;</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 56</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">ReadLine</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 58</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 59</span> }</p></div></pre>

<p>As you can see from the above example the 2 instances of the SingletonPerson object are the same and so are their properties viz. Name, Age and Address.&#160; We can use the singleton pattern when we need to create only one instance of a class or when we need to provide controlled access to an instance.</p>

<p>In my next post I will be writing about the Abstract Factory Pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/26/Design-Patterns-for-Dummies.-The-Singleton-Pattern.aspx]]></link>
         <pubDate>Mon, 22 Dec 2008 04:37:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns For Dummies. The Factory Method Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In continuation of my posts on Creational design patterns, I will be discussing about the Factory Method pattern in this post. You can read about the other patterns from the following links. </p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/24/Design-Patterns-For-Dummies.-The-Prototype-Pattern.aspx">Prototype Pattern</a> </li> </ol>  <p>You can read about Structural Patterns from <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.</p>  <p>The Factory Method pattern deals with creation of objects in which the subclasses decide which class's object has to be created. Various subclasses may implement the same interface, but the Factory Method creates the appropriate object based on some supplied information.</p>  <pre class="code"><div style="font-size: 10pt; background: #f8f8f8; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 1</span>&#160;<span style="color: navy">public</span> <span style="color: navy">class</span> <span style="color: #a65300">FactoryPattern</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">interface</span> <span style="color: #2b91af">IWorkLocation</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">string</span> <span style="color: maroon">WorkingFrom</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 7</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">class</span> <span style="color: #a65300">Developer</span> : <span style="color: #a65300">Person</span>, <span style="color: #2b91af">IWorkLocation</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">Developer</span>( <span style="color: navy">string</span> <span style="color: maroon">name</span>, <span style="color: navy">string</span> <span style="color: maroon">designation</span> )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">this</span>.<span style="color: maroon">Name</span> = <span style="color: maroon">name</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">this</span>.<span style="color: maroon">Designation</span> = <span style="color: maroon">designation</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 15</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">WorkingFrom</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="background: #ffffe6">&quot;Name : &quot;</span> + <span style="color: navy">this</span>.<span style="color: maroon">Name</span> + <span style="background: #ffffe6">&quot;, Work Location :&#160; Home&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 20</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 21</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">class</span> <span style="color: #a65300">Tester</span> : <span style="color: #a65300">Person</span>, <span style="color: #2b91af">IWorkLocation</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 23</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">Tester</span>( <span style="color: navy">string</span> <span style="color: maroon">name</span>, <span style="color: navy">string</span> <span style="color: maroon">designation</span> )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">this</span>.<span style="color: maroon">Name</span> = <span style="color: maroon">name</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">this</span>.<span style="color: maroon">Designation</span> = <span style="color: maroon">designation</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 29</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">WorkingFrom</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="background: #ffffe6">&quot;Name : &quot;</span> + <span style="color: navy">this</span>.<span style="color: maroon">Name</span> + <span style="background: #ffffe6">&quot;, Work Location :&#160; Office&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 34</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 35</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 36</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">class</span> <span style="color: #a65300">Person</span> : <span style="color: #2b91af">IWorkLocation</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 37</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">Designation</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">Name</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 40</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">Person</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Name</span> = <span style="background: #ffffe6">&quot;Base Person&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Designation</span> = <span style="background: #ffffe6">&quot;&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 46</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">Person</span>( <span style="color: navy">string</span> <span style="color: maroon">name</span>, <span style="color: navy">string</span> <span style="color: maroon">designation</span> )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Designation</span> = <span style="color: maroon">designation</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Name</span> = <span style="color: maroon">name</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 52</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">WorkingFrom</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="background: #ffffe6">&quot;Name : &quot;</span> + <span style="color: navy">this</span>.<span style="color: maroon">Name</span> + <span style="background: #ffffe6">&quot;, Work Location :&#160; Not Available&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 57</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 58</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 59</span>&#160;&#160;&#160;&#160; <span style="color: green">//Creator</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 60</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">class</span> <span style="color: #a65300">Creator</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 61</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: #2b91af">IWorkLocation</span> <span style="color: maroon">FactoryMethod</span>( <span style="color: #a65300">Person</span> <span style="color: maroon">per</span> )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">if</span> ( <span style="color: maroon">per</span>.<span style="color: maroon">Designation</span>.<span style="color: maroon">Equals</span>( <span style="background: #ffffe6">&quot;Developer&quot;</span> ) )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="color: navy">new</span> <span style="color: #a65300">Developer</span>( <span style="color: maroon">per</span>.<span style="color: maroon">Name</span>, <span style="color: maroon">per</span>.<span style="color: maroon">Designation</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">else</span> <span style="color: navy">if</span> ( <span style="color: maroon">per</span>.<span style="color: maroon">Designation</span>.<span style="color: maroon">Equals</span>( <span style="background: #ffffe6">&quot;Tester&quot;</span> ) )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="color: navy">new</span> <span style="color: #a65300">Tester</span>( <span style="color: maroon">per</span>.<span style="color: maroon">Name</span>, <span style="color: maroon">per</span>.<span style="color: maroon">Designation</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">else</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="color: navy">new</span> <span style="color: #a65300">Person</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 71</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 72</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 73</span>&#160;&#160;&#160;&#160; <span style="color: navy">static</span> <span style="color: navy">void</span> <span style="color: maroon">Main</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 74</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Creator</span> <span style="color: maroon">crtr</span> = <span style="color: navy">new</span> <span style="color: #a65300">Creator</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">var</span> <span style="color: maroon">person</span> = <span style="color: maroon">crtr</span>.<span style="color: maroon">FactoryMethod</span>( <span style="color: navy">new</span> <span style="color: #a65300">Person</span>( <span style="background: #ffffe6">&quot;Person1&quot;</span>, <span style="background: #ffffe6">&quot;Developer&quot;</span> ) );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="color: maroon">person</span>.<span style="color: maroon">WorkingFrom</span>() );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output: Name : Person1, Work Location :&#160; Home</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 79</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person</span> = <span style="color: maroon">crtr</span>.<span style="color: maroon">FactoryMethod</span>( <span style="color: navy">new</span> <span style="color: #a65300">Person</span>( <span style="background: #ffffe6">&quot;Person2&quot;</span>, <span style="background: #ffffe6">&quot;Tester&quot;</span> ) );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="color: maroon">person</span>.<span style="color: maroon">WorkingFrom</span>() );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output: Name : Person2, Work Location :&#160; Office</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 83</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 84</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person</span> = <span style="color: maroon">crtr</span>.<span style="color: maroon">FactoryMethod</span>( <span style="color: navy">new</span> <span style="color: #a65300">Person</span>() );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="color: maroon">person</span>.<span style="color: maroon">WorkingFrom</span>() );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Output:&#160; Name : Base Person, Work Location :&#160; Not Available</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 87</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">ReadLine</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 89</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 90</span> }</p></div></pre>

<p>As seen in the above example, the instantiation of objects is handled by the FactoryMethod of the Creator class. In this method, the information being passed helps the method in deciding which object to create. We must use the Factory method pattern when we want the flexibility of creating objects based on some criteria or the objects can be extended in subclasses.</p>

<p>In my next post I will talking about the Singleton pattern.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/25/Design-Patterns-For-Dummies.-The-Factory-Method-Pattern.aspx]]></link>
         <pubDate>Thu, 18 Dec 2008 06:18:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Design Patterns For Dummies. The Prototype Pattern]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>Continuing with the series on Design patterns, I will take up Creational Patterns from today. I have written about <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">Structural Patterns</a> in my previous posts. You can get all the links for them <a href="http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx">here</a>.</p>  <p><strong>Creational Patterns</strong></p>  <p>Creational patterns deals with separation of a system in terms of how their objects are created, what they are made of and how they must be represented. There are 5 types of creational patterns as observed by the GOF. I will write about the Prototype pattern today.</p>  <p><strong>The Prototype</strong></p>  <p>The prototype patterns deals with creation of new objects by cloning one of the available prototypes. The creation of objects is much faster for large classes. It also keeps a record of parts of a data structure so that it can be copied without knowing the subclass from which they were created.</p>  <pre class="code"><div style="font-size: 10pt; background: #f8f8f8; color: black; font-family: consolas"><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 1</span>&#160;<span style="color: navy">class</span> <span style="color: #a65300">PrototypePattern</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #a65300">Serializable</span>]</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">abstract</span> <span style="color: navy">class</span> <span style="color: #a65300">ProtoType</span>&lt;<span style="color: maroon">T</span>&gt; <span style="color: navy">where</span> <span style="color: maroon">T</span> : <span style="color: navy">class</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">T</span> <span style="color: maroon">ShallowCopy</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="color: navy">this</span>.<span style="color: maroon">MemberwiseClone</span>() <span style="color: navy">as</span> <span style="color: maroon">T</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 10</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">T</span> <span style="color: maroon">DeepCopy</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">MemoryStream</span> <span style="color: maroon">stream</span> = <span style="color: navy">new</span> <span style="color: #a65300">MemoryStream</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">BinaryFormatter</span> <span style="color: maroon">formatter</span> = <span style="color: navy">new</span> <span style="color: #a65300">BinaryFormatter</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">formatter</span>.<span style="color: maroon">Serialize</span>( <span style="color: maroon">stream</span>, <span style="color: navy">this</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">stream</span>.<span style="color: maroon">Seek</span>( <span style="background: #e6ffff">0</span>, <span style="color: #2b91af">SeekOrigin</span>.<span style="color: maroon">Begin</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">T</span> <span style="color: maroon">copy</span> = <span style="color: maroon">formatter</span>.<span style="color: maroon">Deserialize</span>( <span style="color: maroon">stream</span> ) <span style="color: navy">as</span> <span style="color: maroon">T</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">stream</span>.<span style="color: maroon">Close</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="color: maroon">copy</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 21</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 22</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 23</span>&#160;&#160;&#160;&#160; [<span style="color: #a65300">Serializable</span>]</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 24</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">class</span> <span style="color: #a65300">Address</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 25</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">Street</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">City</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">Country</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 29</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">Address</span>( <span style="color: navy">string</span> <span style="color: maroon">street</span>, <span style="color: navy">string</span> <span style="color: maroon">city</span>, <span style="color: navy">string</span> <span style="color: maroon">country</span> )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Street</span> = <span style="color: maroon">street</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">City</span> = <span style="color: maroon">city</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Country</span> = <span style="color: maroon">country</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 36</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 37</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 38</span>&#160;&#160;&#160;&#160; [<span style="color: #a65300">Serializable</span>]</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 39</span>&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">class</span> <span style="color: #a65300">Person</span> : <span style="color: #a65300">ProtoType</span>&lt;<span style="color: #a65300">Person</span>&gt;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 40</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">string</span> <span style="color: maroon">Name</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">int</span> <span style="color: maroon">Age</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: #a65300">Address</span> <span style="color: maroon">Address</span> { <span style="color: navy">get</span>; <span style="color: navy">set</span>; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 44</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: maroon">Person</span>( <span style="color: navy">string</span> <span style="color: maroon">name</span>, <span style="color: navy">int</span> <span style="color: maroon">age</span>, <span style="color: navy">string</span> <span style="color: maroon">street</span>, <span style="color: navy">string</span> <span style="color: maroon">city</span>, <span style="color: navy">string</span> <span style="color: maroon">country</span> )</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Name</span> = <span style="color: maroon">name</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Age</span> = <span style="color: maroon">age</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Address</span> = <span style="color: navy">new</span> <span style="color: #a65300">Address</span>( <span style="color: maroon">street</span>, <span style="color: maroon">city</span>, <span style="color: maroon">country</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 51</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: navy">override</span> <span style="color: navy">string</span> <span style="color: maroon">ToString</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">return</span> <span style="color: navy">string</span>.<span style="color: maroon">Format</span>( <span style="background: #ffffe6">&quot;Name : {0}\nAge : {1}\nAddress : {2}, {3}, {4}&quot;</span>,</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">Name</span>, <span style="color: maroon">Age</span>, <span style="color: maroon">Address</span>.<span style="color: maroon">Street</span>, <span style="color: maroon">Address</span>.<span style="color: maroon">City</span>, <span style="color: maroon">Address</span>.<span style="color: maroon">Country</span> );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 57</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 58</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 59</span>&#160;&#160;&#160;&#160; <span style="color: navy">class</span> <span style="color: #a65300">PersonManager</span> : <span style="color: #a65300">ProtoType</span>&lt;<span style="color: #a65300">Person</span>&gt;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 60</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">public</span> <span style="color: #a65300">Dictionary</span>&lt;<span style="color: navy">string</span>, <span style="color: #a65300">Person</span>&gt; <span style="color: maroon">persons</span> =</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: navy">new</span> <span style="color: #a65300">Dictionary</span>&lt;<span style="color: navy">string</span>, <span style="color: #a65300">Person</span>&gt;()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 63</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: gray; color: white">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="background: #ffffe6">&quot;person1&quot;</span>, <span style="color: navy">new</span> <span style="color: #a65300">Person</span>(<span style="background: #ffffe6">&quot;Person1&quot;</span>, <span style="background: #e6ffff">25</span>, <span style="background: #ffffe6">&quot;Street1&quot;</span>, <span style="background: #ffffe6">&quot;City1&quot;</span>, <span style="background: #ffffe6">&quot;Country1&quot;</span>) },</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style="background: #ffffe6">&quot;person2&quot;</span>, <span style="color: navy">new</span> <span style="color: #a65300">Person</span>(<span style="background: #ffffe6">&quot;Person2&quot;</span>, <span style="background: #e6ffff">26</span>, <span style="background: #ffffe6">&quot;Street2&quot;</span>, <span style="background: #ffffe6">&quot;City2&quot;</span>, <span style="background: #ffffe6">&quot;Country2&quot;</span>) }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 66</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: gray; color: white">&#160;&#160; 67</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 68</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 69</span>&#160;&#160;&#160;&#160; <span style="color: navy">static</span> <span style="color: navy">void</span> <span style="color: maroon">Main</span>()</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 70</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">PersonManager</span> <span style="color: maroon">persManager</span> = <span style="color: navy">new</span> <span style="color: #a65300">PersonManager</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 72</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Person</span> <span style="color: maroon">person1</span>, <span style="color: maroon">person2</span>, <span style="color: maroon">person3</span>, <span style="color: maroon">person4</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 74</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person1</span> = <span style="color: maroon">persManager</span>.<span style="color: maroon">persons</span>[<span style="background: #ffffe6">&quot;person1&quot;</span>];</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="color: maroon">person1</span>.<span style="color: maroon">ToString</span>() );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output:&#160; Name : Person1</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age : 25</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Address : Street1, City1, Country1</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 80</span>&#160;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person3</span> = <span style="color: maroon">persManager</span>.<span style="color: maroon">persons</span>[<span style="background: #ffffe6">&quot;person1&quot;</span>].<span style="color: maroon">ShallowCopy</span>();</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person3</span>.<span style="color: maroon">Name</span> = <span style="background: #ffffe6">&quot;Person3&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 83</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person3</span>.<span style="color: maroon">Age</span> = <span style="background: #e6ffff">20</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 84</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person3</span>.<span style="color: maroon">Address</span>.<span style="color: maroon">Street</span> = <span style="background: #ffffe6">&quot;Street3&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person3</span>.<span style="color: maroon">Address</span>.<span style="color: maroon">City</span> = <span style="background: #ffffe6">&quot;City3&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">person3</span>.<span style="color: maroon">Address</span>.<span style="color: maroon">Country</span> = <span style="background: #ffffe6">&quot;Country3&quot;</span>;</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 87</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="color: maroon">person3</span>.<span style="color: maroon">ToString</span>() );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a65300">Console</span>.<span style="color: maroon">WriteLine</span>( <span style="color: maroon">person1</span>.<span style="color: maroon">ToString</span>() );</p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 89</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output:&#160; Name : Person3</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age : 20</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 91</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Address : Street3, City3, Country3</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 92</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Output:&#160; Name : Person1</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 93</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Age : 25</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 94</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Address : Street3, City3, Country3</span></p><p style="margin: 0px"><span style="background: gray; color: white">&#160;&#160; 95</span>&#160;</p><p style="margin: 0px"><
