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

<channel>
	<title>Pragmatic Creativity &#187; Design patterns</title>
	<atom:link href="http://www.vlad-yatsenko.eu/category/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vlad-yatsenko.eu</link>
	<description>Software Systems Design and Development in Detail</description>
	<lastBuildDate>Sat, 04 Feb 2012 17:18:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<div id='fb-root'></div>
					<script type='text/javascript'>
						window.fbAsyncInit = function()
						{
							FB.init({appId: null, status: true, cookie: true, xfbml: true});
						};
						(function()
						{
							var e = document.createElement('script'); e.async = true;
							e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
							document.getElementById('fb-root').appendChild(e);
						}());
					</script>	
						<item>
		<title>Optimistic locking vs Pessimistic locking explained concisely</title>
		<link>http://www.vlad-yatsenko.eu/2011/07/optimistic-locking-vs-pessimistic-locking-explained-concisely/</link>
		<comments>http://www.vlad-yatsenko.eu/2011/07/optimistic-locking-vs-pessimistic-locking-explained-concisely/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 14:38:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design patterns]]></category>
		<category><![CDATA[optomistic locking]]></category>
		<category><![CDATA[pessimistic locking]]></category>

		<guid isPermaLink="false">http://www.vlad-yatsenko.eu/?p=381</guid>
		<description><![CDATA[I&#8217;ve found this funny and interesting explanation of Optimistic vs Pessimistic locking through the analogy with the toilet door and could not stand not to share Optimistic locking is like leaving the door to the toilet open: you have a number of toilets, select one, and you are optimistic that nobody will come to use [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found this funny and interesting explanation of Optimistic vs Pessimistic locking through the analogy with the toilet door and could not stand not to share <img src='http://www.vlad-yatsenko.eu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<blockquote><p><strong>Optimistic locking</strong> is like leaving the door to the toilet open: you have a number of toilets, select one, and you are optimistic that nobody will come to use the same toilet, too. If someome comes, it will of course be embarassing, an exception is raised, and he must abort his try, but you hope that these conflicts are the execption.</p>
<p><strong>Pessimistic locking</strong> is like always locking the door to the toilet: although there are a number of different toilets, you are expecting every time that somebody else will come to use one, too, and that the only safe option is to lock all toilet doors (which corresponds to page locking). If someone comes and wants to use a toilet, he cannot enter and must wait before the locked door.</p></blockquote>
<p>Found at <a title="Optimistic vs. Pessimistic Locking" href="http://4loc.wordpress.com/2009/04/25/optimistic-vs-pessimistic-locking/">Optimistic vs. Pessimistic Locking</a>.</p>
<div style="float: right; margin-left: 10px;"><a href="http://twitter.com/share?url=http://www.vlad-yatsenko.eu/2011/07/optimistic-locking-vs-pessimistic-locking-explained-concisely/&via=vyatsenko&text=Optimistic locking vs Pessimistic locking explained concisely&related=:&lang=en&count=horizontal" class="twitter-share-button">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style="display:none;"><a href="http://government-politics.forum1000.com">government,politics</a>&nbsp;<a href="http://news365live.com">news,politics</a>&nbsp;<a href="http://worldnews365online.com">news,politics</a></div><div class='wpfblike' style='height: 40px;'><fb:like href='http://www.vlad-yatsenko.eu/2011/07/optimistic-locking-vs-pessimistic-locking-explained-concisely/' layout='button_count' show_faces='false' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.vlad-yatsenko.eu/2011/07/optimistic-locking-vs-pessimistic-locking-explained-concisely/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Matcher &#8211; a design pattern candidate</title>
		<link>http://www.vlad-yatsenko.eu/2009/11/matcher-a-design-pattern-candidate/</link>
		<comments>http://www.vlad-yatsenko.eu/2009/11/matcher-a-design-pattern-candidate/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 17:06:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Matcher]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.vlad-yatsenko.eu/?p=46</guid>
		<description><![CDATA[Simple and clear code &#8211; what does it look like? Basically, besides comprehensible clear naming we expect it to be highly granular and to have as little as possible branching. Indeed, such code is easy to read, to understand its purpose and required test coverage is optimal. Well, clear naming has nothing special to talk [...]]]></description>
			<content:encoded><![CDATA[<p><em>Simple and clear code</em> &#8211; what does it look like? Basically, besides comprehensible clear naming we expect it to be <strong>highly granular</strong> and to have as little as possible <strong>branching</strong>. Indeed, such code is easy to read, to understand its purpose and required test coverage is optimal.</p>
<p>Well, clear naming has nothing special to talk about. High granularity is all about code decomposition (<a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">The Single Responsibility</a> principle can also be applied here).</p>
<p>One of frequent reasons for the code branching is a ramification of logic depending on some object&#8217;s internal state (normally, it is a domain class object). Such branching can be &#8220;switch&#8221;-style &#8211; when you compare a particular state variable with its possible values. However, a more common and basic is the binary branching, which operates on boolean conditions. E.g., <em>if object is in this state then do this else do that</em>.</p>
<p>Often there is a need to repeat such logic in many places throughout the application code. Additionally, such logic may happen to be more intricate than one conditional <em>if</em> statement. To simplify it one may define appropriate <em>isXXX()</em> method(s) to the examined object&#8217;s class. But what if the object state is fairly complex or domain class is not aware of its possible states, or the logic examining its state is not limited to several cases, but more cases can be developed in future. Or even, you can be trapped in a situation, when you are not allowed to change the domain object class.</p>
<p>So here is an elegant solution&#8230; it&#8217;s the <strong>Matcher pattern</strong>! This is a pretty simple design pattern that consists of three classes in general case. The pattern&#8217;s class diagram is following:</p>
<p><center><img src="http://www.vlad-yatsenko.eu/wp-content/uploads/2009/11/matcher_pattern.jpg" alt="Matcher UML Diagram" title="Matcher UML Diagram" width="352" height="196" class="size-full wp-image-57" /></center></p>
<p>There are the Entity class which is the specific domain class, the Matcher which is an abstract class or interface with a single abstract method that represents the matching algorithm, and the ConcreteMatcher which is a specific implementation of Matcher.</p>
<p>When implementing the pattern, Java generics can be leveraged to provide more flexibility with less code.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">interface</span> Matcher <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">boolean</span> matches<span class="br0">&#40;</span>T object<span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
&nbsp;<br />
<span class="kw1">public</span> <span class="kw1">class</span> EuropeanCountryMatcher <span class="kw1">implements</span> Matcher<span class="sy0">&lt;</span>Country<span class="sy0">&gt;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// the list of EU countries &#8211; can be injected</span><br />
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Set</span></a> countryCodes<span class="sy0">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span class="co1">// Checks if the given country is an EU country</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">boolean</span> matches<span class="br0">&#40;</span>Country country<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>countryCodes.<span class="me1">contains</span><span class="br0">&#40;</span>country.<span class="me1">getCode</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>What is even more beautiful about the Matcher is that it can be injected into classes that rely on its logic. E.g.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> DeliveryStrategy <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">private</span> Matcher<span class="sy0">&lt;</span>Country<span class="sy0">&gt;</span> specialPartnerCountryMatcher<span class="sy0">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setSpecialPartnerCountryMatcher<span class="br0">&#40;</span>Matcher<span class="sy0">&lt;</span>Country<span class="sy0">&gt;</span> matcher<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; specialPatnerCountryMatcher <span class="sy0">=</span> matcher<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span class="kw1">public</span> Priority getDeliveryPriorityForClient<span class="br0">&#40;</span>Customer customer<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>specialPatnerCountryMatcher.<span class="me1">matches</span><span class="br0">&#40;</span>customer.<span class="me1">getAddress</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getCountry</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Priority.<span class="me1">HIGHEST</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Priority.<span class="me1">STANDARD</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Besides logic reuse and simplification, another nice use of Matcher is the collection filtering.</p>
<p>I believe, similar &#8220;Matcher&#8221; approaches have been used in many projects independently. One described here  can have pretty big number of various applications, so is worthy of being stated as a design pattern.</p>
<div style="float: right; margin-left: 10px;"><a href="http://twitter.com/share?url=http://www.vlad-yatsenko.eu/2009/11/matcher-a-design-pattern-candidate/&via=vyatsenko&text=Matcher - a design pattern candidate&related=:&lang=en&count=horizontal" class="twitter-share-button">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style="display:none;"><a href="http://government-politics.forum1000.com">government,politics</a>&nbsp;<a href="http://news365live.com">news,politics</a>&nbsp;<a href="http://worldnews365online.com">news,politics</a></div><div class='wpfblike' style='height: 40px;'><fb:like href='http://www.vlad-yatsenko.eu/2009/11/matcher-a-design-pattern-candidate/' layout='button_count' show_faces='false' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.vlad-yatsenko.eu/2009/11/matcher-a-design-pattern-candidate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Thinking of Patterns</title>
		<link>http://www.vlad-yatsenko.eu/2009/11/thinking-of-patterns/</link>
		<comments>http://www.vlad-yatsenko.eu/2009/11/thinking-of-patterns/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 21:18:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design patterns]]></category>
		<category><![CDATA[SDLC]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://www.vlad-yatsenko.eu/?p=20</guid>
		<description><![CDATA[Design Patterns are a great piece of knowledge, that gives a possibility to learn how to create a good design instead of researching it. From the other side, agile development practices is another mainstream that for ones means flexible though stable organizational methodology and for others a way to save some money by reducing time [...]]]></description>
			<content:encoded><![CDATA[<p>Design Patterns are a great piece of knowledge, that gives a possibility to learn how to create a good design instead of researching it. From the other side, agile development practices is another mainstream that for ones means flexible though stable organizational methodology and for others a way to save some money by reducing time (in reality by reducing paper work etc. while going directly into the development). To me these two mainstreams are highly coupled. The later makes great use of the former, and from the other side represents a set of organizational patterns itself. So I will talk about patterns in general having in mind agile practices and design patterns as part of it. Patterns are great because you gain a scheme for your solution and do not need to invent a wheel from scratch. Generally saying, patterns are considered to be &#8220;best practices&#8221;, and likewise they are.</p>
<p>For these and other similar reasons the design patterns and agile methodologies are &#8220;mainstream&#8221; now, and that&#8217;s why are severely required to follow in many organizations (perhaps, you have heard something like &#8220;this is not compliant with this or that design pattern or best practice, so is wrong!&#8221;). However, while thinking of patterns against real life, you may find that they are merely common recommendations and do not provide &#8220;every hole fitting&#8221; solutions &#8211; &#8217;cause simply it&#8217;s (almost) impossible &#8211; and then you spent long time searching for a pattern that fits your problem the best or how to apply that given pattern to it. There are so many general design patterns, so that in some cases they overlap and provide confusion. In the end, we&#8217;ve got lots of anti-pattens. Doesn&#8217;t it sound a bit of non-sense?</p>
<p>Thinking of pattens further, I find them restricting a developer&#8217;s fantasy (one, who is able to think and generate his own ideas). Moreover, patterns are simply a set of terms or names for widely known notions which were (and are being!) forged in many organizations. With time new patterns appear, evolve, replace older ones, and it is normal process since new problems to be solved appear. Patterns are being &#8220;invented&#8221; and shouted out and sold (indirectly).</p>
<p>Concluding, in my opinion, the design patterns or principles should not be considered as a rigorous tool and used blindly, though do not forget that patterns are your basic and fast knowledge and don&#8217;t afraid improve them&#8230; so sometimes you will have to make a choice: to drag in a mainstream (for the &#8220;it&#8217;s cool!&#8221; sake), or start your own one!</p>
<div style="float: right; margin-left: 10px;"><a href="http://twitter.com/share?url=http://www.vlad-yatsenko.eu/2009/11/thinking-of-patterns/&via=vyatsenko&text=Thinking of Patterns&related=:&lang=en&count=horizontal" class="twitter-share-button">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style="display:none;"><a href="http://government-politics.forum1000.com">government,politics</a>&nbsp;<a href="http://news365live.com">news,politics</a>&nbsp;<a href="http://worldnews365online.com">news,politics</a></div><div class='wpfblike' style='height: 40px;'><fb:like href='http://www.vlad-yatsenko.eu/2009/11/thinking-of-patterns/' layout='button_count' show_faces='false' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.vlad-yatsenko.eu/2009/11/thinking-of-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

