<?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; Java</title>
	<atom:link href="http://www.vlad-yatsenko.eu/tag/java/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>Default property value in Spring 3</title>
		<link>http://www.vlad-yatsenko.eu/2010/10/default-property-value-in-spring-3/</link>
		<comments>http://www.vlad-yatsenko.eu/2010/10/default-property-value-in-spring-3/#comments</comments>
		<pubDate>Sat, 23 Oct 2010 22:29:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Titbits]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.vlad-yatsenko.eu/?p=133</guid>
		<description><![CDATA[When using Spring all your configuration comes from properties and a mean to make it work is PropertyPlaceholderConfigurer (xml is no longer considered as a configuration, since Spring&#8217;s xml is something stable and considered to be part of codebase, in opposite to properties). Nice it sounds, however, using placeholders has an annoying drawback, i.e. you [...]]]></description>
			<content:encoded><![CDATA[<p>When using Spring all your configuration comes from properties and a mean to make it work is <em>PropertyPlaceholderConfigurer</em> (xml is no longer considered as a configuration, since Spring&#8217;s xml is something stable and considered to be part of codebase, in opposite to properties).</p>
<p>Nice it sounds, however, using placeholders has an annoying drawback, i.e. you can&#8217;t leave a property undefined if there is a reference to it in Spring xml, which basically makes a lot of sense and keeps you on the safe side. However, you would want to lessen your configuration maintenance efforts by specifying, where it may fit, default values for some of the properties. Before <em>Spring 3.0</em> it was not easy to accomplish (<em>PropertyPlaceholderConfigurer.setProperties</em> overrides externally defined values).</p>
<p>Fortunately, guys in SpringSource came up with a neat way to do it, which makes it bish-bash-bosh now:</p>
<div class="codesnip-container" >${propertyName:defaultValue}</div>
<p>Such placeholder commands the Spring&#8217;s PropertyPlaceholderConfigurer to use <em>defaultValue</em> if property named <em>propertyName</em> is not found.</p>
<p>This saved me from a burden of maintaining a new property over a few of dozens of server-specific configuration .properties files, where only one required to have new functionality provided by a new property setting.</p>
<div style="float: right; margin-left: 10px;"><a href="http://twitter.com/share?url=http://www.vlad-yatsenko.eu/2010/10/default-property-value-in-spring-3/&via=vyatsenko&text=Default property value in Spring 3&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/2010/10/default-property-value-in-spring-3/' layout='button_count' show_faces='false' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.vlad-yatsenko.eu/2010/10/default-property-value-in-spring-3/feed/</wfw:commentRss>
		<slash:comments>1</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>
	</channel>
</rss>

