<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>It's about programming</title>
	<atom:link href="http://lakmalk.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://lakmalk.wordpress.com</link>
	<description>Coolest Development</description>
	<lastBuildDate>Mon, 03 Nov 2008 05:49:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='lakmalk.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>It's about programming</title>
		<link>http://lakmalk.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://lakmalk.wordpress.com/osd.xml" title="It&#039;s about programming" />
	<atom:link rel='hub' href='http://lakmalk.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Strategy Pattern</title>
		<link>http://lakmalk.wordpress.com/2008/11/02/strategy-pattern/</link>
		<comments>http://lakmalk.wordpress.com/2008/11/02/strategy-pattern/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 13:09:08 +0000</pubDate>
		<dc:creator>Lakmal</dc:creator>
				<category><![CDATA[Software Design and Documenting]]></category>

		<guid isPermaLink="false">http://lakmalk.wordpress.com/2008/11/02/strategy-pattern/</guid>
		<description><![CDATA[Strategy Pattern (also known as the policy pattern), Simple and useful, it&#8217;s a very simple pattern I went through. Even though it&#8217;s simple it is gaining great weight to best coding practices. Official Definition &#8220;Define Family of Algorithms, encapsulate each one, and make them interchangeable. Strategy let&#8217;s algorithm vary independently from client that use it&#8221;. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lakmalk.wordpress.com&amp;blog=525511&amp;post=135&amp;subd=lakmalk&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Strategy Pattern (also known as the <strong>policy pattern)</strong>, Simple and useful, it&#8217;s a very simple pattern I went through. Even though it&#8217;s simple it is gaining great weight to best coding practices.
</p>
<p><strong>Official Definition<br />
</strong></p>
<p>&#8220;Define Family of Algorithms, encapsulate each one, and make them interchangeable. Strategy let&#8217;s algorithm vary independently from client that use it&#8221;.
</p>
<p><strong>Why we need pattern like this<br />
</strong></p>
<p>If you see how horrible lengthy if else statements or switch statements, that is one of the reason where strategy pattern come to the screen. Normally a program is such a sequence of Logical Operations; here operation itself might be simple algorithm. In Object Oriented Programming, we have classes and inside a class we are having methods where we writing particular sequence of operations to achieve some business aspect.  In business flow, to take the decisions to getting invoke some operations in execution sequence, are doing at run time according current state. So there you need If-else statements or switch statement to manage that.
</p>
<p><strong>Every programmer should dream to write their code as a story, Code reviewers would love to give best comments on that definitely.<br />
</strong></p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat12.png">
	</p>
<p>This is such simple story which might love to read by especially maintenance people, they won&#8217;t be having any harm to understand this. Business process too explaining in method itself, no one need to go back and see use cases and all when maintenance are doing on the code. In Such a business workflow, we might be having more than this if-else statements or lengthy switch statement. Yes, suppose we wrote the logic as nice readable story, but still it&#8217;s having too many lengthy logical statements.
</p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat22.png">
	</p>
<p>Here is Context class where actually Calculation business aspect implementations is been used.
</p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat32.png">
	</p>
<p>While having such switch statements in the business logic implementations, you are taking risk of frequently changing when new business aspect is asked to be added to the flow. Suppose in this case, someone is asking to added another Calculation Operation into the business logic and then you need to do two things primarily
</p>
<ol style="margin-left:54pt;">
<li>Add new Item into the <strong>enum</strong>
		</li>
<li>Add new <strong>case</strong><br />
			<strong>statement</strong> into the switch statement
</li>
</ol>
<p>With this example it seems no any harm to do that, but when business scenarios getting large, you might be having bit complex business flows. Same thing can occur in lengthy if-else statements too. This is the place where Strategy Pattern comes to the screen. Basically it will allow you to add new business aspect to the business workflow or change existing business aspect in flow, without touching existing business workflow. With that you won&#8217;t need to add new item to <strong>emun</strong> and add new <strong>case statement</strong> to the switch.
</p>
<p><strong>Strategy Pattern<br />
</strong></p>
<p>If you have written the business logic as a good readable story which itself explain the Algorithm associated with it clearly, and then it is the time to think about Strategy Pattern to enhance code design to support future changes. Identify the Algorithm is extremely vital with this pattern and extract Algorithm signature into an Interface is the second step. Then Client will only couple with Algorithm Interface and burry implementation details into derived classes. Then Client won&#8217;t be having any harm on adding new derived classes to extend the business and or change derived classes on requirement changes. Here is the check list you might help to identify and do stuff.
</p>
<ol style="margin-left:54pt;">
<li>Identify an Algorithm that client need to access.
</li>
<li>Code signature for that algorithm in an Interface.
</li>
<li>Burry alternative implementation details in derived classes.
</li>
<li>Clients of the algorithm couple themselves to the Interface
</li>
</ol>
<p>Calculation Interface
</p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat42.png">
	</p>
<p>Just interface Operation need to be done to achieve particular business aspect. Particular calculation should implement ICalculation interface and implement Calculate Method
</p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat52.png">
	</p>
<p>By implementing ICalculation Interface, implicitly we implement calculate method and it&#8217;s subject to that particular Calculation. Here it&#8217;s Add two values and return result back.  Then Calculation class would look like this with new method
</p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat62.png">
	</p>
<p>And Context class (Calculation User class) is going to be like this
</p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat72.png">
	</p>
<p>Implementation is looking great after new method than earlier. But advantage is not only that, suppose you need to Use another Calculation in the context. Then
</p>
<ol style="margin-left:54pt;">
<li>You don&#8217;t need to touch Calculation Business workflow implementations. It means don&#8217;t need to changes Calculation class or ICalculation Interface.
</li>
<li>Just need add another Calculation Operation class like &#8220;Modulation&#8221; while implementing it ICalculation interface.
</li>
<li>In the &#8220;UseCalculation&#8221; Method, you can use instance of newly created class to achieve new Calculation Operation in existing system without changing main business workflow.
</li>
</ol>
<p>Found good Diagrams explaining concept well
</p>
<p><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat82.gif"><img src="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat92.gif">
	</p>
<p>Here we can remind one another such important concepts in design pattern like &#8220;Program to interface and not to an implementation&#8221; and &#8220;Open for extension and close for modification&#8221;. Implicitly it has been followed by us on this. Here we concern only business logic implementation and not on Context implementation where Business Logics are been used. Our main concern should be on securing business logic from ambiguous implementations.
</p>
<p><strong>Conclusion<br />
</strong></p>
<p>Strategy Pattern might have been followed by us without knowing even it&#8217;s the Strategy Pattern. This posting was about mentioning things bit clearly.
</p>
<p>Resources
</p>
<p>Head First Design Patterns
</p>
<p><a href="http://en.wikipedia.org/wiki/Strategy_pattern">http://en.wikipedia.org/wiki/Strategy_pattern</a>
	</p>
<p><a href="http://jeremyjarrell.com/archive/2007/10/28/64.aspx">http://jeremyjarrell.com/archive/2007/10/28/64.aspx</a>
	</p>
<p><a href="http://www.dofactory.com/Patterns/PatternStrategy.aspX">http://www.dofactory.com/Patterns/PatternStrategy.aspX</a>
	</p>
<p><a href="http://www.developer.com/tech/article.php/3683091">http://www.developer.com/tech/article.php/3683091</a>
	</p>
<p><a href="http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx">http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx</a>
	</p>
<p><a href="http://www.primaryobjects.com/CMS/Article70.aspx">http://www.primaryobjects.com/CMS/Article70.aspx</a>
	</p>
<p><a href="http://blog.cumps.be/design-patterns-strategy-pattern/">http://blog.cumps.be/design-patterns-strategy-pattern/</a>
	</p>
<p><a href="http://sourcemaking.com/design_patterns/strategy">http://sourcemaking.com/design_patterns/strategy</a>
	</p>
<p>
 </p>
<p>Regards
</p>
<p>Lakmal Kankanamge
</p>
<p>
 </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lakmalk.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lakmalk.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lakmalk.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lakmalk.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lakmalk.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lakmalk.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lakmalk.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lakmalk.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lakmalk.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lakmalk.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lakmalk.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lakmalk.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lakmalk.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lakmalk.wordpress.com/135/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lakmalk.wordpress.com&amp;blog=525511&amp;post=135&amp;subd=lakmalk&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lakmalk.wordpress.com/2008/11/02/strategy-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/de1233b9f7d3ab97c726ff774924f00b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Lakmal</media:title>
		</media:content>

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat12.png" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat22.png" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat32.png" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat42.png" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat52.png" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat62.png" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat72.png" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat82.gif" medium="image" />

		<media:content url="http://lakmalk.files.wordpress.com/2008/11/110208-1310-strategypat92.gif" medium="image" />
	</item>
		<item>
		<title>Encapsulate what varies from what stay same……</title>
		<link>http://lakmalk.wordpress.com/2008/10/12/encapsulate-what-varies-from-what-stay-same%e2%80%a6%e2%80%a6/</link>
		<comments>http://lakmalk.wordpress.com/2008/10/12/encapsulate-what-varies-from-what-stay-same%e2%80%a6%e2%80%a6/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 09:16:39 +0000</pubDate>
		<dc:creator>Lakmal</dc:creator>
				<category><![CDATA[Software Design and Documenting]]></category>

		<guid isPermaLink="false">http://lakmalk.wordpress.com/2008/10/12/encapsulate-what-varies-from-what-stay-same%e2%80%a6%e2%80%a6/</guid>
		<description><![CDATA[Encapsulating is all about how you arrange you implementations throughout classes and methods in a particular system implementation. And further on this, It&#8217; about How you arrange your class structure Deciding your class structure and which responsibilities are going to assign each other class How you control access to each and every classes, methods, properties [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lakmalk.wordpress.com&amp;blog=525511&amp;post=122&amp;subd=lakmalk&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Encapsulating is all about how you arrange you implementations throughout classes and methods in a particular system implementation. And further on this, It&#8217; about
</p>
<ul style="margin-left:54pt;">
<li>How you arrange your class structure
</li>
<li>Deciding your class structure and which responsibilities are going to assign each other class
</li>
<li>How you control access to each and every classes, methods, properties and variables
</li>
<li>And etc..
</li>
</ul>
<p><strong>Design Principle<br />
</strong></p>
<p>&#8220;Identify the aspect of your application that vary and separate then from what stays same.&#8221;
</p>
<p>It&#8217;s like this, suppose some aspect of you application is changing with new requirements, then it&#8217;s obvious you got behavior that need be pulled out and separated from all the stuff that doesn&#8217;t change. Other way round it&#8217;s like, take parts that vary and encapsulate them, so that later you can extend or alter the parts that vary without affecting that don&#8217;t.
</p>
<p>Almost all the Design patterns are coming save this concept. All patterns provide a way to let &#8220;some part of a system vary independently of all other parts&#8221;.
</p>
<p>
 </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lakmalk.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lakmalk.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lakmalk.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lakmalk.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lakmalk.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lakmalk.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lakmalk.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lakmalk.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lakmalk.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lakmalk.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lakmalk.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lakmalk.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lakmalk.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lakmalk.wordpress.com/122/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lakmalk.wordpress.com&amp;blog=525511&amp;post=122&amp;subd=lakmalk&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lakmalk.wordpress.com/2008/10/12/encapsulate-what-varies-from-what-stay-same%e2%80%a6%e2%80%a6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/de1233b9f7d3ab97c726ff774924f00b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Lakmal</media:title>
		</media:content>
	</item>
	</channel>
</rss>
