<?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>Software Passion &#187; General Programming</title>
	<atom:link href="http://www.softwarepassion.com/category/general_programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.softwarepassion.com</link>
	<description>by Krzysztof Grajek</description>
	<lastBuildDate>Fri, 23 Dec 2011 07:48:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Java EE 6 Testing with Arquillian Persistence Extension</title>
		<link>http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/</link>
		<comments>http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 07:42:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Learning Materials]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=721</guid>
		<description><![CDATA[Today I will describe my newly discovered library which is an extension to the great JBoss Arquillian project. The mission of the Arquillian project is to provide a simple test harness that abstracts away all container lifecycle and deployment from the test logic so developers can easily produce a broad range of integration tests for [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will describe my newly discovered library which is an extension to the great <a href="http://www.jboss.org/arquillian">JBoss Arquillian</a> project.</p>
<blockquote><p>The mission of the Arquillian project is to provide a simple test harness that abstracts away all container lifecycle and deployment from the test logic so developers can easily produce a broad range of integration tests for their enterprise Java applications.</p></blockquote>
<p>Arquillian persistence extension allows you to test JPA related code without filling up the database with test data. All you have to do is to populate .yml file with your data specified. That will be best described with an example.</p>
<p>First of all add arquillian persistence dependencies to your project:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.jboss.arquillian.extension<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>arquillian-persistence-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${version.arquillian_persistence}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.jboss.arquillian.extension<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>arquillian-persistence-impl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${version.arquillian_persistence}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>where &#8216;${version.arquillian_persistence}&#8217;  is equal to &#8217;1.0.0.Alpha2&#8242;. I assume that you have the basic arquillian tests (for EJB&#8217;s and CDI) working, If not read on.</p>
<p>I have found myself setting up arquillian enabled project quite cumbersome, always ending up with some classpath dependency problems while trying to remote test my classes. For those of you new to Arquillian I would strongly recommend starting up with working example provided by <a href="https://github.com/bartoszmajsak">Bartosz Majsak</a> and available on github as a <a href="https://github.com/bartoszmajsak/Beer-Advisor">&#8216;Beer-Advisor</a>&#8216; project. </p>
<p>The &#8216;Beer-Advisor&#8217; project can be quickly refactored to suit your needs, it lack comments but its so clearly written that it shouldn&#8217;t be a problem to grasp the idea.</p>
<p>Ok then, lets get going with the arquillian persistence extension, we are going to use two nice annotations to help us testing our JPA layer: @UsingDataSet and @ShouldMatchDataSet.</p>
<p>We start off with doing some setup, I will copy part of the example from the Beer-Advisor project which you can easily clone and try for yourself:</p>
<p>Having an entity class Beer:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aentity+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Entity</span></a><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Beer <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Serializable</span></a><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 5892013208071126314L<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Id<br />
&nbsp; &nbsp;@GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> id<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> price<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> alcohol<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> code<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Enumerated<span style="color: #009900;">&#40;</span>EnumType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> Type type<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// If lazy then Glassfish Embedded is facing this problem: https://bugs.eclipse.org/bugs/show_bug.cgi?id=323403</span><br />
&nbsp; &nbsp;@ManyToOne<span style="color: #009900;">&#40;</span>optional <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span>, cascade <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">PERSIST</span>, CascadeType.<span style="color: #006633;">MERGE</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> Brewery brewery<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;Beer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// to satisfy JPA</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> Beer<span style="color: #009900;">&#40;</span>Brewery brewery, Type type, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> price, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> alcohol<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">price</span> <span style="color: #339933;">=</span> price<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">alcohol</span> <span style="color: #339933;">=</span> alcohol<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">type</span> <span style="color: #339933;">=</span> type<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">brewery</span> <span style="color: #339933;">=</span> brewery<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">brewery</span>.<span style="color: #006633;">addBeer</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
........ <span style="color: #006633;">Getters</span> and Setters ...............</div></div>
<p>and simple DAO for managing Beer objects on the persistence layer:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@RequestScoped<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JpaBeerRepository <span style="color: #000000; font-weight: bold;">implements</span> BeerRepository<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;@PersistenceContext<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> EntityManager em<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Override<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> Beer getById<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> id<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> em.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>Beer.<span style="color: #000000; font-weight: bold;">class</span>, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
..........<br />
<br />
@Override<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> Set<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> fetchAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; CriteriaBuilder criteriaBuilder <span style="color: #339933;">=</span> em.<span style="color: #006633;">getCriteriaBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; CriteriaQuery<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> query <span style="color: #339933;">=</span> criteriaBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>Beer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Root<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> from <span style="color: #339933;">=</span> query.<span style="color: #006633;">from</span><span style="color: #009900;">&#40;</span>Beer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; CriteriaQuery<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> select <span style="color: #339933;">=</span> query.<span style="color: #006633;">select</span><span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; Set<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; result.<span style="color: #006633;">addAll</span><span style="color: #009900;">&#40;</span>em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>select<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;@Override<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> save<span style="color: #009900;">&#40;</span>Beer beer<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>beer.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;em.<span style="color: #006633;">persist</span><span style="color: #009900;">&#40;</span>beer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;em.<span style="color: #006633;">merge</span><span style="color: #009900;">&#40;</span>beer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
..................</div></div>
<p>We can easily test our DAO by creating datasets in yml format and test our logic against it:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Beer:<br />
&nbsp; - id: <span style="color: #000000;">1</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">4.5</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Mocny Full&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">1.0</span><br />
&nbsp; &nbsp; type: LAGER<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">1</span><br />
&nbsp; - id: <span style="color: #000000;">2</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">55.0</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;End of history&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">765</span><br />
&nbsp; &nbsp; type: BLOND_ALE<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">2</span><br />
&nbsp; - id: <span style="color: #000000;">3</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">41.0</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Sink The Bismarck!&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">64.0</span><br />
&nbsp; &nbsp; type: QUADRUPEL_IPA<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">2</span><br />
&nbsp; - id: <span style="color: #000000;">4</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">8.5</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Delirium Tremens&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">10.0</span><br />
&nbsp; &nbsp; type: PALE_ALE<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">3</span><br />
&nbsp; - id: <span style="color: #000000;">5</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">8.4</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Pauwel Kwak&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">4.0</span><br />
&nbsp; &nbsp; type: AMBER<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">4</span></div></div>
<p>You should put your .yml file into test/resources/datasets folder in you maven project</p>
<p>@UsingDataSet annotation will populate our database with the data specified in beers.yml file</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;@Test<br />
&nbsp; &nbsp;@UsingDataSet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;beers.yml&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldReturnBeerByItsId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// given</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> beerId <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> expectedName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Mocny Full&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// when</span><br />
&nbsp; &nbsp; &nbsp; Beer beers <span style="color: #339933;">=</span> beerRepository.<span style="color: #006633;">getById</span><span style="color: #009900;">&#40;</span>beerId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// then</span><br />
&nbsp; &nbsp; &nbsp; assertThat<span style="color: #009900;">&#40;</span>beers.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isEqualTo</span><span style="color: #009900;">&#40;</span>expectedName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<p>Now, lets say we want to add new Beer and see if the database will have a specific data in it, for this purpose we are going to use @ShouldMatchDataSet(..)</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Test<br />
&nbsp; &nbsp; @ShouldMatchDataSet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;expected-insert-beer.yml&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldSaveNewBeer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//when</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Beer b <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Beer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//set the necessary fields on the beer object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; beerRepository.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertThat<span style="color: #009900;">&#40;</span>beerRepository<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>after inserting our Beer object to the database through the EntityManager we should end up with a single beer record in our database, exactly like the one specified in &#8216;expected-insert-beer.yml&#8217;.</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why you should join Stackoverflow</title>
		<link>http://www.softwarepassion.com/why-you-should-join-stackoverflow/</link>
		<comments>http://www.softwarepassion.com/why-you-should-join-stackoverflow/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 19:09:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=646</guid>
		<description><![CDATA[Back in May 2008 when I first heard about stackoverflow idea I was really skeptical about it. You can read my rants about the whole idea here. Since then, I have used stackoverflow more and more and I have to say that for the last couple of months I actually love it.This was a long [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Back in May 2008 when I first heard about stackoverflow idea I was really skeptical about it. You can read my rants about the whole idea <a title="Why do we really need Stackoverflow" href="http://www.softwarepassion.com/do-we-really-need-stackoverflow/" target="_blank">here</a>. Since then, I have used stackoverflow more and more and I have to say that for the last couple of months I actually love it.This was a long process for me but I can assure you, that this post is going to be completely different the the one written in the middle of 2008.<br />
Once I started contributing and become a part of stackoverflow community I have discovered quite a few things about me and about the website itself.</p>
<p style="text-align: center;"><a title="Stackoverflow" href="http://stackoverflow.com/" target="_blank"><img class="aligncenter size-full wp-image-647" title="stackoverflow-logo" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/stackoverflow-logo.png" alt="" width="250" height="70" /></a></p>
<p style="text-align: left;">I think many developers agree, that if you are into programming you are willing to learn all the time, learning is fun and if you have a source of learning repeatedly every day something new, you will be a happy developer. Of course many of us read books, blogs, community sites like reddit, dzone or digg, looking for something cool to read every day. Many of us do certifications just for fun, just to get some feeling of knowledge gaining. For me Stackoverflow is becoming one of the major sources of self development which I can do at my own peace.</p>
<p>Possibly, every person will find something different for himself at stackoverflow, and it all depends on our personal preferences and goals. If you are reading this and you still don&#8217;t use stackoverflow much, I urge you to try at least for a month.<strong> </strong></p>
<h3><strong>1. You can learn by teaching</strong></h3>
<p style="text-align: center;"><strong><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/learning.jpg" rel="lightbox[646]"><img class="aligncenter size-medium wp-image-648" title="learning" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/learning-300x262.jpg" alt="" width="300" height="262" /></a><br />
</strong></p>
<p><strong> </strong>Everyone who has ever thought about improving his own learning efficiency knows this. When you try to teach what you have just learned, everything auto-magically get nicely organized in your head. All you have to do is to find a way to share your knowledge &#8211; now you can, use a stackoverflow of course.</p>
<p>You can read more about effective learning <a href="http://psychology.about.com/od/educationalpsychology/tp/effective-learning.htm">here</a>.</p>
<h3><strong>2. You can collect stuff</strong></h3>
<p><strong><br />
</strong></p>
<p>
<iframe frameborder="0" width="480" height="360" src="http://www.dailymotion.com/embed/video/xcbtqd"></iframe><br /><a href="http://www.dailymotion.com/video/xcbtqd_turbo-bubble-gum-pictures_auto" target="_blank">Turbo bubble gum pictures</a> <i> by <a href="http://www.dailymotion.com/KnightRiderDDR" target="_blank">KnightRiderDDR</a></i>
</p>
<p><em><br />
</em></p>
<p>The first and last thing in my life I was actually collecting, was a chewing gum stickers in the middle eighties, they were called <a href="http://www.dailymotion.com/video/xcbtqd_turbo-bubble-gum-pictures_auto">&#8216;Turbo&#8217;</a> and most of the people reading this won&#8217;t probably know them, as they were sold in eastern europe only(I can be wrong), it was fun though, but now I have stackoverflow <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
For all the people collecting all kind of stuff, well, by using a stackoverflow you can fullfill your desire and collect something new &#8211; stackoverflow points and different kinds of badges</p>
<h3><strong>3. You can compete</strong></h3>
<p>People like competition of many kinds and they do it for whatever reason. Whatever reason you have, you can now compete on stackoverflow when providing better answers than others, collecting more points etc.<br />
Remember, competition is a driving force of progress.</p>
<h3><strong>4. You can revise your knowledge</strong></h3>
<p>This is for me actually the most important point. Remember &#8220;forgetting curve&#8221;? No? Well, its because you have probably not revised that information and you have forgotten <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
This is simple, if you don&#8217;t repeat what you have learned you will finally forget it. By using a stackoverflow you can highly minimize or prevent totally your memory loss.</p>
<p style="text-align: center;"><a href="http://en.wikipedia.org/wiki/Forgetting_curve"><img class="aligncenter" title="Forgetting Curve - Wikipedia" src="http://upload.wikimedia.org/wikipedia/commons/4/4e/ForgettingCurve.svg" alt="Forgetting Curve - Wikipedia" width="277" height="237" /></a></p>
<h3><strong>5. You can expand your knowledge</strong></h3>
<p>If you&#8217;ll jump into a stackoverflow mania you will spend there a bit more time than just the time answering/asking questions. You will probably be  browsing the web app, reading notification of posted comments to your questions/answers, at the same time you notice other people answers, you&#8217;ll often find out a better approach of doing something than your own answer gave, links to an on-line articles, documentation, other interesting questions in your field of expertise etc, you&#8217;ll just find the best of the web posted by other people in one place, all related to whats the most interesting for you.</p>
<h3><strong>6. You can ask questions yourself</strong></h3>
<p>I almost forgot about this one. You can of course ask questions there and if they are not too difficult someone will probably answer them pretty fast <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3><strong>7. You can join Careers 2.0</strong></h3>
<p>I&#8217;ve just joined<a href="http://careers.stackoverflow.com/"> Careers 2.0</a> and I can say that it&#8217;s well done web app, unfortunately there is not a single company from Poland <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Anyway I guess it can be useful for others (especially in the US + UK state)</p>
<h3><strong>8. You can meet people at Stackoverflow meet-up days</strong></h3>
<p><strong><br />
</strong></p>
<p style="text-align: center;"><strong><a href="http://blog.stackoverflow.com/2011/03/stack-overflow-meetups-april-6/" target="_blank"><img class="aligncenter size-full wp-image-653" title="220x250_Meetups_Banner" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/220x250_Meetups_Banner.jpg" alt="" width="220" height="250" /></a><br />
</strong></p>
<p>Haven&#8217;t tried that yet but It&#8217;s always good to have a good excuse to go for a beer, or isn&#8217;t it?</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/why-you-should-join-stackoverflow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing Artifactory on Ubuntu box</title>
		<link>http://www.softwarepassion.com/install-artifactory-on-ubuntu-box/</link>
		<comments>http://www.softwarepassion.com/install-artifactory-on-ubuntu-box/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 10:28:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=586</guid>
		<description><![CDATA[This is a simple and straightforward guide on how to install Artifactory and import exisiting maven repository into it. 1. Download Artifactory from JFrog website 2. Unzip downloaded file in your home directory 3. Change to root user (su -) 4. Set your JAVA_HOME in /etc/environment JAVA_HOME=/usr/lib/jvm/java-6-sun/ PATH=&#34;/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games&#34; 5. Set your JAVA_HOME in /etc/profile # [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>This is a simple and straightforward guide on how to install Artifactory and import exisiting maven repository into it.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/Screenshot-1.png" rel="lightbox[586]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/Screenshot-1.png" alt="" title="Screenshot-1" width="800" height="404" class="aligncenter size-full wp-image-590" /></a></p>
<p>1. Download Artifactory from <a href="http://www.jfrog.com/download.php">JFrog website</a><br />
2. Unzip downloaded file in your home directory<br />
3. Change to root user (su -)<br />
4. Set your JAVA_HOME in /etc/environment</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>jvm<span style="color: #000000; font-weight: bold;">/</span>java-<span style="color: #000000;">6</span>-sun<span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games&quot;</span></div></div>
<p>5. Set your JAVA_HOME in /etc/profile</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># /etc/profile: system-wide .profile file for the Bourne shell (sh(1))</span><br />
<span style="color: #666666; font-style: italic;"># and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).</span><br />
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>jvm<span style="color: #000000; font-weight: bold;">/</span>java-<span style="color: #000000;">6</span>-sun<span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>profile.d <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>profile.d<span style="color: #000000; font-weight: bold;">/*</span>.sh; <span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp;.............. rest of the file.....</div></div>
<p>6. Go to /home/kris/artifactory-2.3.2/bin (of course replace &#8216;kris&#8217; with your home folder name)<br />
7. Run</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$</span>: .<span style="color: #000000; font-weight: bold;">/</span>install.sh</div></div>
<p>8. Check if your installation was successful, run:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$</span>: service artifactory check</div></div>
<p>9. If you get error saying something like: &#8220;Cannot find a JRE or JDK. Please set JAVA_HOME to a >=1.5 JRE&#8221; , edit /etc/artifactory/default and add JAVA_HOME export:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000;">10</span>. Run the check again <span style="color: #7a0874; font-weight: bold;">&#40;</span>service artifactory check<span style="color: #7a0874; font-weight: bold;">&#41;</span> and <span style="color: #000000; font-weight: bold;">if</span> all goes fine run<br />
<br />
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">cc</span> <span style="color: #007800;">lang</span>=<span style="color: #ff0000;">&quot;bash&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
$: service artifactory start</div></div>
<p>11. In your browser to into: http://localhost:8081/artifactory and log in as administrator (default user/pass is admin/password)</p>
<p>12. After logging in, click on the &#8216;Admin&#8217; tab, then &#8216;Import &#038; Export&#8217; and &#8216;Repositories&#8217;</p>
<p>13. Inside the &#8216;Import&#8217; section select &#8216;ext-release-local&#8217; as your &#8216;Target Local Repo&#8217; and find your maven .m2 repository<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/artifactory_import.png" rel="lightbox[586]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/artifactory_import-300x172.png" alt="" title="artifactory_import" width="300" height="172" class="aligncenter size-medium wp-image-587" /></a></p>
<p>14. Click import and your are all set!</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/install-artifactory-on-ubuntu-box/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Design Patterns Series &#8211; Part Five: Adapter pattern</title>
		<link>http://www.softwarepassion.com/design-patterns-series-part-five-adapter-pattern/</link>
		<comments>http://www.softwarepassion.com/design-patterns-series-part-five-adapter-pattern/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 09:42:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=511</guid>
		<description><![CDATA[According to wikipedia: In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. The adapter pattern is commonly used when there is a need to access an object through the interface which doesn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>According to <a href="http://en.wikipedia.org/wiki/Adapter_pattern">wikipedia</a>:</p>
<blockquote><p>In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface.</p></blockquote>
<p>The adapter pattern is commonly used when there is a need to access an object through the interface which doesn&#8217;t match the one you need. This pattern is especially useful if you have a set of existing classes and you need to use them together with other ones, exposing different interface.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/adapterpattern2.png" rel="lightbox[511]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/adapterpattern2.png" alt="" title="adapterpattern" width="917" height="320" class="aligncenter size-full wp-image-521" /></a><br />
The purpose of this pattern is  to make some objects look like something they are not. Normally, you wouldn&#8217;t use this pattern at the design stage but rather while extending an existing application, working with 3rd party libraries or tools.</p>
<p>Simple example using adapter pattern:<br />
We have an existing class called ThirdPartyShape with a method &#8216;paintShape&#8217; we want to move into a common interface.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThirdPartyShape <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paintShape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Painting 3rd party shape&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Common interface in our new application:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ShinyShapeIfc <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Example of standard shape class in our application:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyNewShinyShape <span style="color: #000000; font-weight: bold;">implements</span> ShinyShapeIfc<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Painting brand new and shiny custom shape!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Adapter class used to utilize functionality of 3rd party class into our interface for painting shapes</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">adapterpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShinyShape3rdpartyAdapter <span style="color: #000000; font-weight: bold;">implements</span> ShinyShapeIfc<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ThirdPartyShape thirdPartyShape<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ShinyShape3rdpartyAdapter<span style="color: #009900;">&#40;</span>ThirdPartyShape tps<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">thirdPartyShape</span> <span style="color: #339933;">=</span> tps<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Method adapting 3rd party functionality into a common<br />
&nbsp; &nbsp; &nbsp;* interface<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">thirdPartyShape</span>.<span style="color: #006633;">paintShape</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Simple proof of concept, using our shapes with existing 3rd party shapes by implementing and adapter pattern:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args the command line arguments<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ThirdPartyShape thirdPartyShape <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ThirdPartyShape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ShinyShapeIfc myOwnShinyShape <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyNewShinyShape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//adapting 3rd party class into a common interface using an adapter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ShinyShapeIfc adapted3rdPartyShape <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ShinyShape3rdpartyAdapter<span style="color: #009900;">&#40;</span>thirdPartyShape<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//object below implement common interface </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myOwnShinyShape.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; adapted3rdPartyShape.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/design-patterns-series-part-five-adapter-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Design Pattern Series &#8211; Part Four: Observer pattern</title>
		<link>http://www.softwarepassion.com/design-pattern-series-part-four-observer-pattern/</link>
		<comments>http://www.softwarepassion.com/design-pattern-series-part-four-observer-pattern/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 08:17:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=479</guid>
		<description><![CDATA[Today I&#8217;m adding another important design pattern to the toolbox. Observer Pattern is commonly used and quite powerful. The Observer Pattern defines one to many dependency between objects so that when one object changes state, all of it dependents are notified and updated. We can create a class structure for the pattern ourselves from scratch [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I&#8217;m adding another important design pattern to the toolbox. Observer Pattern is commonly used and quite powerful.</p>
<blockquote><p>The Observer Pattern defines one to many dependency between objects so that when one object changes state, all of it dependents are notified and updated.
</p></blockquote>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/observerpattern1.png" rel="lightbox[479]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/observerpattern1.png" alt="" title="observerpattern" width="960" height="370" class="aligncenter size-full wp-image-491" /></a></p>
<p>We can create a class structure for the pattern ourselves from scratch or we can use build in Observer functionality in JDK.</p>
<p>Utilizing JDK Observer-Observable mechanism:</p>
<p>NameService.java</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">jdk_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Observable</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameService <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobservable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observable</span></a> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setChanged</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; notifyObservers<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>NameObserver.java</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">jdk_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Observable</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Observer</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameObserver <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a><span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> objName<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> NameObserver<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> observername<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">objName</span> <span style="color: #339933;">=</span> observername<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> update<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobservable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observable</span></a> o, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Observer: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, name changed: &quot;</span> <span style="color: #339933;">+</span> o1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">objName</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Main method for testing:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">jdk_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> NameService service<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> NameObserver observer1<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> NameObserver observer2<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; observer1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameObserver<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;First Observer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; observer2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameObserver<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Second Observer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span style="color: #006633;">addObserver</span><span style="color: #009900;">&#40;</span>observer1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span style="color: #006633;">addObserver</span><span style="color: #009900;">&#40;</span>observer2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args the command line arguments<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Main m <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006633;">test</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Count: &quot;</span> <span style="color: #339933;">+</span> service.<span style="color: #006633;">countObservers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Big Lebowsky&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>service.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Custom Observer pattern implementation:</p>
<p>Subject interface</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">custom_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Subject <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> registerObserver<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a> o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeObserver<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a> o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> notifyObservers<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Observer interface:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">custom_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> updateStatus<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>NameService:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">custom_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameService <span style="color: #000000; font-weight: bold;">implements</span> Subject <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Observer<span style="color: #339933;">&gt;</span> observers<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> NameService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; observers <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Observer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> registerObserver<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">observers</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeObserver<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">observers</span>.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> notifyObservers<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a> o <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">observers</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o.<span style="color: #006633;">updateStatus</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">notifyObservers</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>NameObserver:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">custom_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameObserver <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobserver+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Observer</span></a> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> objName<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> NameObserver<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> observername<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">objName</span> <span style="color: #339933;">=</span> observername<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> updateStatus<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating status: &quot;</span> <span style="color: #339933;">+</span> o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">objName</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Main class for testing purposes:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">custom_observerpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main2 <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> NameService service<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> NameObserver observer1<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> NameObserver observer2<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Main2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; observer1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameObserver<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;First Observer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; observer2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameObserver<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Second Observer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span style="color: #006633;">registerObserver</span><span style="color: #009900;">&#40;</span>observer1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span style="color: #006633;">registerObserver</span><span style="color: #009900;">&#40;</span>observer2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> art<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Main2 m <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Main2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006633;">test</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Big Lebowsky&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>service.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/design-pattern-series-part-four-observer-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Biggest Java WTF</title>
		<link>http://www.softwarepassion.com/the-biggest-java-wtf/</link>
		<comments>http://www.softwarepassion.com/the-biggest-java-wtf/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 14:27:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=484</guid>
		<description><![CDATA[Today one of my co-workers send me the following link. As I couldn&#8217;t believe my eyes, I have tested it myself. WTF? package wtfjava; /** &#160;* This program really HANGS!!!! &#160;* @author kris &#160;*/ public class Main &#123; &#160; &#160; /** &#160; &#160; &#160;* @param args the command line arguments &#160; &#160; &#160;*/ &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today one of my co-workers send me the following <a href="http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/">link</a>.<br />
As I couldn&#8217;t believe my eyes, I have tested it myself.<br />
WTF?</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">wtfjava</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* This program really HANGS!!!!<br />
&nbsp;* @author kris<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args the command line arguments<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> d <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adouble+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Double</span></a>.<span style="color: #006633;">parseDouble</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2.2250738585072012e-308&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Value: &quot;</span> <span style="color: #339933;">+</span> d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/the-biggest-java-wtf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design Patterns &#8211; Part Three: Singleton</title>
		<link>http://www.softwarepassion.com/design-patterns-part-three-singleton/</link>
		<comments>http://www.softwarepassion.com/design-patterns-part-three-singleton/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 11:20:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=473</guid>
		<description><![CDATA[Today I will describe the Singleton pattern, one of the simplest, yet generating active discussions all over the net. Some people consider it to be an antipattern. We have many different flavours of this pattern, singleton can be thread safe, lazy loaded, eagerly loaded, implemented as a static class or enum. Simple thread safe implementation: [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will describe the Singleton pattern, one of the simplest, yet generating active discussions all over the net. Some people consider it to be an antipattern. We have many different flavours of this pattern, singleton can be thread safe, lazy loaded, eagerly loaded, implemented as a static class or enum.<br />
Simple thread safe implementation:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* This solution uses class loading mechanizm and its<br />
&nbsp;* thread safe<br />
&nbsp;* @author kris<br />
&nbsp;* @version 01-02-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FirstSingleton <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> FirstSingletonHolder <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> FirstSingleton INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FirstSingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> FirstSingleton getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> FirstSingletonHolder.<span style="color: #006633;">INSTANCE</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Simple lazy loading implementation:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Lazy loaded version of singleton pattern<br />
&nbsp;* @author kris<br />
&nbsp;* @version 01-02-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SecondSingleton <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> SecondSingleton instance <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SecondSingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SecondSingleton getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> instance<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span>SecondSingleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> instance<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SecondSingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> instance<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aclonenotsupportedexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">CloneNotSupportedException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aclonenotsupportedexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">CloneNotSupportedException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Enum implementation of Singleton pattern (according to J.Bloch the best implementation availalble)</p>
<blockquote><p>&#8220;This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.&#8221;</p></blockquote>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Enum implementation of singleton design pattern<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 01-02-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> EnumSingleton <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; INSTANCE<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>If you feel that singleton is the right solution to your problem, use the one which is shortest/simplest and available in your environment (enum version J2EE 5+)</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/design-patterns-part-three-singleton/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Design Patterns Series &#8211; Part Two: Template Method</title>
		<link>http://www.softwarepassion.com/design-patterns-series-part-two-template-method/</link>
		<comments>http://www.softwarepassion.com/design-patterns-series-part-two-template-method/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 12:50:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=464</guid>
		<description><![CDATA[A typical template method pattern consists of the abstract base class which defines one or more template methods (usually set as final) and some abstract methods which the subclass has to implement. Template method defines common algorithm where some of the steps are implemented by subclasses. A Template method pattern provides a skeleton for performing [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>A typical template method pattern consists of the abstract base class which defines one or more template methods (usually set as final) and some abstract methods which the subclass has to implement. Template method defines common algorithm where some of the steps are implemented by subclasses.</p>
<blockquote><p>A Template method pattern provides a skeleton for performing any sort of algorithm or an operation, and it allows the sub-classes to re-define part of the logic</p></blockquote>
<p>This is one of the easiest design patterns and has a very simple UML model:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/templatemethodpattern.png" rel="lightbox[464]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/templatemethodpattern.png" alt="" title="templatemethodpattern" width="624" height="592" class="aligncenter size-full wp-image-465" /></a></p>
<p>AmazonKindle.java</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.templatemethodpattern</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Defines a template for assembling Amazon Kindle Devices<br />
&nbsp;*<br />
&nbsp;* @author kris<br />
&nbsp;* @version 31-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> AmazonKindle <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> AmazonKindle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Example of Template Method design pattern<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">void</span> assembleAmazonKindle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; createKindleCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setDisplay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; addModem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; printSpecs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> &nbsp;createKindleCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> setDisplay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> addModem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> printSpecs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Printing Amazon Kindle specs..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>AmazonKindle6WiFi.java</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* To change this template, choose Tools | Templates<br />
&nbsp;* and open the template in the editor.<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.templatemethodpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Concrete implementation of {@code AmazonKindle}<br />
&nbsp;* with 6&quot; screen and WiFi modem<br />
&nbsp;* assembled in a standard way using a template<br />
&nbsp;* @author kris<br />
&nbsp;* @version 31-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AmazonKindle6WiFi <span style="color: #000000; font-weight: bold;">extends</span> AmazonKindle<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createKindleCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;creates a kindle case for 6<span style="color: #000099; font-weight: bold;">\&quot;</span> kindles&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDisplay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;adds a 6<span style="color: #000099; font-weight: bold;">\&quot;</span> display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addModem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;adds a WiFi modem&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>AmazonKindle6_3G.java</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.templatemethodpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Concrete implementation of {@code AmazonKindle}<br />
&nbsp;* with 6&quot; screen and 3G modem<br />
&nbsp;* assembled in a standard way using a template<br />
&nbsp;* <br />
&nbsp;* @author kris<br />
&nbsp;* @version 31-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AmazonKindle6_3G <span style="color: #000000; font-weight: bold;">extends</span> AmazonKindle<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createKindleCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;creates a kindle case for 6<span style="color: #000099; font-weight: bold;">\&quot;</span> kindles&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDisplay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;adds a 6<span style="color: #000099; font-weight: bold;">\&quot;</span> display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addModem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;adds a 3G modem&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Main.java for simple testing..</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args the command line arguments<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Assembling amazon kindles&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//assembling wifi kindle</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; AmazonKindle kindle <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonKindle6WiFi<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Main main <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; main.<span style="color: #006633;">assembleAmazonKindle</span><span style="color: #009900;">&#40;</span>kindle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//assembling 3g kindle</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; kindle <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonKindle6_3G<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; main.<span style="color: #006633;">assembleAmazonKindle</span><span style="color: #009900;">&#40;</span>kindle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> assembleAmazonKindle<span style="color: #009900;">&#40;</span>AmazonKindle kindle<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; kindle.<span style="color: #006633;">assembleAmazonKindle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/design-patterns-series-part-two-template-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design Patterns Series &#8211; Part One: Strategy Pattern</title>
		<link>http://www.softwarepassion.com/design-patterns-series-part-one-strategy-pattern/</link>
		<comments>http://www.softwarepassion.com/design-patterns-series-part-one-strategy-pattern/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 21:04:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=439</guid>
		<description><![CDATA[The Strategy Pattern is a first pattern described in a series of post I&#8217;m planning to make regarding this topic. The Strategy pattern allows us to select a specific algorithm at runtime and its one of the simples design patterns to grasp. The scenario described on the diagram below shows a simple usage of the [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>The Strategy Pattern is a first pattern described in a series of post I&#8217;m planning to make regarding this topic. The Strategy pattern allows us to select a specific algorithm at runtime and its one of the simples design patterns to grasp.<br />
The scenario described on the diagram below shows a simple usage of the strategy pattern.<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/strategy_pattern.png" rel="lightbox[439]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/strategy_pattern.png" alt="" title="strategy_pattern" width="799" height="380" class="aligncenter size-full wp-image-441" /></a></p>
<p>Let&#8217;s say we have a HttpServlet which creates a report using data retrieved from the request parameters, or some other sources like database. and depending on which kind of report we want to create (PDF, DOC or HTML) chooses appropriate algorithm to do just that.<br />
So w end up with common interface for creating reports called &#8216;ReportGenerator&#8217; with a single method &#8216;generateReport(ReportData rd)&#8217;:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;* <br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Common interface for generating reports using different formats<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ReportGenerator <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Generates report using provided report data<br />
&nbsp; &nbsp; &nbsp;* @param rd ReportData object carrying all info needed to produce report<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>We have three different implementations of this interface, allowing us to generate report in a form of PDF, DOC or HTML format.<br />
PDF Report Generator:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Responsible for handling PDF reports<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFReportGenerator <span style="color: #000000; font-weight: bold;">implements</span> ReportGenerator <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generating PDF report...&quot;</span> <span style="color: #339933;">+</span> rd.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Microsoft Word Generator:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Responsible for handling reports in MS Word format<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MsDOCReportGenerator <span style="color: #000000; font-weight: bold;">implements</span> ReportGenerator<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generating MS Word report.. &quot;</span> <span style="color: #339933;">+</span> rd.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>HTML Report Generator:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Responsible for generating reports in HTML format<br />
&nbsp;*<br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HTMLReportGenerator <span style="color: #000000; font-weight: bold;">implements</span> ReportGenerator<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generating HTML report...&quot;</span> <span style="color: #339933;">+</span> rd.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>All mentioned above generators use a single object (simple POJO class) containing the data needed to generate report ReportData</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Used to transport data needed to produce report<br />
&nbsp;* in any form using {@link ReportGenerator}<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ReportData <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> title<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> description<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//...other report related stuff needed to produce report</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//..getters and setters for the class fields</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getDescription<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> description<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescription<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> title<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTitle<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> title<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">title</span> <span style="color: #339933;">=</span> title<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>All of different kinds of generators can be used in our simple report servlet:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Example class using {@code ReportGenerator} interface<br />
&nbsp;* This class mimmicks the behaviour of a http servlet, assuming that all needed<br />
&nbsp;* parameters for generating report are coming from the database or in the form<br />
&nbsp;* of servlet request parameters contained in a single {@code ReportData } object,<br />
&nbsp;* we choose one of the available report generators<br />
&nbsp;* to generate our report.<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MockReportServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ReportGenerator reportGenerator<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onGet<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> request, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//1. get the data from the database, request parameters or any other source</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//in the form of ReportData object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ReportData reportData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReportData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; reportData.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Design Patterns Report for: Strategy Pattern&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//2. Using the request parameter decide on which one of three report generators</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//to use when generating the report</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//3. Assuming that PDF generator was selected by the user generate PDF report</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PDF&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reportGenerator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDFReportGenerator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reportGenerator.<span style="color: #006633;">generateReport</span><span style="color: #009900;">&#40;</span>reportData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The source code for this example (in the form of Netbeans project) can be found <a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/StrategyDesignPattern.zip">here</a></p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/design-patterns-series-part-one-strategy-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple Guide to basic Javadoc</title>
		<link>http://www.softwarepassion.com/simple-guide-to-basic-javadoc/</link>
		<comments>http://www.softwarepassion.com/simple-guide-to-basic-javadoc/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 15:09:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=428</guid>
		<description><![CDATA[Javadoc tool is in my opinion one of the most underestimated tools available for a Java developer. Recently I have started digging into some opensource projects when looking for a nice examples of design patterns and I was surprised at high quality of their code in terms of commenting. This is something I, and many [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Javadoc tool is in my opinion one of the most underestimated tools available for a Java developer. Recently I have started digging into some opensource projects when looking for a nice examples of design patterns and I was surprised at high quality of their code in terms of commenting. This is something I, and many people I know, forget to do while producing source code every day.<br />
Following the principle to leave the source in a better condition every time we open it in the IDE, I will leave here a simple example of a nice and clean class comment:</p>
<p>(example from JHotDraw)</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Tool to select and manipulate figures.<br />
*<br />
<br />
* A selection tool is in one of three states: 1) area<br />
* selection, 2) figure dragging, 3) handle manipulation. The different<br />
* states are handled by different tracker objects: the<br />
* &lt;code&gt;DefaultSelectAreaTracker&lt;/code&gt;, the &lt;code&gt;DefaultDragTracker&lt;/code&gt; and the<br />
* &lt;code&gt;DefaultHandleTracker&lt;/code&gt;.<br />
*<br />
<br />
* A Figure can be selected by clicking at it. Holding the alt key or the<br />
* ctrl key down, selects the Figure behind it.<br />
*<br />
<br />
&lt;hr /&gt;<br />
* &lt;strong&gt;Design Patterns&lt;/strong&gt;<br />
*<br />
*<br />
<br />
&lt;em&gt;Strategy&lt;/em&gt;<br />
<br />
* The different behavior states of the selection tool are implemented by<br />
* trackers.<br />
<br />
* Context: {@link SelectionTool}; State: {@link DragTracker},<br />
* {@link HandleTracker}, {@link SelectAreaTracker}.<br />
*<br />
*<br />
<br />
&lt;em&gt;Chain of responsibility&lt;/em&gt;<br />
<br />
* Mouse and keyboard events of the user occur on the drawing view, and are<br />
* preprocessed by the {@code DragTracker} of a {@code SelectionTool}. In<br />
* turn {@code DragTracker} invokes &quot;track&quot; methods on a {@code Handle} which in<br />
* turn changes an aspect of a figure.<br />
<br />
* Client: {@link SelectionTool}; Handler: {@link DragTracker}, {@link Handle}.<br />
*<br />
<br />
&lt;hr /&gt;<br />
*<br />
* @author Werner Randelshofer<br />
* @version $Id: SelectionTool.java 718 2010-11-21 17:49:53Z rawcoder $<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SelectionTool <span style="color: #000000; font-weight: bold;">extends</span> AbstractTool<span style="color: #009900;">&#123;</span><br />
.................... <span style="color: #000000; font-weight: bold;">class</span> source code ..........<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Some comments on the comment itself:</p>
<p><strong>1.</strong> Use</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/code.html"><span style="color: #000000; font-weight: bold;">code</span></a>&gt;</span></div></div>
<p>style for keywords and names.</code><br />
Keywords and names are offset by</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/code.html"><span style="color: #000000; font-weight: bold;">code</span></a>&gt;</span>...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/code.html"><span style="color: #000000; font-weight: bold;">code</span></a>&gt;</span></div></div>
<p>when mentioned in a description. This includes:</p>
<p>* Java keywords<br />
* package names<br />
* class names<br />
* method names<br />
* interface names<br />
* field names<br />
* argument names<br />
* code examples</p>
<p><strong>2.</strong> add links for API names (listed immediately above) using the</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{@link}</div></div>
<p>tag. It is not necessary to add links for all API names in a doc comment. Because links call attention to themselves (by their color and underline in HTML, and by their length in source code doc comments).</p>
<p><strong>3.</strong> User @Code instead of <code> if you have to include some special chars in it eg.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;{@code Outer<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/s.html"><span style="color: #000000; font-weight: bold;">S</span></a>&gt;</span>.Inner<span style="color: #009900;">&lt;T&gt;</span>}</div></div>
<p>see the <a href="http://blogs.sun.com/darcy/entry/javadoc_tip_code_and_literal">Joseph D. Darcy's Oracle Weblog</a> for more info.</p>
<p><strong>4. </strong> Use the @author tag and don't be ashamed of your code <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>See more example of good commenting in <a href="http://www.jhotdraw.org/">JHotDraw</a> project</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/simple-guide-to-basic-javadoc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

