<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8323612215254179121</id><updated>2012-01-05T11:58:42.659Z</updated><category term='commandname'/><category term='repeater'/><category term='datepicker'/><category term='Jquery'/><category term='bubble event'/><category term='AutoComplete'/><category term='Design Patterns'/><category term='Singleton design pattern'/><title type='text'>Patterns</title><subtitle type='html'>This blog is all about Asp.net design patterns using c#. Domian driven login, asp.net controls, tip and tricks and mvc</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-1031758167851010808</id><published>2012-01-05T10:24:00.006Z</published><updated>2012-01-05T10:35:38.759Z</updated><title type='text'>De- Caching items on all the servers in the Windows Azure</title><content type='html'>&lt;div style="text-align: center;"&gt;
&lt;/div&gt;&lt;div style="text-align: center;"&gt;
&lt;/div&gt;
You can use the following code even if you would like to pass any message to all the instances in the cloud.  &lt;div&gt;The following post shows you how to get the ip address of  each instance in the windows azure.&lt;/div&gt;&lt;div&gt;
&lt;/div&gt;&lt;div style="text-align: center;"&gt;
&lt;/div&gt;&lt;div&gt;&lt;p class="MsoNormal"&gt;In order to de-cache keys or to communicate or to pass the messages to all load balanced servers, please create a new internal endpoint in azure web role. Add a internal endpoint with name "&lt;span style="color: rgb(163, 21, 21); background-color: white; font-family: Consolas; font-size: 13px; "&gt;DeCacheEndPointInternal&lt;/span&gt;".&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;img src="http://2.bp.blogspot.com/-X4A5UcY-C8c/TwV8GYNthfI/AAAAAAAABfE/NFS_HEgrVBI/s400/Capture.JPG" border="0" alt="" id="BLOGGER_PHOTO_ID_5694093752900879858" style="text-align: center; cursor: pointer; width: 500px; height: 130px; " /&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;
&lt;/p&gt;&lt;p class="MsoNormal"&gt;In order to send a message through query string. use the following code&lt;/p&gt;&lt;p class="MsoNormal"&gt;
&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;/p&gt;&lt;pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; "&gt;  &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;RoleInstance&lt;/span&gt; roleInstance &lt;span style="color:blue;"&gt;in&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;RoleEnvironment&lt;/span&gt;.CurrentRoleInstance.Role.Instances)                     {                         &lt;span style="color:blue;"&gt;var&lt;/span&gt; ipEndpoint =                              roleInstance.InstanceEndpoints[&lt;span style="color:#a31515;"&gt;"DeCacheEndPointInternal"&lt;/span&gt;].IPEndpoint;                         &lt;span style="color:blue;"&gt;var&lt;/span&gt; url = &lt;span style="color:blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;"http://&lt;/span&gt;&lt;span style="color:mediumseagreen;"&gt;{0}&lt;/span&gt;&lt;span style="color:#a31515;"&gt;,&lt;/span&gt;&lt;span style="color:mediumseagreen;"&gt;{1}&lt;/span&gt;&lt;span style="color:#a31515;"&gt;,&lt;/span&gt;&lt;span style="color:mediumseagreen;"&gt;{2}&lt;/span&gt;&lt;span style="color:#a31515;"&gt;"&lt;/span&gt; ,ipEndpoint.ToString() ,                             &lt;span style="color:#a31515;"&gt;"/Cache.aspx?ClearCache="&lt;/span&gt;, CacheKey) ;                         &lt;span style="color:blue;"&gt;var&lt;/span&gt; req = (&lt;span style="color:#2b91af;"&gt;HttpWebRequest&lt;/span&gt;)&lt;span style="color:#2b91af;"&gt;WebRequest&lt;/span&gt;.Create(url);                         &lt;span style="color:blue;"&gt;var&lt;/span&gt; res = (&lt;span style="color:#2b91af;"&gt;HttpWebResponse&lt;/span&gt;)req.GetResponse();                     }&lt;/pre&gt;&lt;pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;/pre&gt;&lt;pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; "&gt;This will send a HTTPRequest to the "&lt;span style="color: rgb(163, 21, 21); background-color: white; "&gt;Cache.aspx" &lt;/span&gt;&lt;span style="background-color: white; "&gt;with the querystring containing cache-key to remove on each servers.&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;span style="background-color: white; "&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;span style="background-color: white; "&gt;Thanks&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-1031758167851010808?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/1031758167851010808/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2012/01/de-caching-items-on-all-servers-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/1031758167851010808'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/1031758167851010808'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2012/01/de-caching-items-on-all-servers-in.html' title='De- Caching items on all the servers in the Windows Azure'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-X4A5UcY-C8c/TwV8GYNthfI/AAAAAAAABfE/NFS_HEgrVBI/s72-c/Capture.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-5533541413462976887</id><published>2009-08-24T21:09:00.001+01:00</published><updated>2009-08-24T21:38:37.711+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AutoComplete'/><category scheme='http://www.blogger.com/atom/ns#' term='Jquery'/><title type='text'>JQuery AutoComplete using XML with Key Value pair</title><content type='html'>&lt;p align="justify"&gt;Recently i have been to different websites and blogs for autocomplete plug-in but i could not find much help regarding to auto complete with key value pair specially using xml.&amp;#160; because personally i think using xml file for autocomplete is better option than using JavaScript strings because&amp;#160; of ease of use of xml file. It can be generated and changed easily.&lt;/p&gt;  &lt;h3&gt;AutoComplete&lt;/h3&gt;  &lt;p&gt;Autocomplete an input field to enable users quickly finding and selecting some value, leveraging searching and filtering. For more help you can read all documentation and examples about this plug-in at &lt;a href="http://docs.jquery.com/Plugins/Autocomplete" target="_blank"&gt;http://docs.jquery.com/Plugins/Autocomplete#API_Documentation&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;To download JavaScript files please visits &lt;a href="http://www.pengoworks.com/workshop/jquery/autocomplete.htm" target="_blank"&gt;http://www.pengoworks.com/workshop/jquery/autocomplete.htm&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;To download JQuery    &lt;br /&gt;&lt;a href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js"&gt;http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Output&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/SpLzkAC93HI/AAAAAAAABFI/XKUDYb_rTi0/s1600-h/image%5B2%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_dIl2j8iaPMg/SpLzkSxoRAI/AAAAAAAABFM/qQILVyGuBEM/image_thumb.png?imgmax=800" width="188" height="52" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://lh6.ggpht.com/_dIl2j8iaPMg/SpLzk5UApEI/AAAAAAAABFQ/g-gr2ezMQG4/s1600-h/image%5B5%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_dIl2j8iaPMg/SpLzlTXGf1I/AAAAAAAABFU/m6v_CAgy6Hg/image_thumb%5B1%5D.png?imgmax=800" width="172" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h3&gt;How to use XML with AutoComplete&lt;/h3&gt;  &lt;p&gt;You may find&amp;#160; couple of example on internet but none of them has actually worked for me. &lt;/p&gt;  &lt;p&gt;First of all you can create any xml. I have an xml file&amp;#160; having links inside in it.&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 400px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;xml version=&amp;quot;1.0&amp;quot;&lt;span style="color: #0000ff"&gt;?&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;items&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;item&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;![CDATA[yahoo]]&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;![CDATA[www.yhaoo.com]]&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;item&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;item&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;![CDATA[google]]&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;![CDATA[www.google.com]]&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;item&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;item&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;![CDATA[msn]]&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;![CDATA[www.msn.com]]&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;item&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;items&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;h4&gt;JavaScript to use JQuery and plug-in&lt;/h4&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 440px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;script&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;src&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;JavaScripts/jquery-1.3.2.min.js&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;script&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;script&lt;/span&gt; &lt;span style="color: #ff0000"&gt;src&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;JavaScripts/jquery.autocomplete.pack.js&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;script&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;h4&gt;To use JQuery with XML file&lt;/h4&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 440px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; &amp;lt;script type=&amp;quot;&lt;span style="color: #8b0000"&gt;text/javascript&lt;/span&gt;&amp;quot;&amp;gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;        &lt;span style="color: #008000"&gt;//this function will clear only the default text of input field&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;        &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; clearText(thefield) {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (thefield.defaultValue == thefield.value)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                thefield.value = &amp;quot;&lt;span style="color: #8b0000"&gt;&lt;/span&gt;&amp;quot;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;        $(&lt;span style="color: #0000ff"&gt;document&lt;/span&gt;).ready(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt;() {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #008000"&gt;// get the input field&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; input = $('input#txtResourceFinder');
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #008000"&gt;// This function opens the full list before even typing&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #008000"&gt;// if you dont want this  omit this part.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            input.one('click', &lt;span style="color: #0000ff"&gt;function&lt;/span&gt;() {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                $(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;).&lt;span style="color: #0000ff"&gt;focus&lt;/span&gt;().click().select();
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                $(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;).one('click', &lt;span style="color: #0000ff"&gt;arguments&lt;/span&gt;.&lt;span style="color: #0000ff"&gt;callee&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            });
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #008000"&gt;// Jquery autocomplete with xml file&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            input.autocomplete('links.xml', {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                delay: 10,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                minChars: 2,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                matchSubset: 1,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                matchContains: 1,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                minChars: 0,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                parse: parseXML,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                formatItem: formatItem,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                formatResult: formatResult
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            }).result(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt;(event, item) {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                &lt;span style="color: #0000ff"&gt;window&lt;/span&gt;.&lt;span style="color: #0000ff"&gt;location&lt;/span&gt;.href = item.value
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            });
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #008000"&gt;// this function will parse xml and return as a array of string&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; parseXML(xml) {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; results = [];
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                $(xml).&lt;span style="color: #0000ff"&gt;find&lt;/span&gt;('item').each(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt;() {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                    &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; text = $.trim($(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;).&lt;span style="color: #0000ff"&gt;find&lt;/span&gt;('text').text());
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                    &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; value = $.trim($(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;).&lt;span style="color: #0000ff"&gt;find&lt;/span&gt;('value').text());
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                    results[results.&lt;span style="color: #0000ff"&gt;length&lt;/span&gt;] = { 'data': { text: text, value: value },
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                        'result': text, 'value': value
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                    };
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                });
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; results;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            };
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; formatItem(data) {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; data.text;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            };
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; formatResult(data) {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; data.text;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;            };
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;        });
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;    &amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;parseXML:&lt;/strong&gt; This function will parse xml and return as a array of text value pairs.&lt;/p&gt;

&lt;p&gt;As you can see above that result returns item as a text value pair. Once you select the option it will redirect to that location.&lt;/p&gt;

&lt;h4&gt;&lt;a href="http://sites.google.com/site/aspdotnetpatterns/Home/AutoComplete.zip?attredirects=0"&gt;Download Code&lt;/a&gt;&lt;/h4&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-5533541413462976887?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/5533541413462976887/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/08/jquery-autocomplete-using-xml-with-key.html#comment-form' title='22 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/5533541413462976887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/5533541413462976887'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/08/jquery-autocomplete-using-xml-with-key.html' title='JQuery AutoComplete using XML with Key Value pair'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_dIl2j8iaPMg/SpLzkSxoRAI/AAAAAAAABFM/qQILVyGuBEM/s72-c/image_thumb.png?imgmax=800' height='72' width='72'/><thr:total>22</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-6265825841139986099</id><published>2009-08-14T08:34:00.001+01:00</published><updated>2009-08-14T13:01:49.056+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Decorator Design Pattern using C#</title><content type='html'>&lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p align="justify"&gt;The role of the Decorator pattern is to be able to add functionality dynamically to an object, making it appear as if the object used inheritance.Decorator pattern&amp;#160; provides a way of attaching new state and behaviour to an object dynamically. The object does not know it is being decorated,which makes this a useful pattern for evolving systems. Decorator pattern is the instantiation and organization of the individual    &lt;br /&gt;elements&lt;/p&gt;  &lt;h3&gt;Explanation&lt;/h3&gt;  &lt;p&gt;To discuss it more take a little look at UML diagram below.&lt;/p&gt;  &lt;h5&gt;UML Diagram&lt;/h5&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_dIl2j8iaPMg/SoUTdaW0FqI/AAAAAAAABEY/-rieToOR4mc/s1600-h/decorator%5B8%5D.jpg"&gt;&lt;img title="decorator" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="331" alt="decorator" src="http://lh6.ggpht.com/_dIl2j8iaPMg/SoUTeTF32TI/AAAAAAAABEc/UyhMlGoSNIo/decorator_thumb%5B4%5D.jpg?imgmax=800" width="469" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In our example &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Component:&lt;/strong&gt; To keep this example simple i am using System.Draw.Image class as a component.     &lt;br /&gt;&lt;strong&gt;Decorator:&amp;#160; &lt;/strong&gt;In our example we have IDecorator interface     &lt;br /&gt;&lt;strong&gt;ConcreteDecorator:&lt;/strong&gt; Then we have two classes DrawText and DrawRectangle as concrete decorator classes.&lt;/p&gt;  &lt;h4&gt;Attributes of Decorator Design Pattern&lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt;The component implementations is dynamic and can be altered at runtime. &lt;/li&gt;    &lt;li&gt;The original object is unaware of any decorations. &lt;/li&gt;    &lt;li&gt;The decorations are independent of each other. &lt;/li&gt;    &lt;li&gt;The decorations can be composed together in a mix-and-match fashion. &lt;/li&gt; &lt;/ul&gt;  &lt;pre style="border-right: #ffffff 1px solid; padding-right: 5px; border-top: #ffffff 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #ffffff 1px solid; width: 500px; padding-top: 5px; border-bottom: #ffffff 1px solid; background-color: #e9e9e9"&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Drawing.Imaging;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Drawing;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.IO;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; DecoratorPattern
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;{
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    // &lt;span style="color: #0000ff"&gt;Interface&lt;/span&gt; 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IDecorator
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        void Operation();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    //ConcreteDecorator   Classes
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; DrawText : IDecorator
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        Image image;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; DrawText(Image image)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            this.image = image;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        /// &amp;lt;summary&amp;gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        /// Drawing text &lt;span style="color: #0000ff"&gt;on&lt;/span&gt; image
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        /// &amp;lt;/summary&amp;gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; void  Operation(  )
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;          
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            Graphics g = Graphics.FromImage(image);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            StringFormat strFormat = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StringFormat();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            strFormat.Alignment = StringAlignment.Center;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            strFormat.LineAlignment = StringAlignment.Center;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            g.DrawString(&amp;quot;&lt;span style="color: #8b0000"&gt;pebbles&lt;/span&gt;&amp;quot;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Font(&amp;quot;&lt;span style="color: #8b0000"&gt;Tahoma&lt;/span&gt;&amp;quot;, 20), Brushes.Black,50,50);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            image.Save(&amp;quot;&lt;span style="color: #8b0000"&gt;new.jpg&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    //ConcreteDecorator   Classes
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; DrawRectangle : IDecorator
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;       Image image;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;       &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; DrawRectangle(Image image)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            this.image = image;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        /// &amp;lt;summary&amp;gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        /// drawing rectangle &lt;span style="color: #0000ff"&gt;on&lt;/span&gt; Image
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        /// &amp;lt;/summary&amp;gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;       &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; void Operation()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            Graphics g = Graphics.FromImage(image);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            Pen blackPen = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Pen(Color.Black, 3);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            Rectangle rect = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Rectangle(0, 0, 200, 200);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            g.DrawRectangle(blackPen, rect);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            image.Save(&amp;quot;&lt;span style="color: #8b0000"&gt;new.jpg&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Client
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; void Main()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            //Component can have responsibilities added &lt;span style="color: #0000ff"&gt;to&lt;/span&gt; them dynamically. 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            Image image = Image.FromFile(&amp;quot;&lt;span style="color: #8b0000"&gt;pebbles.jpg&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            IDecorator decorator = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DrawRectangle(image);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            decorator.Operation();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            decorator = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DrawText(image);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            decorator.Operation();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    } 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;}
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/SoUTe9UvosI/AAAAAAAABEg/yaaPZZztcIY/s1600-h/pebbles%5B2%5D.jpg"&gt;&lt;img title="pebbles" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="160" alt="pebbles" src="http://lh3.ggpht.com/_dIl2j8iaPMg/SoUTfLt9SRI/AAAAAAAABEk/zSewqItizD0/pebbles_thumb.jpg?imgmax=800" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh3.ggpht.com/_dIl2j8iaPMg/SoUTffZkXlI/AAAAAAAABEo/0BuzA6njh50/s1600-h/new%5B2%5D.jpg"&gt;&lt;img title="new" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="160" alt="new" src="http://lh5.ggpht.com/_dIl2j8iaPMg/SoUTfw1wgkI/AAAAAAAABEs/QWuOJmzeWRw/new_thumb.jpg?imgmax=800" width="244" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;In general decorator pattern is used to extend the functionality of individual objects, not classes.&amp;#160; So because of it all modification are done at runtime rather than on design time. A you can see&amp;#160; above pebbles example that we have changed the image by drawing rectangle and text. A Decorator, also known as a Wrapper. Because of its dynamic behavior they are also&amp;#160; useful for adapting objects to new situations without re-writing the original object's code. Though its called decorator pattern in my example but i have spoiled the image than decorating it :). &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-6265825841139986099?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/6265825841139986099/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/08/decorator-design-pattern-using-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/6265825841139986099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/6265825841139986099'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/08/decorator-design-pattern-using-c.html' title='Decorator Design Pattern using C#'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_dIl2j8iaPMg/SoUTeTF32TI/AAAAAAAABEc/UyhMlGoSNIo/s72-c/decorator_thumb%5B4%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-7070637900080156412</id><published>2009-07-31T14:09:00.001+01:00</published><updated>2009-07-31T15:37:09.294+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Strategy Design Pattern</title><content type='html'>&lt;p&gt;&lt;a style="display: none" href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=473787" rel="tag"&gt;codeproject &lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p align="justify"&gt;By definition Strategy design pattern&amp;#160; allow an object to change&amp;#160; its behaviour when its internal state changes. The object will appear to change its class. The Strategy design pattern enables a client to choose which algorithm to use from a family of algorithms and gives it a simple way to access it. The algorithms can also be expressed independently of the data they are using.&lt;/p&gt;  &lt;h4&gt;A little about past&lt;/h4&gt;  &lt;p align="justify"&gt;In my childhood i used to play a very interesting number game with my Phopho (Aunt :)). She used to play that game with me and i could never figure it out how was that possible. In this game you ask for any number from a person for example if the person thinks a number like 498 then you tell him ok after some calculation answer will be 2496. How cool was that to guess the number even before any calculation. Now then you ask number again from the person and that could be 764 then you give your number which is 235 then 3rd time you ask from person for number which could be 671 and you give him number which is 328 now if you add these number up 498 +764 +235 +671 + 328 = 2496 and that's the number my Aunt always guessed it before. &lt;/p&gt;  &lt;p align="justify"&gt;In this number game only rule is you can think of any number greater than 2 but if you give any number then user have to stick with that length of number so if its 7864 then all numbers will be&amp;#160; equal to length 4. &lt;/p&gt;  &lt;h4&gt;Explanation &lt;/h4&gt;  &lt;p align="justify"&gt;Now lets study more about strategy design pattern to implement this example.The Strategy pattern involves removing an algorithm from its host class and putting it in a separate class. There may be different algorithms (strategies) that are applicable for a given problem. If the algorithms are all kept in the host, messy code with lots of conditional statements will result. The Strategy pattern enables a client to choose which algorithm to use from a family of algorithms and gives it a simple way to access it. The algorithms can also be expressed independently of the data they are using. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_dIl2j8iaPMg/SnLtDA_G5tI/AAAAAAAABEI/Jotz25lrv4U/s1600-h/image%5B1%5D.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="194" alt="image" src="http://lh5.ggpht.com/_dIl2j8iaPMg/SnLtDnLkixI/AAAAAAAABEQ/SdoPYqaC_Qs/image_thumb.png?imgmax=800" width="396" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;in our example we have following classes as form UML Diagram &lt;/p&gt;  &lt;p&gt;Strategy = IStrategy&lt;/p&gt;  &lt;p&gt;Concrete = FinalAnswerGet,YourNumberGet &lt;/p&gt;  &lt;p&gt;Context = Context&lt;/p&gt;  &lt;p&gt;Client will be our Main class&lt;/p&gt;  &lt;p align="justify"&gt;The client does need to know about the general workings of the various IStrategy implementations and the workings of the algorithms&amp;#160; inside the Context. The Context class doesn’t know about the different IStrategy implementations, and only knows the IStrategy interface. So in other words Context class won’t directly reference the IStrategy implementations and IStrategy implementations won’t directly reference the context class instance.&lt;/p&gt;  &lt;pre style="border-right: #ffffff 1px solid; padding-right: 5px; border-top: #ffffff 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #ffffff 1px solid; width: 500px; padding-top: 5px; border-bottom: #ffffff 1px solid; background-color: #e9e9e9"&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;span style="color: #008000"&gt;// The Context class&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Context
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #008000"&gt;//this could be any userNumber from User . But it Must be greater than 9&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; userNumber = 5;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #008000"&gt;// that would be the asnswer you can show it to user or friend&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; FinalAnswer = 0;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #008000"&gt;// that would be your calculated  userNumber against user userNumber  &lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; yourNumber = 0;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #008000"&gt;// Strategy aggregation&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            IStrategy strategy = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FinalAnswerGet();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #008000"&gt;// Algorithm invokes a strategy method&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Algorithm()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; strategy.Play(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #808080"&gt;//// Changing strategies&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SwitchStrategy()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (strategy &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; FinalAnswerGet)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                    strategy = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; YourNumberGet();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                    strategy = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FinalAnswerGet();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #008000"&gt;// Strategy interface&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IStrategy
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Play(Context c);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #008000"&gt;// Strategy 1&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FinalAnswerGet : IStrategy
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Play(Context c)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; c.FinalAnswer = Convert.ToInt32((&amp;quot;&lt;span style="color: #8b0000"&gt;2&lt;/span&gt;&amp;quot; + Convert.ToString(c.userNumber - 2)));
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #008000"&gt;// Strategy 2&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; YourNumberGet : IStrategy
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Play(Context c)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                &lt;span style="color: #008000"&gt;//gets the length of userNumber&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; length = Convert.ToString(c.userNumber).Length;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; numbeToMinus = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Empty;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; length; i++)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                    numbeToMinus = numbeToMinus + &amp;quot;&lt;span style="color: #8b0000"&gt;9&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; c.yourNumber = Math.Abs(c.userNumber - Convert.ToInt32(numbeToMinus));
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;       
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                Context context = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Context();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                context.userNumber = 899;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                context.Algorithm();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;User  enter number = {0} \t  my already guessed  final answer =  {1}&lt;/span&gt;&amp;quot;, context.userNumber, context.FinalAnswer);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                context.userNumber = 245;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;User  enter number  2nd time = {0} &lt;/span&gt;&amp;quot;, context.userNumber);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                context.SwitchStrategy();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                context.Algorithm();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;Your   number   = {0} &lt;/span&gt;&amp;quot;, context.yourNumber);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                context.userNumber = 382;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;User  enter number  3rd time = {0} &lt;/span&gt;&amp;quot;, context.userNumber);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                context.Algorithm();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;Your   number   = {0} &lt;/span&gt;&amp;quot;, context.yourNumber);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;{0} 899 + 245 + 754 + 382+ 617= 2897 AND OFCOURSE I  GUESSED THIS NUMBER BEFORE&lt;/span&gt;&amp;quot;, Environment.NewLine);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;And output is&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_dIl2j8iaPMg/SnLtEPPjlQI/AAAAAAAABEA/9y_QiVQNhc8/s1600-h/image10.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="252" alt="image" src="http://lh6.ggpht.com/_dIl2j8iaPMg/SnLtFT9iRPI/AAAAAAAABEE/IzGOeMkQOeA/image_thumb6.png?imgmax=800" width="488" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Difference between &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/strategy-pattern-design-pattern.html" target="_blank"&gt;Strategy Design Pattern&lt;/a&gt; and &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/state-design-pattern.html" target="_blank"&gt;State Design Pattern&lt;/a&gt;.&lt;/h4&gt;

&lt;p&gt;Strategy pattern is similar to the &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/state-design-pattern.html" target="_blank"&gt;State Design Pattern&lt;/a&gt; from an architectural point of view but the intent is entirely different.Unlike the State pattern, the Algorithm class doesn’t manage state, but represents an algorithm that uses one or more IStrategy implementations.The IStrategy implementation isn’t managed by the Algorithm class, but assigned by the client where as a state usually selects the next state of its context. A state tends to have lots of unrelated methods, so there is little cohesion between the methods of a state. For more detail please read my posting on &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/state-design-pattern.html" target="_blank"&gt;State Design Pattern&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Your feedback is welcome&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-7070637900080156412?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/7070637900080156412/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/strategy-pattern-design-pattern.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/7070637900080156412'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/7070637900080156412'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/strategy-pattern-design-pattern.html' title='Strategy Design Pattern'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_dIl2j8iaPMg/SnLtDnLkixI/AAAAAAAABEQ/SdoPYqaC_Qs/s72-c/image_thumb.png?imgmax=800' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-365398544774012271</id><published>2009-07-22T12:05:00.003+01:00</published><updated>2009-10-05T16:35:41.559+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>State Design Pattern</title><content type='html'>&lt;p&gt;&lt;a style="display: none;" href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=473787" rel="tag"&gt;codeproject &lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p align="justify"&gt;In state design pattern, it can change its behaviour by switching to a set of different operations and we achieved this pattern from an object variable changing its subclass, within a hierarchy. Allow an object to alter its behaviour when its internal state changes. The object will appear to change its class.&lt;/p&gt;  &lt;h3&gt;Explanation&lt;/h3&gt;  &lt;p align="justify"&gt;The State pattern has some &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/mediator-design-pattern.html"&gt;Mediator design pattern&lt;/a&gt; aspects to it. To understand more about state design patterns lets look in more detail of an example has been mentioned  by  in many books and many blogs but i myself could not find its actual and true implementation of it which is Assume you have a sales order that can be in several different states, such as "NewOrder," "Registered," "Granted," "Shipped," "Invoiced," and "Cancelled." There are strict rules concerning to which states the order can "go," and from which states it can "come." In my example, for instance, the state's not allowed to go directly from Registered to Shipped. This example is from Applying Domain-Driven Design and Patterns: With Examples in C# and .NET [Addison-Wesley Professional, ISBN: 0321268202] by Jimmy Nilsson. However there was not any code implementation of it. &lt;/p&gt;  &lt;p align="justify"&gt;There are also differences in behaviour depending upon the states. For example, when order is dispatched, you can't call registered or approved or cancel order for adding more items to the order. Always remember that certain behaviour leads to state transformation. UML diagram of state design pattern is &lt;/p&gt;  &lt;h3&gt;UML Diagram&lt;/h3&gt;  &lt;p&gt; &lt;a href="http://lh6.ggpht.com/_dIl2j8iaPMg/SmXr3SVjlgI/AAAAAAAABDo/0qctYpSJEWI/s1600-h/image%5B7%5D.png"&gt;&lt;img title="image" style="border-width: 0px; display: inline;" alt="image" src="http://lh6.ggpht.com/_dIl2j8iaPMg/SmXr4HDc-SI/AAAAAAAABDs/dy-fonvnUO4/image_thumb%5B3%5D.png?imgmax=800" width="417" border="0" height="201" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In our example &lt;/p&gt;  &lt;p&gt;&lt;span style="font-weight: bold;"&gt;Context &lt;/span&gt;= OrderState,
&lt;span style="font-weight: bold;"&gt;State &lt;/span&gt;=  IOrderState ,     
&lt;b&gt;Concrete State&lt;/b&gt; = (NewOrder, Registered, Dispatched, Approved)&lt;/p&gt;  &lt;p&gt;SO we have an interface called  IOrderState which manages all the states of an order i have kept it to only 4 different state  in my example as below&lt;/p&gt;  &lt;pre style="border: 1px solid rgb(255, 255, 255); padding: 5px; overflow: auto; min-height: 40px; width: 430px; background-color: rgb(233, 233, 233);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt; IOrderState
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;        {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; NewOrderPlaced();
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register();
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Dispatch();
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Approve();
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;        }&lt;/pre&gt;&lt;/pre&gt;

&lt;p align="justify"&gt;Then we have four concrete state classes which implements IOrderState interface which are NewOrder, Registered, Dispatched, Approved as given below&lt;/p&gt;

&lt;pre style="border: 1px solid rgb(255, 255, 255); padding: 5px; overflow: auto; min-height: 40px; width: 430px; background-color: rgb(233, 233, 233);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; NewOrder : IOrderState
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;        {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt; OrderState _Parent;
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; NewOrder(OrderState OrderState)
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;                _Parent = OrderState;
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.NewOrderPlaced();
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;           
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; IsDispatched
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; NewOrderPlaced()
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;                Console.WriteLine("&lt;span style="color: rgb(139, 0, 0);"&gt;NewOrderPlaced&lt;/span&gt;");
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Dispatch()
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;                _Parent._CurrentState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Dispatched(_Parent);
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register()
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;                _Parent._CurrentState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Registered(_Parent);
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Approve()
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            {
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;                _Parent._CurrentState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Approved(_Parent);
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;        }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Registered : IOrderState
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;        {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt; OrderState _Parent;
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; NewOrderPlaced()
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Exception("&lt;span style="color: rgb(139, 0, 0);"&gt;OrderState has already been placed&lt;/span&gt;");
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; Registered(OrderState OrderState)
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                _Parent = OrderState;
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Register();
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Dispatch()
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Exception("&lt;span style="color: rgb(139, 0, 0);"&gt;OrderState has not been registered yet&lt;/span&gt;");
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register()
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                Console.WriteLine("&lt;span style="color: rgb(139, 0, 0);"&gt;Regisrered&lt;/span&gt;");
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Approve()
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                _Parent._CurrentState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Approved(_Parent);
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            }
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;        }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Approved : IOrderState
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;        {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt; OrderState _Parent;
&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);font-family:consolas,'Courier New',courier,monospace;font-size:11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; Approved(OrderState OrderState)
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;                _Parent = OrderState;
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Approve();
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; NewOrderPlaced()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Exception("&lt;span style="color: rgb(139, 0, 0);"&gt;OrderState has already bene placed&lt;/span&gt;");
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Dispatch()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                _Parent._CurrentState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Dispatched(_Parent);
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Exception("&lt;span style="color: rgb(139, 0, 0);"&gt;OrderState has already been registered&lt;/span&gt;");
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Approve()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;                Console.WriteLine("&lt;span style="color: rgb(139, 0, 0);"&gt;Approved&lt;/span&gt;");
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;        }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Dispatched : IOrderState
&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 11px;"&gt;        {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt; OrderState _Parent;
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; NewOrderPlaced()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Exception("&lt;span style="color: rgb(139, 0, 0);"&gt;OrderState has already bene placed&lt;/span&gt;");
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; Dispatched(OrderState OrderState)
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                _Parent = OrderState;
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Dispatch();
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Dispatch()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;                Console.WriteLine("&lt;span style="color: rgb(139, 0, 0);"&gt;dispatched&lt;/span&gt;");
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Exception("&lt;span style="color: rgb(139, 0, 0);"&gt;OrderState has already been registered&lt;/span&gt;");
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Approve()
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(247, 247, 247);"&gt;                _Parent._CurrentState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Approved(_Parent);
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;        }&lt;/pre&gt;&lt;/pre&gt;

&lt;p align="justify"&gt;Then we have a, OrderState class which is a context class that manages all the states of a class as below &lt;/p&gt;

&lt;pre style="border: 1px solid rgb(255, 255, 255); padding: 5px; overflow: auto; min-height: 40px; width: 430px; background-color: rgb(233, 233, 233);"&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; OrderState
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;        {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; IOrderState _CurrentState;
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; OrderState()
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                _CurrentState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; NewOrder(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Dispatch()
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                _CurrentState.Dispatch();
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register()
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                _CurrentState.Register();
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Approve()
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            {
&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="11px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                _CurrentState.Approve();
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            }
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;        }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;to use it&lt;/p&gt;

&lt;pre style="border: 1px solid rgb(255, 255, 255); padding: 5px; overflow: auto; min-height: 40px; width: 430px; background-color: rgb(233, 233, 233);"&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] args)
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;        {
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;            OrderState OrderState = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; OrderState();
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            OrderState.Register();
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;            OrderState.Approve();
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(247, 247, 247);"&gt;            OrderState.Dispatch();
&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 11px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;        }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;and output is &lt;/p&gt;

&lt;p align="justify"&gt;&lt;a href="http://lh3.ggpht.com/_dIl2j8iaPMg/SmXr4iCfNRI/AAAAAAAABDw/k-h01dCOB4Q/s1600-h/image%5B3%5D.png"&gt;&lt;img title="image" style="border-width: 0px; display: inline;" alt="image" src="http://lh5.ggpht.com/_dIl2j8iaPMg/SmXr40wkq9I/AAAAAAAABD0/-yODc9IYGak/image_thumb%5B1%5D.png?imgmax=800" width="310" border="0" height="155" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Note:&lt;/span&gt;&lt;/h4&gt;

&lt;p align="justify"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Now the beauty of this pattern is once you have OrderState.Dispatch(); you cannot  user OrderState.Register(); it will throw and exception that OrderState has already been registered. So it also removes so many if/else statement. because it switches to different class depending on its state.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h4&gt;Things to remember&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Using state design pattern the program can potentially be flooded with small classes. &lt;/li&gt;

&lt;li&gt;Each derived  or small class has knowledge of (coupling to) its siblings, which introduces dependencies between subclasses. &lt;/li&gt;

&lt;li&gt;The state pattern requires tight coupling between its classes. &lt;/li&gt;

&lt;li&gt;State Design Pattern somehow solves your problem with the least amount of duplicated code and also the responsibility portioned out into encapsulated and cohesive unites, the concrete state classes &lt;/li&gt;

&lt;li&gt;State has no state usually it  selects the next state of its context. A state tends to have lots of unrelated methods, so there is little cohesion between the methods of a state. &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Difference between &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/strategy-pattern-design-pattern.html"&gt;Strategy Design Pattern&lt;/a&gt; and &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/state-design-pattern.html"&gt;State Design Pattern&lt;/a&gt;.&lt;/h4&gt;

&lt;p&gt;Strategy pattern is similar to the &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/state-design-pattern.html"&gt;State Design Pattern&lt;/a&gt; from an architectural point of view but the intent is entirely different.Unlike the State pattern, the Algorithm class doesn’t manage state, but represents an algorithm that uses one or more IStrategy implementations.The IStrategy implementation isn’t managed by the Algorithm class, but assigned by the client where as a state usually selects the next state of its context. A state tends to have lots of unrelated methods, so there is little cohesion between the methods of a state. For more detail please read my posting on &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/strategy-pattern-design-pattern.html"&gt;Strategy Design Pattern&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-365398544774012271?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/365398544774012271/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/state-design-pattern.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/365398544774012271'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/365398544774012271'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/state-design-pattern.html' title='State Design Pattern'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_dIl2j8iaPMg/SmXr4HDc-SI/AAAAAAAABDs/dy-fonvnUO4/s72-c/image_thumb%5B3%5D.png?imgmax=800' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-5532439439254717239</id><published>2009-07-14T16:24:00.001+01:00</published><updated>2009-07-16T12:33:56.361+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Mediator design pattern</title><content type='html'>&lt;a style="display: none" href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=473787" rel="tag"&gt;codeproject &lt;/a&gt;  &lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p align="justify"&gt;The Mediator pattern is there to enable objects to communicate without knowing each other’s identities.it promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. &lt;/p&gt;  &lt;h4&gt;Explanation&lt;/h4&gt;  &lt;p align="justify"&gt;Mediator design pattern deal with communication. Today there could be many ways of communication. Consider a Facebook group where so many people becomes fan and get the messages so people interested in that group can get messages&amp;#160; by subscribing to it.&amp;#160; Any messages that come in will go to all of the list’s subscribers. Members can remove themselves from the list at any time by going to their notification options or from their group preferences. So clearly the moderation process here is performing part of the role of the Mediator pattern is&amp;#160; establishing a communication protocol and content filter. The other part keeping fan unaware of each other is inherently part of the mailing list.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;FacebookGroupMediator&lt;/strong&gt;&amp;#160; is playing a role of mediator which has all the preferences&amp;#160; or filters option of&amp;#160; sign on, block or unblock etc.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Fan &lt;/strong&gt;is a class who send and receives messages on that group or in any mailing list. And that class is associates his self to FacebookGroupMediator.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;FanB &lt;/strong&gt;is a class in a which does not get copies of own messages.&lt;/p&gt;  &lt;pre style="border-right: #ffffff 1px solid; padding-right: 5px; border-top: #ffffff 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #ffffff 1px solid; width: 430px; padding-top: 5px; border-bottom: #ffffff 1px solid; background-color: #ffeee6"&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FacebookGroupMediator
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Callback
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; from);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        Callback respond;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SignOn(Callback method)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            respond += method;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Block(Callback method)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            respond -= method;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Unblock(Callback method)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            respond += method;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #008000"&gt;// Send is implemented as a broadcast&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Send(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; from)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            respond(message, from);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            Console.WriteLine();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Fan
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        FacebookGroupMediator mediator;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; name;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Fan(FacebookGroupMediator mediator, 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; name)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.mediator = mediator;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            mediator.SignOn(Receive);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.name = name;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Receive(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; from)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            Console.WriteLine(name + &amp;quot;&lt;span style="color: #8b0000"&gt; received from &lt;/span&gt;&amp;quot; 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                + from + &amp;quot;&lt;span style="color: #8b0000"&gt;: &lt;/span&gt;&amp;quot; + message);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Send(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;Send (From &lt;/span&gt;&amp;quot;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                + name + &amp;quot;&lt;span style="color: #8b0000"&gt;): &lt;/span&gt;&amp;quot; + message);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            mediator.Send(message, name);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FanB : Fan
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; FanB(FacebookGroupMediator mediator, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; name)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            : &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;(mediator, name)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #008000"&gt;// Does not get copies of own messages&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Receive(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; from)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!String.Equals(from, name))
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                Console.WriteLine(name + 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                    &amp;quot;&lt;span style="color: #8b0000"&gt; received from &lt;/span&gt;&amp;quot; + from + &amp;quot;&lt;span style="color: #8b0000"&gt;: &lt;/span&gt;&amp;quot; + message);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        FacebookGroupMediator m = 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FacebookGroupMediator();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        Fan fan1 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Fan(m, &amp;quot;&lt;span style="color: #8b0000"&gt;Fan 1&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        FanB fan2 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FanB(m, &amp;quot;&lt;span style="color: #8b0000"&gt;Fan 2&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        Fan fan3 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Fan(m, &amp;quot;&lt;span style="color: #8b0000"&gt;Fan 3&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        fan1.Send(&amp;quot;&lt;span style="color: #8b0000"&gt;i like this group&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #008000"&gt;// this is the fan who does not want to get copy own messages&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        fan2.Send(&amp;quot;&lt;span style="color: #8b0000"&gt;yes i also like this group&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #008000"&gt;// fan 3 temp uncheck to get the messages&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        m.Block(fan3.Receive); 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        fan1.Send(&amp;quot;&lt;span style="color: #8b0000"&gt;Do you agree that this is the best group&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        fan2.Send(&amp;quot;&lt;span style="color: #8b0000"&gt;Yes i Agree&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        m.Unblock(fan3.Receive); 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #008000"&gt;// open again&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        fan1.Send(&amp;quot;&lt;span style="color: #8b0000"&gt;Thanks all&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;and the output is&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh3.ggpht.com/_dIl2j8iaPMg/SlyjL7pnIwI/AAAAAAAABDg/lT6EexIJqSg/s1600-h/image%5B4%5D.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="277" alt="image" src="http://lh4.ggpht.com/_dIl2j8iaPMg/SlyjMVsCLyI/AAAAAAAABDk/y1SJxAN4RZg/image_thumb%5B2%5D.png?imgmax=800" width="472" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So when fan1 posts a message other fans receive the message.If a fan3 uncheck its receiving option then fan3 does get any updates regarding to messages.&lt;/p&gt;

&lt;h3&gt;Difference between &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/observer-design-pattern.html" target="_blank"&gt;Observer Design Pattern in C#&lt;/a&gt; and &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/mediator-design-pattern.html" target="_blank"&gt;Mediator design pattern&lt;/a&gt;&lt;/h3&gt;

&lt;p align="justify"&gt;In my previous article &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/observer-design-pattern.html"&gt;Observer Design Pattern in C#&lt;/a&gt; i have explained an example of blog and their subscribers. Mediator and Observer are very similar patterns. Mailing lists, chat rooms, groups and blogs etc&amp;#160; all involve communication between individuals who actively sign up to the activity. The difference Mediator centralizes the communication between fans, class workers, colleagues, whereas the Observer distributes control of information by differentiating between subjects (publishers) and observers (subscribers). Mediator hides all activities that it implements, the user is better able to understand the system and the interactions that that the fan make.&lt;/p&gt;

&lt;p align="justify"&gt;The communication in the Mediator pattern is easier to understand. Fans send messages to a mediator and the transmission of the information further to whomever is currently in the group is handled there, where as In the Observer pattern do not send any information; they wait to receive it from subjects, who distributed it to all who have subscribed. An example is the blog. for further details visit at &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/observer-design-pattern.html"&gt;Observer Design Pattern in C#&lt;/a&gt; . 

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Reference:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a title="http://www.dofactory.com/Patterns/PatternMediator.aspx" href="http://www.dofactory.com/Patterns/PatternMediator.aspx"&gt;http://www.dofactory.com/Patterns/PatternMediator.aspx&lt;/a&gt; 

    &lt;br /&gt;C# 3.0 Design Patterns Judith Bishop&lt;/p&gt;&lt;/blockquote&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-5532439439254717239?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/5532439439254717239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/mediator-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/5532439439254717239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/5532439439254717239'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/mediator-design-pattern.html' title='Mediator design pattern'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_dIl2j8iaPMg/SlyjMVsCLyI/AAAAAAAABDk/y1SJxAN4RZg/s72-c/image_thumb%5B2%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-4721595528132623274</id><published>2009-07-10T14:02:00.001+01:00</published><updated>2009-07-10T16:21:29.590+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Observer Design Pattern in C#</title><content type='html'>&lt;h4&gt;Introduction &lt;/h4&gt;  &lt;p&gt;The Observer design pattern&amp;#160; is a relationship between objects so that when one    &lt;br /&gt;changes its state, all the others are notified accordingly.&amp;#160; In other words it defines that an object must be able to notify other objects without making assumptions about what these objects are.&lt;/p&gt;  &lt;p&gt;Observer design pattern is quite useful when a&amp;#160; change to one object requires changing others but, you don't know how many objects need to be changed.&lt;/p&gt;  &lt;h3&gt;Using Observer Design Pattern in Blog Subscriber Example.&lt;/h3&gt;  &lt;p&gt;In this example i have used&amp;#160; two other behavioral patterns which are &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/iterator-design-pattern-in-c.html"&gt;Iterator Design Pattern in C#&lt;/a&gt; and &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/facade-design-pattern-in-c.html"&gt;Facade Design Pattern in C#&lt;/a&gt;. The simplest way to understand observer pattern is to look at blogs, which are website where people write about different topics. Take a example of a one blog where&amp;#160; so many users have subscribed themselves for&amp;#160;&amp;#160; any new post. As we all know bloggers don't always post regularly so it would be great for&amp;#160; bloggers to alert his all subscribers whenever blogger post a new topic.&amp;#160; &lt;/p&gt; &lt;strong&gt;&lt;/strong&gt;  &lt;p&gt;&lt;strong&gt;Blog&lt;/strong&gt;&amp;#160; is a class which deals with the&amp;#160; new blog post and also with the blog subscribers.     &lt;br /&gt;&lt;strong&gt;BlogPost&lt;/strong&gt;&amp;#160; is a class which deals with the posting of new article.     &lt;br /&gt;&lt;strong&gt;Subscribers&lt;/strong&gt;&amp;#160; are the user who have subscribed themselves for alerts of new articles.     &lt;br /&gt;&lt;strong&gt;Facade&lt;/strong&gt; deals them in simpler way and give you operations like PublishSinglePost() or PublishMultiPost()&amp;#160; etc.     &lt;br /&gt;&lt;strong&gt;Subject&lt;/strong&gt; class whose instances independently change their state and it notify to all the subscribers. Run method loops through all of the Subscribers calling their Update methods to notify them about post. Its also contains methods for attaching and detaching functions with event&lt;/p&gt;  &lt;p&gt;Imagine using &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/iterator-design-pattern-in-c.html"&gt;Iterator Design Pattern in C#&lt;/a&gt; we have a list of subscriber as follows. &lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; text-align: left"&gt;   &lt;div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Subscriber&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;  {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;      &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;      &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Email { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;      &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Subscriber(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; name, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; email)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;      {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;          &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Name = name;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;          &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Email = email;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;      }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;  }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;  &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; BlogSubscribers : IEnumerable&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;  {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;      Subscriber[] Arrays = { &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Subscriber(&lt;span style="color: #006080"&gt;&amp;quot;user1&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;firstEmail@gmail.com&amp;quot;&lt;/span&gt;), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Subscriber(&lt;span style="color: #006080"&gt;&amp;quot;user2&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;2ndEmail@gmail.com&amp;quot;&lt;/span&gt;) };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;      &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerator GetEnumerator()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;      {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;          &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (Subscriber element &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Arrays)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;              &lt;span style="color: #0000ff"&gt;yield&lt;/span&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; element;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;      }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;  }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;For more detail related to above code and Iterator design pattern read my article on &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/iterator-design-pattern-in-c.html"&gt;Iterator Design Pattern in C#&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Subject&lt;/strong&gt; class whose instances independently change their state and it notify to all the subscribers. It defines a Dictionary object to store the subscribers name and their callback functions.&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; text-align: left"&gt;
  &lt;div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Callback(String subscriber);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;   &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Subject&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;//Declaring delegate &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, Callback&amp;gt; Notify = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, Callback&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;//Declaring Event &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;// List of Users as IEnumerable&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       BlogSubscribers blogSubscribers = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogSubscribers();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// Attaching the events&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;subscriber&amp;quot;&amp;gt;its the name of user or email address&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;Update&amp;quot;&amp;gt;A method to attach &amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Attach(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; subscriber, Callback Update)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;           &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!Notify.ContainsKey(subscriber))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;           {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;               Notify[subscriber] = &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; { };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;           }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;           Notify[subscriber] += Update;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// To Detach method for specific user&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;subscriber&amp;quot;&amp;gt;its the name of user or email address&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;Update&amp;quot;&amp;gt;A method to attach&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Detach(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; subscriber, Callback Update)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;           Notify[subscriber] -= Update;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// Loop through all users to notify for anychange&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;       &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Run()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;           &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (Subscriber subscriber &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; blogSubscribers)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;           {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;               &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #006080"&gt;&amp;quot; To {0}&amp;lt;{1}&amp;gt; &amp;quot;&lt;/span&gt;, subscriber.Name, subscriber.Email);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;               Notify[subscriber.Name](message);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;           }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;An interface for IObserver specifying how they should be updated. A BlogSpot for posting new articles and an observers as a BlogObserver&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; text-align: left"&gt;
  &lt;div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IObserver&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Update(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; BlogPost&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; PostName { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; BlogObserver : IObserver&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #008000"&gt;//name of the posting&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #008000"&gt;// Implmenting through IOC Inversion of control &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        Subject subject;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        List&amp;lt;BlogPost&amp;gt; ListOfBlogNewPosts = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;BlogPost&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; BlogObserver(Subject subject)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.subject = subject;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            subject.Attach(&lt;span style="color: #006080"&gt;&amp;quot;user1&amp;quot;&lt;/span&gt;, Update);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            subject.Attach(&lt;span style="color: #006080"&gt;&amp;quot;user2&amp;quot;&lt;/span&gt;, Update);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AddPost(BlogPost blogPost)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            ListOfBlogNewPosts.Add(blogPost);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Update(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (BlogPost blogPost &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; ListOfBlogNewPosts)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;                Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;--------------------- New Posting ---------------------&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;                Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot; {0} for Post {1}&amp;quot;&lt;/span&gt;, message, blogPost.PostName);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;                Console.WriteLine();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DetachFromPost(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; subscriber)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            subject.Detach(subscriber, Update);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;I have used &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/facade-design-pattern-in-c.html"&gt;Facade Design Pattern in C#&lt;/a&gt; to make an easy interface for these different sub systems.&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; text-align: left"&gt;
  &lt;div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; BlogFacade&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PublishMultiPost()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            Subject subject = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Subject();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            BlogPost Ps3 = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogPost();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            Ps3.PostName = &lt;span style="color: #006080"&gt;&amp;quot;PS3&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            BlogPost Xbox = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogPost();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            Xbox.PostName = &lt;span style="color: #006080"&gt;&amp;quot;Xbox&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            BlogObserver salmanBlog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogObserver(subject);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            salmanBlog.AddPost(Ps3);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            salmanBlog.AddPost(Xbox);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #008000"&gt;//lets try to detach users2 from the subscriber list  for posting xbox&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #008000"&gt;//salmanBlog.DetachFromPost(&amp;quot;user2&amp;quot;);&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            subject.Run();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PublishSinglePost()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            Subject subject = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Subject();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            BlogPost Xbox = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogPost();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            Xbox.PostName = &lt;span style="color: #006080"&gt;&amp;quot;Xbox&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            BlogObserver salmanBlog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogObserver(subject);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            salmanBlog.AddPost(Xbox);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            subject.Run();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;For more detail regarding facade pattern visit &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/facade-design-pattern-in-c.html"&gt;Facade Design Pattern in C#&lt;/a&gt;.&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; text-align: left"&gt;
  &lt;div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;  {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;      &lt;span style="color: #008000"&gt;//BlogFacade.PublishSinglePost();&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;      BlogFacade.PublishMultiPost();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;  }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h4&gt;OUTPUT &lt;/h4&gt;

&lt;p&gt;So if you run facade operation BlogFacade.PublishMultiPost(); by commenting //salmanBlog.DetachFromPost(&amp;quot;user2&amp;quot;); then output will be &lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_dIl2j8iaPMg/SldWcLf579I/AAAAAAAABC4/9PfEAeALoR0/s1600-h/image1%5B3%5D.png"&gt;&lt;img title="image1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="150" alt="image1" src="http://lh4.ggpht.com/_dIl2j8iaPMg/SldWcs3695I/AAAAAAAABC8/l_-HrarBVtM/image1_thumb%5B1%5D.png?imgmax=800" width="333" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;and if you uncomment this salmanBlog.DetachFromPost(&amp;quot;user2&amp;quot;); and run BlogFacade.PublishMultiPost(); the out put will be&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_dIl2j8iaPMg/SldZ64Dnm2I/AAAAAAAABDI/kXMMFN18bUo/s1600-h/image2%5B1%5D.png"&gt;&lt;img title="image2" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="88" alt="image2" src="http://lh5.ggpht.com/_dIl2j8iaPMg/SldWdr2DV_I/AAAAAAAABDM/-kCYuCpkmws/image2_thumb%5B1%5D.png?imgmax=800" width="333" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;because we have unsubscribed user2 from the list. So that is our blog on Observer pattern if&amp;#160; for any new post or change in post all the others are notified accordingly.&lt;/p&gt;

&lt;p&gt;Your feedback is welcome. 
  &lt;br /&gt;&lt;a href="http://sites.google.com/site/aspdotnetpatterns/Home/ObserverDesignPattern.zip?attredirects=0"&gt;download code here&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-4721595528132623274?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/4721595528132623274/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/observer-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/4721595528132623274'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/4721595528132623274'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/observer-design-pattern.html' title='Observer Design Pattern in C#'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_dIl2j8iaPMg/SldWcs3695I/AAAAAAAABC8/l_-HrarBVtM/s72-c/image1_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-2313564719917775032</id><published>2009-07-10T11:58:00.000+01:00</published><updated>2009-08-14T17:23:38.124+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Facade Design Pattern in C#</title><content type='html'>&lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h3&gt;What is Facade Design Pattern?&lt;/h3&gt;  &lt;p align="justify"&gt;Facade defines a higher-level interface that makes the subsystem easier to use. Facade&amp;#160; is one of the common and easiest patterns. It wrap a complicated subsystem with a simpler interface. As a developer we often use this facade pattern. Even some of them don't recognise while using it that its a facade patterns. I have tried to explain facade pattern by building a blog system.&lt;/p&gt;  &lt;h4&gt;When to use it?&lt;/h4&gt;  &lt;p align="justify"&gt;Facade design patterns&amp;#160; gives easier interface for common task. It provides simplified and uniform interface to a large subsystem of classes. For example in a shopping cart system&amp;#160; there could be many API’s related to register user, purchase,user detail, transactions etc so facade deals with all these interfaces and provide one simple modified interface to deals with them.&lt;/p&gt;  &lt;p align="justify"&gt;A more simple non-software example can be if you want to play a game suppose Metal gear Solid on PS3 (Did i tell you that i love that game?). You Require PS3, HD TV, Controller etc.&amp;#160; So there are so many sub system could be involve if you want to play a game on ps3. But actually all you are doing in simpler term is playing a game. So facade can give you a simple interface with a operation call PlayGame() in this example.The Facade object should be a fairly simple advocate or facilitator. If the Facade is the only access point for the subsystem, it will limit the features and flexibility that “power users” may need.&lt;/p&gt;  &lt;h4&gt;Using Facade in Blog Example&lt;/h4&gt;  &lt;p align="justify"&gt;Consider another example of a Library. Where people borrow and returns books. &lt;/p&gt;  &lt;h4&gt;UML Diagram&lt;/h4&gt;  &lt;p align="justify"&gt;&lt;a href="http://lh6.ggpht.com/_dIl2j8iaPMg/SoWMJjjmC5I/AAAAAAAABEw/gnQpsDfJVU0/s1600-h/Un3titled%5B2%5D.jpg"&gt;&lt;img title="Un3titled" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="169" alt="Un3titled" src="http://lh5.ggpht.com/_dIl2j8iaPMg/SoWMKJn3jqI/AAAAAAAABE0/xiUxqM_MT_U/Un3titled_thumb.jpg?imgmax=800" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In our example we have   &lt;br /&gt;&lt;strong&gt;Facade :&lt;/strong&gt;Facade    &lt;br /&gt;&lt;strong&gt;Subsystem classes :&lt;/strong&gt; Book ,Shelf , Person&lt;/p&gt;  &lt;pre style="border-right: #ffffff 1px solid; padding-right: 5px; border-top: #ffffff 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #ffffff 1px solid; width: 500px; padding-top: 5px; border-bottom: #ffffff 1px solid; background-color: #e9e9e9"&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; program
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        internal &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Book
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            internal &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; FindBook()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                return &amp;quot;&lt;span style="color: #8b0000"&gt;Subsystem for Book, Method FindBook\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            internal &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Returned()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                return &amp;quot;&lt;span style="color: #8b0000"&gt;Subsystem for Book, Method Returned\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        internal &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Shelf
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            internal &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; AssignBook()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                return &amp;quot;&lt;span style="color: #8b0000"&gt;Subsystem for Shelf, Method AssignBook\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            internal &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; PlaceBook()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                return &amp;quot;&lt;span style="color: #8b0000"&gt;Subsystem for Shelf, Method PlaceBook\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        internal &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Person
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            internal &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Return()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                return &amp;quot;&lt;span style="color: #8b0000"&gt;Subsystem For  Person, Method Return\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            internal &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Borrow()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                return &amp;quot;&lt;span style="color: #8b0000"&gt;Subsystem For  Person, Method Borrow\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; static &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Facade
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            static Book book = new Book();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            static Shelf shelf = new Shelf();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            static Person person = new Person();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; static void ReturnBook()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;ReturnBook 1\n&lt;/span&gt;&amp;quot; +
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                person.Return() +
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                shelf.PlaceBook()+
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                book.Returned());
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; static void BorrowBook()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;BorrowBook 1\n&lt;/span&gt;&amp;quot; +
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;               book.FindBook() +
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                person.Borrow()                 
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;                );
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        static void Main()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            Facade.BorrowBook();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            Facade.ReturnBook();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_dIl2j8iaPMg/SoWOopVpuHI/AAAAAAAABFA/fQcTl8r9EGw/s1600-h/umlState%5B3%5D.jpg"&gt;&lt;img title="umlState" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="247" alt="umlState" src="http://lh3.ggpht.com/_dIl2j8iaPMg/SoWOpSLg8dI/AAAAAAAABFE/qM24Gm8pg6k/umlState_thumb%5B1%5D.jpg?imgmax=800" width="454" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p align="justify"&gt;Above was very simple demonstration of using a facade design pattern. The role of above example was to provide&amp;#160; an easier interface for different high-level views of subsystems whose details are hidden from users.Everything in the façade has to be public so that the Client, which is compiled into a different assembly, can access it. In library example we have different subsystems and using facade we give very simple interface to client.&lt;/p&gt;

&lt;p&gt;Your feedback is welcome. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-2313564719917775032?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/2313564719917775032/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/facade-design-pattern-in-c.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/2313564719917775032'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/2313564719917775032'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/facade-design-pattern-in-c.html' title='Facade Design Pattern in C#'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_dIl2j8iaPMg/SoWMKJn3jqI/AAAAAAAABE0/xiUxqM_MT_U/s72-c/Un3titled_thumb.jpg?imgmax=800' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-8008648487234759982</id><published>2009-07-09T15:52:00.001+01:00</published><updated>2009-07-17T09:01:05.167+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Iterator Design Pattern in C#</title><content type='html'>&lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p align="justify"&gt;The Iterator pattern provides a way of accessing elements of a collection sequentially,    &lt;br /&gt;without exposing its underlying representation. An Iterator object encapsulates the internal structure of how the iteration occurs.&lt;/p&gt;  &lt;h4&gt;Explanation&lt;/h4&gt;  &lt;p align="justify"&gt;The Iterator design pattern is useful because it provides a defined way to move thought a set of elements without exposing what is taking place inside a class. Enumerators are responsible for producing the next element in a sequence defined by certain criteria. Such a sequence is said to be enumerable. &lt;/p&gt;  &lt;h4&gt;Example of Blog Subscribers&lt;/h4&gt;  &lt;p align="justify"&gt;Consider an example of a blog where users subscribes themselves&amp;#160; to get alerts for newer post. We can use iterator pattern in this example because all we want to returns the list of emails of subscribers without really know its&amp;#160; its underlying representation.&lt;/p&gt;  &lt;p align="justify"&gt;Suppose we have a class called &lt;strong&gt;Subscriber&lt;/strong&gt; which takes name and email address to subscribe for&amp;#160; new posts for blog.&lt;/p&gt;  &lt;pre style="border-right: #ffffff 1px solid; padding-right: 5px; border-top: #ffffff 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #ffffff 1px solid; width: 430px; padding-top: 5px; border-bottom: #ffffff 1px solid; background-color: #ffeee6"&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Subscriber
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Email { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Subscriber(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; name, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; email)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Name = name;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Email = email;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;p align="justify"&gt;Then we have a class Blog Subscribers which&amp;#160; fetch the list of email addresses from database&amp;#160; however i have used&amp;#160; an array of blog subscribers in this example &lt;/p&gt;

&lt;pre style="border-right: #ffffff 1px solid; padding-right: 5px; border-top: #ffffff 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #ffffff 1px solid; width: 430px; padding-top: 5px; border-bottom: #ffffff 1px solid; background-color: #ffeee6"&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;   &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; BlogSubscribers : IEnumerable
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        Subscriber[] Arrays = { &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Subscriber(&amp;quot;&lt;span style="color: #8b0000"&gt;user1&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color: #8b0000"&gt;firstEmail@gmail.com&lt;/span&gt;&amp;quot;), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Subscriber(&amp;quot;&lt;span style="color: #8b0000"&gt;user2&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color: #8b0000"&gt;2ndEmail@gmail.com&lt;/span&gt;&amp;quot;) };
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerator GetEnumerator()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (Subscriber element &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Arrays)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;                yield &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; element;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;You may have noticed that BlogSubscribers is an IEnumerable. IEnumerable is an interface that has one method, GetEnumerator. Classes that implement IEnumerable supply a GetEnumerator method that successively returns a value from a collection using yield return. Each time GetEnumerator is invoked, control picks up after the yield return statement.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre style="border-right: #ffffff 1px solid; padding-right: 5px; border-top: #ffffff 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #ffffff 1px solid; width: 430px; padding-top: 5px; border-bottom: #ffffff 1px solid; background-color: #ffeee6"&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;  &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main()
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        BlogSubscribers collectionOfBlogSubscribers = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogSubscribers();
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        &lt;span style="color: #008000"&gt;// Consumes values generated from collectionOfBlogSubscribers's GetEnumerator method&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (Subscriber subscriber &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; collectionOfBlogSubscribers)
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;            Console.Write(&amp;quot;&lt;span style="color: #8b0000"&gt;To {0}&amp;lt;{1}&amp;gt;&lt;/span&gt;&amp;quot;, subscriber.Name, subscriber.Email);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;            Console.WriteLine(&amp;quot;&lt;span style="color: #8b0000"&gt;\n&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #ffffff"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; background-color: #f7f7f7"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://lh3.ggpht.com/_dIl2j8iaPMg/SlYEHGpUt5I/AAAAAAAABBg/3ZMDfLdoNAQ/s1600-h/image%5B7%5D.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="154" alt="image" src="http://lh3.ggpht.com/_dIl2j8iaPMg/SlYEHmoS6lI/AAAAAAAABBk/1Ro855RqGRc/image_thumb%5B5%5D.png?imgmax=800" width="323" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;How does it Work?&lt;/h4&gt;

&lt;p align="justify"&gt;The BlogSubscribers.GetEnumerator method encounters a yield statement and returns the value of first subscriber. But before returning from the method, a bookmark is made to where execution returned control.In the foreach loop, the .NET runtime assigns the value to subscriber object. The loop executes and calls the method Console.WriteLine. Then a new loop is started, and the bookmark in the GetEnumerator method is the starting
  &lt;br /&gt;point of the next execution.Then another yield statement is encountered, and the value of&amp;#160; 2nd subscriber is returned. Another foreach loop occurs, and this bookmarking and looping occurs until there are no more yields.&lt;/p&gt;

&lt;p align="justify"&gt;Now you may argue and&amp;#160; wonder why we simply did not loop through the Arrays of subscribers The reason is that subscribers is declared not in the Client, but in its own collection class. Even without the benefits gained from separating iteration and enumeration, it would be bad style for the Client to subscribers directly.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-8008648487234759982?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/8008648487234759982/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/iterator-design-pattern-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/8008648487234759982'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/8008648487234759982'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/iterator-design-pattern-in-c.html' title='Iterator Design Pattern in C#'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_dIl2j8iaPMg/SlYEHmoS6lI/AAAAAAAABBk/1Ro855RqGRc/s72-c/image_thumb%5B5%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-3422586040245826968</id><published>2009-07-06T15:43:00.002+01:00</published><updated>2010-09-08T16:47:22.054+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='Singleton design pattern'/><title type='text'>Singleton Design Pattern in Asp.net using C#</title><content type='html'>&lt;a style="display: none;" href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=473787" rel="tag"&gt;codeproject &lt;/a&gt;  &lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p align="justify"&gt;When we want to make a only one instance of a class and also making sure that  there is a global access point to that object then the design pattern we user is called Singleton. The pattern ensures that the class is instantiated only once and that all requests are directed to that one and only object. &lt;/p&gt;  &lt;h4&gt;Usage&lt;/h4&gt;  &lt;p&gt;In asp.net you can use singleton through sessions or application variables easily however i will show you how to implement singleton object  to User class.&lt;/p&gt;  &lt;h4&gt;Problem&lt;/h4&gt;  &lt;p&gt;In Asp.net to get user information like first name, surname or username  through Memebership object does not look neat because  to get UserID first you need to know user User.Identity.Name then use Membership.GetUser in order to get more detail for user.&lt;/p&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;I have created a interface called IUser which you can use anywhere on the page.&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippet"   style="padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;     &lt;pre    style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt;font-family:'Courier New',courier,monospace;font-size:8pt;color:white;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt; IUser&lt;/pre&gt;
&lt;!--CRLF--&gt;

   &lt;pre   style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;      {&lt;/pre&gt;
&lt;!--CRLF--&gt;

   &lt;pre    style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt;font-family:'Courier New',courier,monospace;font-size:8pt;color:white;"&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; Email { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

   &lt;pre   style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; Firstname { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

   &lt;pre    style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt;font-family:'Courier New',courier,monospace;font-size:8pt;color:white;"&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; Surname { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

   &lt;pre face="'Courier New',courier,monospace" size="8pt" style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;"&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; UserID { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

   &lt;pre    style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt;font-family:'Courier New',courier,monospace;font-size:8pt;color:white;"&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; Username { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

   &lt;pre face="'Courier New',courier,monospace" size="8pt" style="padding: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;"&gt;      }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Here is our Singleton class implementing IUser.&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
 &lt;div id="codeSnippet" face="'Courier New',courier,monospace" size="8pt" style="padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-style: none; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;"&gt;
&lt;pre id="pre0" style="margin-top: 0pt;"&gt;public sealed class Singleton
      {
          private const string IUserSessionName = "User";
          private static readonly object padlock = new object();
          private static IUser objUser ;
          Singleton()
          {

          }
          /// &lt;summary&gt;
          /// Loading user information
          /// &lt;/summary&gt;
          public static IUser IUserInstance
          {
              get
              {
                  lock (padlock)
                  {
                                            
                         if (null == Session[IUserSessionName])
                         {
                         
                          string userID = Membership.GetUser().ProviderUserKey.ToString();
                          // getting userinformation from database
                          objUser = new UserClass().GetUserInformation(userID);
                          Session[IUserSessionName] = objUser;
                         }
                         else
                         {
                         objUser = (IUser)Current.Session[IUserSessionName];
                         }
                    
         
                     return objUser;
                 }
              }
              set
              {
                  Session[IUserSessionName] = value;
              }
          }
        
          public static void UserInstanceFlush()
          {
              Session[IUserSessionName] = null;
          }

      }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Because of IuserSessionName  you are also making sure that you will keep this session name unique and  at only one place.&lt;/p&gt;

&lt;p&gt;IUser is a static property which can be used on any page from Singleton.IUserInstance.UserID.&lt;/p&gt;

&lt;p&gt;UserInstanceFlush  flushes the session if you want to remove the session value.&lt;/p&gt;

&lt;p&gt;&lt;span style="color:#ff0000;"&gt;Note: People may argue its not a pure Singleton object example because its one instance of object per user and i agree with all of them its a one instance of object per user when they get authenticated throughout for all pages in application and give you extra user properties through the interface IUser.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="color:#000000;"&gt;your feed is welcome.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="color:#ff0000;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-3422586040245826968?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/3422586040245826968/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/singleton-design-pattern-in-aspnet.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/3422586040245826968'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/3422586040245826968'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/singleton-design-pattern-in-aspnet.html' title='Singleton Design Pattern in Asp.net using C#'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-3745334381431825082</id><published>2009-07-01T16:52:00.002+01:00</published><updated>2009-07-10T16:22:30.890+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Adapter Design Pattern How to use in Asp.net using C#</title><content type='html'>&lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h4&gt;What is Adapter Pattern?&lt;/h4&gt;  &lt;p&gt;The Adapter pattern is a structural&amp;#160; design pattern&amp;#160; which enables a system to use classes whose interfaces don’t quite match its requirements or in other words is used to make a interface which client understands. &lt;/p&gt;  &lt;h4&gt;When to use it?&lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt;Its quite useful when dealing with the legacy code specially that was written a while ago and to which one might not have access. &lt;/li&gt;    &lt;li&gt;It is especially useful for off-the-shelf code, for toolkits, for libraries or any third party software's &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Lots of us uses the enterprise library data access application block. Suppose later if we find that some other third party data access&amp;#160; application block is better than micorsoft data access application block&amp;#160; then in the project would not change just adapter internally would call the new data access&amp;#160; application block.    &lt;br /&gt;&lt;/p&gt;  &lt;h3&gt;Object adapter pattern&lt;/h3&gt;  &lt;p&gt;In Object adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.&amp;#160; &lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; text-align: left"&gt;     &lt;div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;       &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&lt;span style="color: #008000"&gt;// Existing class.. this may be your legacy code or third party code&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Adaptee&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #008000"&gt;// validates the email&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsEmail(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; email)&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; System.Text.RegularExpressions.Regex.IsMatch(email, &lt;span style="color: #006080"&gt;@&amp;quot;\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #008000"&gt;// Required standard  implementing through interface&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; ITarget&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #008000"&gt;// Rough estimate required&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; ValidateEmail(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; email);&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #008000"&gt;// Implementing the required standard via Adaptee&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Adapter : Adaptee, ITarget&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; ValidateEmail(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; email)&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; IsEmail(email);&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;    &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Client&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;        &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main()&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        {&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #008000"&gt;// Showing the Adapteee in standalone mode&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            Adaptee first = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Adaptee();&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            Console.Write(&lt;span style="color: #006080"&gt;&amp;quot;Before the new standard: &amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            Console.WriteLine(first.IsEmail(&lt;span style="color: #006080"&gt;&amp;quot;anyemail@gmail.com&amp;quot;&lt;/span&gt;));&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            &lt;span style="color: #008000"&gt;// What the client really wants&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            ITarget second = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Adapter();&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;            Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;\nMoving to the new standard&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;            Console.WriteLine(second.ValidateEmail(&lt;span style="color: #006080"&gt;&amp;quot;anyemail@gmail.com&amp;quot;&lt;/span&gt;));&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"&gt;        }&lt;/pre&gt;
&lt;!--CRLF--&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;ITarget&lt;/strong&gt; 

  &lt;br /&gt;The interface that the Client wants to use 

  &lt;br /&gt;&lt;strong&gt;Adaptee&lt;/strong&gt; 

  &lt;br /&gt;An implementation that needs adapting 

  &lt;br /&gt;&lt;strong&gt;Adapter &lt;/strong&gt;The class that implements the ITarget interface in terms of the Adaptee 

  &lt;br /&gt;&lt;strong&gt;ValidateEmail&lt;/strong&gt; 

  &lt;br /&gt;An operation that the Client wants 

  &lt;br /&gt;&lt;strong&gt;IsEmail&lt;/strong&gt; 

  &lt;br /&gt;The implementation of Request’s functionality in the Adaptee&lt;/p&gt;

&lt;p&gt;Note : &lt;/p&gt;

&lt;p&gt;The adapter pattern is also&amp;#160; useful in where an already existing class provides some or all of the services you need but does not use the interface you need. Also using this way you can change third-party library easily without effecting your project . In above example if you want to change the validation method of email all you need to do is to use ValidateEmail function for any new method or third party library functions.&lt;/p&gt;

&lt;p&gt;Your feedback is welcome.&lt;strong&gt;&lt;a href="http://aspdotnetpatterns.blogspot.com/2009/07/adapter-design-pattern-how-to-use-in.html"&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-3745334381431825082?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/3745334381431825082/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/adapter-design-pattern-how-to-use-in.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/3745334381431825082'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/3745334381431825082'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/07/adapter-design-pattern-how-to-use-in.html' title='Adapter Design Pattern How to use in Asp.net using C#'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-6075121761085844474</id><published>2009-06-29T16:59:00.004+01:00</published><updated>2009-08-24T12:18:30.385+01:00</updated><title type='text'>Asp.net date picker control part 2</title><content type='html'>&lt;p&gt;&lt;a style="display: none" href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=473787" rel="tag"&gt;codeproject &lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_dIl2j8iaPMg/Skjk7-3yTCI/AAAAAAAABBU/tM3uDihEPpk/s1600-h/image%5B2%5D.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="244" alt="image" src="http://lh5.ggpht.com/_dIl2j8iaPMg/Skjk8OIw17I/AAAAAAAABBY/BntyJmjycTM/image_thumb.png?imgmax=800" width="217" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h4&gt;&lt;a href="http://sites.google.com/site/aspdotnetpatterns/Home/DatePicker.zip?attredirects=0"&gt;download code here&lt;/a&gt;&lt;/h4&gt;  &lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;In my previous post &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/06/how-to-create-date-picker-composite.html" target="_blank"&gt;How to: Create a Date Picker Composite Control in ASP.NET (C#)&lt;/a&gt;. I explained how to work with asp.net composite control to create a date picker control for asp.net. &lt;/p&gt;  &lt;h3&gt;Problem&lt;/h3&gt;  &lt;p&gt;However to use that control you still require JavaScript, style and images to include on.&lt;/p&gt;  &lt;h3&gt;Solution&lt;/h3&gt;  &lt;p&gt;In this post i will explain you how i have embedded javascript , images and style sheet to my previous article . &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/06/how-to-create-date-picker-composite.html" target="_blank"&gt;How to: Create a Date Picker Composite Control in ASP.NET (C#)&lt;/a&gt;. Also i have added another property to control which is how to change date format for the control. You can download the code , drag and drop it and start using it inside data navigation controls or anywhere in your asp.net web application.&lt;/p&gt;  &lt;h3&gt;Property&lt;/h3&gt;  &lt;p&gt;&lt;strong&gt;DateFormat&lt;/strong&gt; By defaults its been set to &amp;quot;%d/%m/%Y&amp;quot; which is DMY. &lt;/p&gt;  &lt;h3&gt;How to embed ?&lt;/h3&gt;  &lt;p&gt;I added to the project images, java scripts, and styles that I need for my date picker control.Click on each file properties and change Build Action from Content to Embedded Resource, then modify the AssemblyInfo.cs as below&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; background-color: rgb(244,244,244); text-align: left"&gt;   &lt;pre id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; border-right-style: none; border-left-style: none; background-color: rgb(244,244,244); text-align: left; border-bottom-style: none"&gt;[assembly: System.Web.UI.WebResource(&lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.calendarview.css&amp;quot;&lt;/span&gt;, &lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt;)]
[assembly: System.Web.UI.WebResource(&lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.calendarview.js&amp;quot;&lt;/span&gt;, &lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;text/js&amp;quot;&lt;/span&gt;)]
[assembly: System.Web.UI.WebResource(&lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.CalendarIcon.gif&amp;quot;&lt;/span&gt;, &lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;img/gif&amp;quot;&lt;/span&gt;)]
[assembly: System.Web.UI.WebResource(&lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.prototype.js&amp;quot;&lt;/span&gt;, &lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;text/js&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;span style="color: #ff0000"&gt;Note: the format of embedded resources is very important which is &lt;/span&gt;&lt;/p&gt;

&lt;p align="center"&gt;&lt;strong&gt;[Assembly Name].[Folder].[File Name]&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;To embed JavaScript &lt;/h3&gt;

&lt;p&gt;To embed JavaScript i have created a function called AddJavaScript(string javaScriptFile) which is&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; background-color: rgb(244,244,244); text-align: left"&gt;
  &lt;pre id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; border-right-style: none; border-left-style: none; background-color: rgb(244,244,244); text-align: left; border-bottom-style: none"&gt;&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; AddJavaScript(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; javaScriptFile)
    {
        &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; scriptLocation = Page.ClientScript.GetWebResourceUrl(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.GetType(),javaScriptFile );
        Page.ClientScript.RegisterClientScriptInclude(javaScriptFile, scriptLocation);

    }&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;To embed Style Sheet &lt;/h3&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; background-color: rgb(244,244,244); text-align: left"&gt;
  &lt;pre id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; border-right-style: none; border-left-style: none; background-color: rgb(244,244,244); text-align: left; border-bottom-style: none"&gt;&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; AddStyleSheet()
     {
         &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; includeTemplate = &lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;&amp;lt;link rel='stylesheet' text='text/css' href='{0}' /&amp;gt;&amp;quot;&lt;/span&gt;;
         &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; includeLocation =
               Page.ClientScript.GetWebResourceUrl(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.GetType(), &lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.calendarview.css&amp;quot;&lt;/span&gt;);
         LiteralControl include = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; LiteralControl(String.Format(includeTemplate, includeLocation));
         Page.Header.Controls.Add(include);
     }&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;To embed Image &lt;/h3&gt;

&lt;p&gt;just add following line inside OnInit function&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; background-color: rgb(244,244,244); text-align: left"&gt;
  &lt;pre id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; border-right-style: none; border-left-style: none; background-color: rgb(244,244,244); text-align: left; border-bottom-style: none"&gt;_ImgDate.ImageUrl = Page.ClientScript.GetWebResourceUrl(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.GetType(), &lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.CalendarIcon.gif&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;add these functions indise OnInit function&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; background-color: rgb(244,244,244); text-align: left"&gt;
  &lt;pre id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;,courier,monospace; border-right-style: none; border-left-style: none; background-color: rgb(244,244,244); text-align: left; border-bottom-style: none"&gt;AddStyleSheet();
AddJavaScript(&lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.prototype.js&amp;quot;&lt;/span&gt;);
AddJavaScript(&lt;span style="color: rgb(0,96,128)"&gt;&amp;quot;DatePicker.Resources.calendarview.js&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;and that is our date picker control is ready to use you can drag and drop on any page and start using it.&lt;/p&gt;

&lt;p&gt;Your feedback is welcome.&lt;/p&gt;


&lt;h4&gt;&lt;a href="http://sites.google.com/site/aspdotnetpatterns/Home/DatePicker.zip?attredirects=0"&gt;download code here&lt;/a&gt;&lt;/h4&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-6075121761085844474?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/6075121761085844474/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/06/aspnet-date-picker-control-part-2.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/6075121761085844474'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/6075121761085844474'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/06/aspnet-date-picker-control-part-2.html' title='Asp.net date picker control part 2'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_dIl2j8iaPMg/Skjk8OIw17I/AAAAAAAABBY/BntyJmjycTM/s72-c/image_thumb.png?imgmax=800' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-2585502509956333512</id><published>2009-06-28T13:46:00.006+01:00</published><updated>2010-07-21T16:29:29.797+01:00</updated><title type='text'>The Scrum Management  ( A practical example )</title><content type='html'>&lt;p&gt;&lt;a href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=473787" rel="tag" style="display: none;"&gt;  codeproject  &lt;/a&gt;  &lt;/p&gt; &lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p align="justify"&gt;As a brief introduction, Scrum is an agile process for software development. With Scrum, projects progress via a series of iterations called sprints. Each sprint is typically 2-4 weeks long. Scrum is ideally suited for projects with rapidly changing or highly emergent requirements. As i have found it ideal for in-house development. Though its equally recommended for third party clients because of rapid change of requirments.
&lt;/p&gt;  &lt;h4&gt;Introduction to Terms&lt;/h4&gt;  &lt;table bordercolordark="#ffffff" bordercolorlight="#ffffff" width="100%" bgcolor="#ffffff" border="1" bordercolor="#ff8000"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;table bordercolordark="#ffffff" bordercolorlight="#ffffff" width="100%" bgcolor="#ffd280" border="1" bordercolor="#ff8000"&gt;&lt;tbody&gt;             &lt;tr&gt;               &lt;td width="77"&gt;&lt;strong&gt;Scrum team&lt;/strong&gt;&lt;/td&gt;                &lt;td width="425"&gt;                 &lt;p&gt;Scrum team  consists of product owner, scrum master, developers or anyone who is related to project.&lt;/p&gt;               &lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td width="77"&gt;&lt;strong&gt;Product Owner&lt;/strong&gt;&lt;/td&gt;                &lt;td width="425"&gt; In ideal situation a product owner is your stakeholder or  any representative from client. However as in our sprint planning  most often we don't find them present for meetings so thats  part goes to Project Manager for us. Again product owner could be anyone who is concerning to your project the most :)
&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td width="77"&gt;&lt;strong&gt;Scrum master&lt;/strong&gt;&lt;/td&gt;                &lt;td width="425"&gt;&lt;p&gt;Scrum master could be your team lead or again project manager if you luckily  find product stake holder resent in the meeting. Scrum master deals with  the   burn down charts, daily scrum meeting and sprint planning etc.
&lt;/p&gt;&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td width="77"&gt;&lt;strong&gt;Product backlog&lt;/strong&gt;&lt;/td&gt;                &lt;td width="425"&gt;The product backlog is a prioritized features list containing every desired feature or change to the product.&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td width="77"&gt;&lt;strong&gt;Sprint Planning&lt;/strong&gt;&lt;/td&gt;                &lt;td width="425"&gt;                 &lt;p&gt;Sprint planning is a meeting ideally not more than 4 hours. But i have seen some people take it to long and waste their whole day. In sprint plan developers, designers , scurm master , products owner or anyone who is concerning to project or to those task are present.&lt;/p&gt;                 &lt;strong&gt;&lt;/strong&gt;&lt;/td&gt;             &lt;/tr&gt;           &lt;/tbody&gt;&lt;/table&gt;       &lt;/td&gt;        &lt;td&gt;
&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;         &lt;h4&gt;A practical example  : How we do sprint planning ?&lt;/h4&gt;          &lt;p&gt;Lets suppose we have one Project Manager as a product owner ( In most cases  we don't find  stakeholders, customers  present all time ) then  a scrum master (can be your team lead) then We have a team of  2 developer, 1 designer and 1 database admin.&lt;/p&gt;          &lt;p&gt;Note i am using developer1 as a scrum master. as show in the figure below&lt;/p&gt;          &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/Skdl_8Ex6aI/AAAAAAAABA0/u97LjwIVse0/s1600-h/image%5B2%5D.png"&gt;&lt;img title="image" style="border: 0px none; display: block; float: none; margin-left: auto; margin-right: auto;" alt="image" src="http://lh4.ggpht.com/_dIl2j8iaPMg/SkdmAMFoTtI/AAAAAAAABA4/rYkBf9Zwmxw/image_thumb.png?imgmax=800" width="244" border="0" height="108" /&gt;&lt;/a&gt;&lt;/p&gt;          &lt;h4&gt;Story Points&lt;/h4&gt;          &lt;p&gt;Lets suppose we have discussed in our sprint plan total six tasks. A story point  can  also be represent as a day but a story point is actually amount of time you can put to work on any specific story or task.&lt;/p&gt;          &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/SkdmAXb_3CI/AAAAAAAABA8/BKxBHSmo-Ag/s1600-h/image%5B5%5D.png"&gt;&lt;img title="image" style="border: 0px none; display: block; float: none; margin-left: auto; margin-right: auto;" alt="image" src="http://lh5.ggpht.com/_dIl2j8iaPMg/SkdmA3MmVrI/AAAAAAAABBA/KcIEMjj2CBY/image_thumb%5B1%5D.png?imgmax=800" width="90" border="0" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;          &lt;p&gt;in other words we have total &lt;/p&gt;          &lt;p&gt;&lt;strong&gt;5 + 3  + 7+ 1+ 3 + 6 = 25 points&lt;/strong&gt;&lt;/p&gt;          &lt;h4&gt;Man days&lt;/h4&gt;          &lt;p&gt;You can always do from 2-4 week sprints , however i prefer 2 weeks of sprint planning.  imagine in 2 weeks of sprint plan each member has 14 man days. But we always ask in sprint  plan i any of our team member  has booked their annual leaves .Lets suppose devlioper1  has booked 3 annual leaves,developer2 booked 4 annual leaves, designer booked 2 annual leaves where database admin  no annual leaves booked. in this case we have &lt;/p&gt;          &lt;table bordercolordark="#ffffff" bordercolorlight="#ffffff" width="100%" bgcolor="#f3af49" border="1" bordercolor="#ff8000"&gt;&lt;tbody&gt;             &lt;tr&gt;               &lt;td&gt;&lt;strong&gt;Team member for 2 weeks sprint (14 days)&lt;/strong&gt;&lt;/td&gt;                &lt;td&gt;&lt;strong&gt;Man days &lt;/strong&gt;&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td&gt;Developer1  &lt;/td&gt;                &lt;td&gt;11&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td&gt;Developer2&lt;/td&gt;                &lt;td&gt;10&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td&gt;Designer&lt;/td&gt;                &lt;td&gt;12&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td&gt;Database Admin&lt;/td&gt;                &lt;td&gt;14&lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td&gt;Total Days&lt;/td&gt;                &lt;td&gt;47&lt;/td&gt;             &lt;/tr&gt;           &lt;/tbody&gt;&lt;/table&gt;          &lt;p&gt;&lt;strong&gt;In this we have total man days  =  51&lt;/strong&gt;&lt;/p&gt;          &lt;h4&gt;Focus factor&lt;/h4&gt;          &lt;p&gt;Now lets suppose we have focus factor is 50%&lt;/p&gt;          &lt;p&gt;Focus factor is basically the amount time of we can give to our stories (tasks) in our next 2 weeks of sprint. We use a "focus factor" to help account for all the interruptions we will invariably have. When we plan a sprint, we multiply the ideal man hours available in the sprint (40 hours * 2 weeks * x developers) by a percentage such as 0.8. That calculation tells us how many hours worth of product backlog items we can commit to for the sprint.&lt;/p&gt;          &lt;h4&gt;Estimated velocity&lt;/h4&gt;          &lt;h5&gt;( Available man days  * Focus factor  = Estimated velocity )&lt;/h5&gt;          &lt;p&gt;in our case we have &lt;/p&gt;          &lt;p&gt;47 * 0.50 (50%) = 23  story points&lt;/p&gt;          &lt;p&gt;So our estimated velocity for the upcoming sprint is 23 story points. that's means the team should add stories to the sprint until  it adds up to approximately 23. in this case we will remove our task 5 which can be less important and may not fit in this scrum and we will move it to our product backlog.&lt;/p&gt;          &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/SkdmBDuUfRI/AAAAAAAABBE/ZGFXL7yj4lA/s1600-h/image%5B9%5D.png"&gt;&lt;img title="image" style="border: 0px none; display: block; float: none; margin-left: auto; margin-right: auto;" alt="image" src="http://lh4.ggpht.com/_dIl2j8iaPMg/SkdmBt_6LJI/AAAAAAAABBI/c011Ngm0RqA/image_thumb%5B3%5D.png?imgmax=800" width="344" border="0" height="258" /&gt;&lt;/a&gt; &lt;/p&gt;          &lt;h5&gt;Scrum board&lt;/h5&gt;          &lt;p&gt;and that is how our scrum board will look like.&lt;/p&gt;          &lt;table width="396" border="1" cellpadding="2" cellspacing="0"&gt;&lt;tbody&gt;             &lt;tr&gt;               &lt;td valign="top" width="137"&gt;                 &lt;p align="center"&gt;&lt;b&gt;Not Checked Out&lt;/b&gt;&lt;/p&gt;                 &lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/SkdmBr8iOiI/AAAAAAAABBM/nVo1_yinzeg/s1600-h/image%5B12%5D.png"&gt;&lt;/a&gt;&lt;/td&gt;                &lt;td valign="top" width="130"&gt;                 &lt;p align="center"&gt;&lt;b&gt;Checked Out&lt;/b&gt;&lt;/p&gt;               &lt;/td&gt;                &lt;td valign="top" width="127"&gt;                 &lt;p align="center"&gt;&lt;b&gt;Done&lt;/b&gt;&lt;/p&gt;               &lt;/td&gt;             &lt;/tr&gt;              &lt;tr&gt;               &lt;td valign="top" width="141"&gt;&lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/SkdmBr8iOiI/AAAAAAAABBM/nVo1_yinzeg/s1600-h/image%5B12%5D.png"&gt;&lt;img title="image" style="border: 0px none; display: inline;" alt="image" src="http://lh3.ggpht.com/_dIl2j8iaPMg/SkdmCLJbnpI/AAAAAAAABBQ/8ZoflNP9478/image_thumb%5B4%5D.png?imgmax=800" width="104" border="0" height="244" /&gt;&lt;/a&gt;&lt;/td&gt;                &lt;td valign="top" width="129"&gt;
&lt;/td&gt;                &lt;td valign="top" width="127"&gt;
&lt;/td&gt;             &lt;/tr&gt;           &lt;/tbody&gt;&lt;/table&gt;          &lt;p&gt;i will update my post regarding to burn down chart and daily scrum meeting in my next post &lt;/p&gt;          &lt;h4&gt;References&lt;/h4&gt;          &lt;p&gt;• &lt;a href="http://agilemanifesto.org/"&gt;http://agilemanifesto.org/&lt;/a&gt;        
• &lt;a href="http://www.mountaingoatsoftware.com/scrum"&gt;http://www.mountaingoatsoftware.com/scrum&lt;/a&gt;        
• &lt;a href="http://www.xprogramming.com/xpmag/whatisxp.htm"&gt;http://www.xprogramming.com/xpmag/whatisxp.htm&lt;/a&gt;&lt;/p&gt;          &lt;p&gt; &lt;/p&gt;          &lt;p&gt; &lt;/p&gt;       &lt;/td&gt;        &lt;td&gt;
&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-2585502509956333512?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/2585502509956333512/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/06/scrum-development-process-how-do-we.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/2585502509956333512'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/2585502509956333512'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/06/scrum-development-process-how-do-we.html' title='The Scrum Management  ( A practical example )'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_dIl2j8iaPMg/SkdmAMFoTtI/AAAAAAAABA4/rYkBf9Zwmxw/s72-c/image_thumb.png?imgmax=800' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8323612215254179121.post-1425020509543669778</id><published>2009-06-26T13:04:00.002+01:00</published><updated>2009-07-02T19:57:27.248+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='bubble event'/><category scheme='http://www.blogger.com/atom/ns#' term='datepicker'/><category scheme='http://www.blogger.com/atom/ns#' term='commandname'/><category scheme='http://www.blogger.com/atom/ns#' term='repeater'/><title type='text'>How to: Create a Date Picker Composite Control in ASP.NET (C#)</title><content type='html'>&lt;p&gt;&lt;a style="display: none;" href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=473787" rel="tag"&gt;codeproject &lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;img src="http://docs.google.com/File?id=df3fw3bz_4czdr59gq_b" width="453" height="228" /&gt; &lt;a href="http://thelegend.8k.com/box_widget.html"&gt;download code here&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Introduction &lt;/h3&gt;  &lt;p align="justify"&gt;Composite controls are controls that combine multiple controls together to form a new reusable control. For example, a simple composite control could consist of both a &lt;a href="http://wiki.asp.net/page.aspx/320/label/"&gt;Label&lt;/a&gt; control and a TextBox control.&lt;a name="more"&gt; This example, however, puts one TextBox with an Image control together to allow a user to pick a date. &lt;/a&gt;The Calendar widget is a JavaScript-based calendar using prototype that can either be embedded within a page or popup when a trigger element is clicked. It is based very loosely on the &lt;a href="http://www.dynarch.com/projects/calendar/"&gt;Dynarch Calendar&lt;/a&gt;, only in the sense that it was used as a base, but has been almost entirely re-implemented. You can read more about this JavaScript-based calendar to here &lt;a href="http://calendarview.org/"&gt;http://calendarview.org/&lt;/a&gt; . What I have done here is used this JavaScript-based calendar inside a web composite control combined with a image in order to make it work like a true date picker control for asp.net. &lt;/p&gt;  &lt;h3&gt;Problem &lt;/h3&gt;  &lt;p&gt;it’s not very simple to use JavaScript control inside a repeater, datalist or gridview because repeater uses .net prefix with them. Because they don't expose commandName and commandArgument properties for calendar control.&lt;/p&gt;  &lt;h3&gt;Solution &lt;/h3&gt;  &lt;p&gt;I will extend this control with properties of CommandName and CommandArgument to use it easily inside data navigation controls.&lt;/p&gt;  &lt;p&gt;Here we go guys I have combined image control with the textbox control with JavaScript based control&lt;a href="http://calendarview.org/"&gt; http://calendarview.org/&lt;/a&gt; to make it work perfect for an asp.net date picker control. &lt;/p&gt;  &lt;h3&gt;How to Do it?. &lt;/h3&gt;  &lt;p&gt;it’s very simple and I will guide you step by step how to make a perfect asp.net date picker control. &lt;/p&gt;  &lt;h5&gt;Things you need?&lt;/h5&gt;  &lt;p&gt;&lt;a href="http://www.prototypejs.org/download"&gt;Prototype Library &lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://calendarview.org/"&gt;http://calendarview.org/&lt;/a&gt; (JavaScript and CSS files only.)&lt;/p&gt;  &lt;h5&gt;Here We Go&lt;/h5&gt;  &lt;p&gt;Using visual Studio 2008 click on file then New then choose Project. Under web menu choose ASP.NET Server Control and name the project DatePicker. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_dIl2j8iaPMg/SkSug0Dk6oI/AAAAAAAABAs/fFD8NlBR7bs/s1600-h/vsgui%5B4%5D.jpg"&gt;&lt;img title="vsgui" style="border-width: 0px; display: inline;" alt="vsgui" src="http://lh4.ggpht.com/_dIl2j8iaPMg/SkSuhXMylmI/AAAAAAAABAw/J1x-oFtRk_I/vsgui_thumb%5B2%5D.jpg?imgmax=800" width="471" border="0" height="295" /&gt;&lt;/a&gt; Now we will change this control inheritance form WebControl to a CompositeControl. We will rename the toolboxdata and also will give the name to control with a default prefix showing as below&lt;/p&gt;  &lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;   &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.ComponentModel;
&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Text;
&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Web;
&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Web.UI;
&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Web.UI.Web Controls;

[assembly: TagPrefix(&lt;span style="color: rgb(0, 96, 128);"&gt;"DatePicker"&lt;/span&gt;, &lt;span style="color: rgb(0, 96, 128);"&gt;"SQ"&lt;/span&gt;)]
&lt;span style="color: rgb(0, 0, 255);"&gt;namespace&lt;/span&gt; DatePicker
{
 [DefaultProperty(&lt;span style="color: rgb(0, 96, 128);"&gt;"Text"&lt;/span&gt;)]
 [ToolboxData(&lt;span style="color: rgb(0, 96, 128);"&gt;"&amp;lt;{0}:DatePicker runat=server&amp;gt;&amp;lt;/{0}:DatePicker&amp;gt;"&lt;/span&gt;)]
 &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; DatePicker : CompositeControl
 {&lt;/pre&gt;
&lt;/div&gt;
Note: Instead of asp tag prefix i have used SQ (Salman Qayyum :)Now we will add asp TextBox,Image and properties.

&lt;h3&gt;Properties&lt;/h3&gt;
&lt;b&gt;ImageUrl&lt;/b&gt; : URL for image. &lt;b&gt;AutoPostBack&lt;/b&gt;: To set AutoPostBack true or false. &lt;b&gt;Value&lt;/b&gt;: To access the value of date picker e.g datepicker. &lt;b&gt;ImageCssClass&lt;/b&gt;: To style the image. &lt;b&gt;TextBoxCssClass&lt;/b&gt;: To Style the textbox .&lt;span style="font-size:85%;"&gt;&lt;/span&gt;

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//To retrieve value i am using textbox&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; TextBox _TxtDate = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; TextBox();
     &lt;span style="color: rgb(0, 128, 0);"&gt;// Image to select the calendar date&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; Image _ImgDate = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Image();
     &lt;span style="color: rgb(0, 128, 0);"&gt;// Image URL to expose the image URL Property&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; _ImageUrl;
     &lt;span style="color: rgb(0, 128, 0);"&gt;// Exposing autopostback property &lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; _AutoPostBack;
     &lt;span style="color: rgb(0, 128, 0);"&gt;// property get the value from datepicker.&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; _Value;
     &lt;span style="color: rgb(0, 128, 0);"&gt;//CSS class to design the Image&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; _ImageCssClass;
     &lt;span style="color: rgb(0, 128, 0);"&gt;//CSS class to design the TextBox&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; _TextBoxCssClass;

     &lt;span style="color: rgb(0, 128, 0);"&gt;/**** properties***/&lt;/span&gt;

     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Appearance"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; ImageUrl
     {
         set
         {
             &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._ImageUrl = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }

     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Appearance"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;), Localizable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; Text
     {
         get
         {
             String s = (String)ViewState[&lt;span style="color: rgb(0, 96, 128);"&gt;"Text"&lt;/span&gt;];
             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; ((s == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) ? &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;.Empty : s);
         }

         set
         {
             ViewState[&lt;span style="color: rgb(0, 96, 128);"&gt;"Text"&lt;/span&gt;] = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }

     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Appearance"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;), Localizable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; Value
     {
         get
         {

             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _Value = _TxtDate.Text;
         }

         set
         {
             _Value = _TxtDate.Text = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }
     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Appearance"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;), Localizable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; AutoPostBack
     {
         get
         {
             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _AutoPostBack;
         }

         set
         {
             _AutoPostBack = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }
     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Appearance"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;), Localizable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; ImageCssClass
     {
         get
         {
             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _ImageCssClass;
         }

         set
         {
             _ImageCssClass = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }

     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Appearance"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;), Localizable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; TextBoxCssClass
     {
         get
         {
             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _TextBoxCssClass;
         }

         set
         {
             _TextBoxCssClass = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }

     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Custom"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;), Localizable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; CommandName
     {
         get
         {
             &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; s = ViewState[&lt;span style="color: rgb(0, 96, 128);"&gt;"CommandName"&lt;/span&gt;] &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;;
             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; s == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt; ? String.Empty : s;
         }
         set
         {
             ViewState[&lt;span style="color: rgb(0, 96, 128);"&gt;"CommandName"&lt;/span&gt;] = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }

     [Bindable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;), Category(&lt;span style="color: rgb(0, 96, 128);"&gt;"Custom"&lt;/span&gt;), DefaultValue(&lt;span style="color: rgb(0, 96, 128);"&gt;""&lt;/span&gt;), Localizable(&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;)]
     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; CommandArgument
     {
         get
         {
             &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; s = ViewState[&lt;span style="color: rgb(0, 96, 128);"&gt;"CommandArgument"&lt;/span&gt;] &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;;
             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; s == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt; ? String.Empty : s;
         }
         set
         {
             ViewState[&lt;span style="color: rgb(0, 96, 128);"&gt;"CommandArgument"&lt;/span&gt;] = &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;;
         }
     }&lt;/pre&gt;
&lt;/div&gt;
&lt;strong&gt;&lt;/strong&gt;Above I have also added CommandName and CommandArgument property to make this date picker work inside data navigation controls.

&lt;h4&gt;&lt;/h4&gt;
Generating Bubble Event

&lt;p align="justify"&gt;Event bubbling enables events to be raised from a more convenient location in the controls hierarchy and allows event handlers to be attached to the original control as well as to the control that exposes the bubbled event. Event bubbling is used by the data-bound controls to expose command events raised by child controls (within item templates) as top-level events. While ASP.NET server controls in the .NET Framework use event bubbling for command events (events whose event data class derives from CommandEventArgs), any event defined on a server control can be bubbled. &lt;/p&gt;

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; EventCommandObj = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;();

     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;event&lt;/span&gt; CommandEventHandler Command
     {
         add
         {
             Events.AddHandler(EventCommandObj, &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;);
         }
         remove
         {
             Events.RemoveHandler(EventCommandObj, &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;);
         }
     }
     &lt;span style="color: rgb(0, 128, 0);"&gt;//this will raise the bubble event&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;virtual&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; OnCommand(CommandEventArgs commandEventArgs)
     {
         CommandEventHandler eventHandler = (CommandEventHandler)Events[EventCommandObj];
         &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (eventHandler != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)
         {
             eventHandler(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;, commandEventArgs);
         }
         &lt;span style="color: rgb(0, 0, 255);"&gt;base&lt;/span&gt;.RaiseBubbleEvent(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;, commandEventArgs);
     }
     &lt;span style="color: rgb(0, 128, 0);"&gt;//this will be initialized to  OnTextChanged event on the normal textbox&lt;/span&gt;
     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; OnTextChanged(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, EventArgs e)
     {
         &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.AutoPostBack)
         {
             &lt;span style="color: rgb(0, 128, 0);"&gt;//pass the event arguments to the OnCommand event to bubble up&lt;/span&gt;
             CommandEventArgs args = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; CommandEventArgs(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.CommandName, &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.CommandArgument);
             OnCommand(args);
         }
     }&lt;/pre&gt;
&lt;/div&gt;
&lt;b&gt;We will raise this bubble event with texbox.OnTextChanged using OnInit function as below.&lt;/b&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Always create dynamic controls inside OnInit function because viewstate reloads after OnInit and before the Load event. So if you want to keep the viewstate on postback then the best place to keep the code is inside OnInit&lt;/span&gt; &lt;/p&gt;

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; OnInit(EventArgs e)
     {
         &lt;span style="color: rgb(0, 128, 0);"&gt;//AddStyleSheet();&lt;/span&gt;
         &lt;span style="color: rgb(0, 128, 0);"&gt;//AddJavaScript();&lt;/span&gt;
         &lt;span style="color: rgb(0, 0, 255);"&gt;base&lt;/span&gt;.OnInit(e);

         &lt;span style="color: rgb(0, 128, 0);"&gt;// For TextBox&lt;/span&gt;
         &lt;span style="color: rgb(0, 128, 0);"&gt;// setting name for textbox. using t just to concat with this.ID for unqiueName&lt;/span&gt;
         _TxtDate.ID = &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.ID + &lt;span style="color: rgb(0, 96, 128);"&gt;"t"&lt;/span&gt;;
         &lt;span style="color: rgb(0, 128, 0);"&gt;// setting postback&lt;/span&gt;
         _TxtDate.AutoPostBack = &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.AutoPostBack;
         &lt;span style="color: rgb(0, 128, 0);"&gt;// giving the textbox default value for date&lt;/span&gt;
         _TxtDate.Text = &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Value;
         &lt;span style="color: rgb(0, 128, 0);"&gt;//Initializing the TextChanged with our custom event to raise bubble event&lt;/span&gt;
         _TxtDate.TextChanged += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; System.EventHandler(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.OnTextChanged);
         &lt;span style="color: rgb(0, 128, 0);"&gt;//Setting textbox to readonly to make sure user dont play with the textbox&lt;/span&gt;
         _TxtDate.Attributes.Add(&lt;span style="color: rgb(0, 96, 128);"&gt;"readonly"&lt;/span&gt;, &lt;span style="color: rgb(0, 96, 128);"&gt;"readonly"&lt;/span&gt;);
         &lt;span style="color: rgb(0, 128, 0);"&gt;// adding stylesheet &lt;/span&gt;
         _TxtDate.Attributes.Add(&lt;span style="color: rgb(0, 96, 128);"&gt;"class"&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.TextBoxCssClass);

         &lt;span style="color: rgb(0, 128, 0);"&gt;// For Image&lt;/span&gt;
         &lt;span style="color: rgb(0, 128, 0);"&gt;// setting alternative name for image&lt;/span&gt;
         _ImgDate.AlternateText = &lt;span style="color: rgb(0, 96, 128);"&gt;"imageURL"&lt;/span&gt;;
         &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (!&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;.IsNullOrEmpty(_ImageUrl))
             _ImgDate.ImageUrl = _ImageUrl;
      
         &lt;span style="color: rgb(0, 128, 0);"&gt;//setting name for image&lt;/span&gt;
         _ImgDate.ID = &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.ID + &lt;span style="color: rgb(0, 96, 128);"&gt;"i"&lt;/span&gt;;
         &lt;span style="color: rgb(0, 128, 0);"&gt;//setting image class for textbox&lt;/span&gt;
         _ImgDate.Attributes.Add(&lt;span style="color: rgb(0, 96, 128);"&gt;"class"&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.ImageCssClass);
     }&lt;/pre&gt;
&lt;/div&gt;
&lt;b&gt;Now we will add and render controls &lt;/b&gt;

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: rgb(0, 128, 0);"&gt;/// adding child controls to composite control&lt;/span&gt;
    &lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; CreateChildControls()
    {
        &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Controls.Add(_TxtDate);
        &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Controls.Add(_ImgDate);
        &lt;span style="color: rgb(0, 0, 255);"&gt;base&lt;/span&gt;.CreateChildControls();
    }

    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; RenderControl(HtmlTextWriter writer)
    {
        &lt;span style="color: rgb(0, 128, 0);"&gt;// render textbox and image&lt;/span&gt;
        _TxtDate.RenderControl(writer);
        _ImgDate.RenderControl(writer);
        RenderContents(writer);
    }&lt;/pre&gt;
&lt;/div&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;b&gt;Replace or add your RenderContents function with&lt;/b&gt;

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: rgb(0, 128, 0);"&gt;/// Adding the javascript to render the content &lt;/span&gt;
    &lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;param name="output"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; RenderContents(HtmlTextWriter output)
    {
        StringBuilder calnder = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; StringBuilder();
        &lt;span style="color: rgb(0, 128, 0);"&gt;//adding javascript first&lt;/span&gt;
        calnder.AppendFormat(&lt;span style="color: rgb(0, 96, 128);"&gt;@"&amp;lt;script type='text/javascript'&amp;gt;
                                 document.observe('dom:loaded', function() {{
                                    Calendar.setup({{
                                    dateField: '{0}',
                                    triggerElement: '{1}',
                                    dateFormat: '%d/%m/%Y'
                                 }})
                                }});
                          "&lt;/span&gt;, _TxtDate.ClientID, _ImgDate.ClientID);
        calnder.Append(&lt;span style="color: rgb(0, 96, 128);"&gt;"&amp;lt;/script&amp;gt;"&lt;/span&gt;);
        output.Write(calnder.ToString());
    }&lt;/pre&gt;
&lt;/div&gt;
Above code is JavaScript code to make the calendar control to work using prototype. For more help kindly visit &lt;a href="http://calendarview.org/"&gt;http://calendarview.org/&lt;/a&gt;.

&lt;h5&gt;We are almost there now&lt;/h5&gt;

&lt;h5 style="font-weight: normal;"&gt;Now to test this file chose solution file add new website and name it DatePickerTest. Click on view from top menu and chose Toolbox if it’s not already opened. on toolbox click right button of mouse and chose items then click browse and navigate to datepicker project under bin directory add DatePicker.dll and click OK. DatePicker has seen added to your design view. &lt;/h5&gt;

&lt;p&gt;Drag and drop and on your page and start using it :) &lt;/p&gt;

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&amp;lt;SQ:DatePicker ID=&lt;span style="color: rgb(0, 96, 128);"&gt;"DatePicker1"&lt;/span&gt; runat=&lt;span style="color: rgb(0, 96, 128);"&gt;"server"&lt;/span&gt;  ImageUrl=&lt;span style="color: rgb(0, 96, 128);"&gt;"Javascript/CalendarIcon.gif"&lt;/span&gt; /&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
Adding JavaScript

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&amp;lt;head id=&lt;span style="color: rgb(0, 96, 128);"&gt;"Head1"&lt;/span&gt; runat=&lt;span style="color: rgb(0, 96, 128);"&gt;"server"&lt;/span&gt;&amp;gt;

 &amp;lt;script src=&lt;span style="color: rgb(0, 96, 128);"&gt;"Javascript/prototype.js"&lt;/span&gt; type=&lt;span style="color: rgb(0, 96, 128);"&gt;"text/javascript"&lt;/span&gt;&amp;gt;&amp;lt;/script&amp;gt;
 &amp;lt;script src=&lt;span style="color: rgb(0, 96, 128);"&gt;"Javascript/calendarview.js"&lt;/span&gt; type=&lt;span style="color: rgb(0, 96, 128);"&gt;"text/javascript"&lt;/span&gt;&amp;gt;&amp;lt;/script&amp;gt;
 &amp;lt;link href=&lt;span style="color: rgb(0, 96, 128);"&gt;"Javascript/calendarview.css"&lt;/span&gt; rel=&lt;span style="color: rgb(0, 96, 128);"&gt;"stylesheet"&lt;/span&gt; type=&lt;span style="color: rgb(0, 96, 128);"&gt;"text/css"&lt;/span&gt; /&amp;gt;
 &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
To retrieve value useResponse.Write(DatePicker1.Value);

&lt;h5&gt;Inside repeater &lt;/h5&gt;

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&amp;lt;asp:Repeater ID=&lt;span style="color: rgb(0, 96, 128);"&gt;"Repeater1"&lt;/span&gt; runat=&lt;span style="color: rgb(0, 96, 128);"&gt;"server"&lt;/span&gt;  DataSourceID=&lt;span style="color: rgb(0, 96, 128);"&gt;"SqlDataSource1"&lt;/span&gt;
    onitemcommand=&lt;span style="color: rgb(0, 96, 128);"&gt;"Repeater1_ItemCommand"&lt;/span&gt;&amp;gt;
    &amp;lt;ItemTemplate&amp;gt;
    &amp;lt;SQ:DatePicker ID=&lt;span style="color: rgb(0, 96, 128);"&gt;"DatePicker2"&lt;/span&gt; runat=&lt;span style="color: rgb(0, 96, 128);"&gt;"server"&lt;/span&gt; CommandName=&lt;span style="color: rgb(0, 96, 128);"&gt;"Clicked"&lt;/span&gt; AutoPostBack=&lt;span style="color: rgb(0, 96, 128);"&gt;"true"&lt;/span&gt; ImageUrl=&lt;span style="color: rgb(0, 96, 128);"&gt;"Javascript/CalendarIcon.gif"&lt;/span&gt;  /&amp;gt;
     &amp;lt;%# Eval(&lt;span style="color: rgb(0, 96, 128);"&gt;"ProductName"&lt;/span&gt;)%&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;/ItemTemplate&amp;gt;
&amp;lt;/asp:Repeater&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
Using ItemCommand event

&lt;div id="codeSnippetWrapper"   style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;
 &lt;pre id="codeSnippet"   style="border-style: none; margin: 0em; padding: 0px; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; background-color: rgb(244, 244, 244); text-align: left;font-family:'Courier New',courier,monospace;font-size:8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Repeater1_ItemCommand(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; source, RepeaterCommandEventArgs e)
 {
     DatePicker.DatePicker dtp = (DatePicker.DatePicker)e.Item.FindControl(&lt;span style="color: rgb(0, 96, 128);"&gt;"DatePicker2"&lt;/span&gt;);
     &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (e.CommandName == &lt;span style="color: rgb(0, 96, 128);"&gt;"Clicked"&lt;/span&gt;)
     {
         Response.Write(dtp.Value);
     }
 }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Don’t forget to see my next posting in weeks time how to work with Scrum Management and DSDM (Dynamic Systems Development Method) and how to implement them in real time scenarios with development.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your feedback is welcome.&lt;/p&gt;

&lt;p&gt;Don't forget to see its updated version on my blog  &lt;a href="http://aspdotnetpatterns.blogspot.com/2009/06/aspnet-date-picker-control-part-2.html"&gt;Asp.net date picker control part 2&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8323612215254179121-1425020509543669778?l=aspdotnetpatterns.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aspdotnetpatterns.blogspot.com/feeds/1425020509543669778/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/06/how-to-create-date-picker-composite.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/1425020509543669778'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8323612215254179121/posts/default/1425020509543669778'/><link rel='alternate' type='text/html' href='http://aspdotnetpatterns.blogspot.com/2009/06/how-to-create-date-picker-composite.html' title='How to: Create a Date Picker Composite Control in ASP.NET (C#)'/><author><name>Salman</name><uri>http://www.blogger.com/profile/10858307446868961853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_dIl2j8iaPMg/SkSuhXMylmI/AAAAAAAABAw/J1x-oFtRk_I/s72-c/vsgui_thumb%5B2%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
