<?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>Shaaf's Blog &#187; Java</title>
	<atom:link href="http://www.shaafshah.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shaafshah.com</link>
	<description>Another bit in the wall</description>
	<lastBuildDate>Wed, 12 Aug 2009 14:28:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting started with JBehave in 8 steps.</title>
		<link>http://www.shaafshah.com/2009/08/12/getting-started-with-jbehave-in-8-steps/</link>
		<comments>http://www.shaafshah.com/2009/08/12/getting-started-with-jbehave-in-8-steps/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 14:13:30 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jbehave]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=256</guid>
		<description><![CDATA[This post is about JBehave and how to quickly get started with it. If you would like to know about BDD please use the following link. What is Behavioral Driven Development? Today I have used JBehave for the first time. It does have some convincing factors for instance diving requirements into scenarios which map pretty [...]]]></description>
			<content:encoded><![CDATA[<p>This post is about JBehave and how to quickly get started with it. If you would like to know about BDD please use the following link.<br />
<a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">What is Behavioral Driven Development?</a></p>
<p>Today I have used JBehave for the first time. It does have some convincing factors for instance diving requirements into scenarios which map pretty nicely to the tests that are written with in the Steps. Thus it seems like it would be easier for Stakeholder/s to use it as a good guideline for the initial requirements. Its always quite usual to come up to some disagreements about the development however the tool does help to bring forth the ease for stake holders who really dont have to get into writing code but will have a technical jargon to communicate through to the developers in shape of scenarios.</p>
<p>So the first things come up.<br />
1. Create a Java project in Eclipse.<br />
2. Download the JBehave latest version from the <a href="http://jbehave.org/software/download/">website</a> and add the jar files to your project build classpath<br />
3. Create a scenario file and name it as user_wants_to_shop.scenario</p>
<p>Add the following text to the file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Given user Shaaf is logged in
Check <span style="color: #000000; font-weight: bold;">if</span> user shaaf is Allowed to shop
Then show shaaf his shopping cart</pre></td></tr></table></div>

<p>The lines above simply state the rules for the scenario we are about to create.</p>
<p>4. Now we will create a java file mapping to it.</p>
<p>Create a new class with the name corresponding to the same as the .scenario file<br />
UserWantsToShop.java</p>
<p>Add the following code to it</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.PropertyBasedConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.Scenario</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.ClasspathScenarioDefiner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.PatternScenarioParser</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.ScenarioDefiner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.UnderscoredCamelCaseResolver</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserWantsToShop <span style="color: #000000; font-weight: bold;">extends</span> Scenario <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// The usual constructor with default Configurations.</span>
	<span style="color: #000000; font-weight: bold;">public</span> UserWantsToShop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ShoppingSteps<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Mapping .scenario files to the scenario. This is not by default.</span>
    <span style="color: #000000; font-weight: bold;">public</span> UserWantsToShop<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">ClassLoader</span> classLoader<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PropertyBasedConfiguration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> ScenarioDefiner forDefiningScenarios<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ClasspathScenarioDefiner<span style="color: #009900;">&#40;</span>
                    <span style="color: #000000; font-weight: bold;">new</span> UnderscoredCamelCaseResolver<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.scenario&quot;</span><span style="color: #009900;">&#41;</span>, 
                    <span style="color: #000000; font-weight: bold;">new</span> PatternScenarioParser<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>, classLoader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> ShoppingSteps<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>5. Just that we now have steps we need some place to put the logic for the Steps in the Scenarios. So we create a new Steps class ShoppingSteps.java</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.annotations.Given</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.annotations.Then</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.annotations.When</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.steps.Steps</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShoppingSteps <span style="color: #000000; font-weight: bold;">extends</span> Steps <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>6. Now you can run the Scenario by running it as a Junit Test.</p>
<p>Following should be the output</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Scenario<span style="color: #339933;">:</span> 
&nbsp;
Given I am not logged in <span style="color: #009900;">&#40;</span>PENDING<span style="color: #009900;">&#41;</span>
When I log in as Shaaf with a password JBehaver <span style="color: #009900;">&#40;</span>PENDING<span style="color: #009900;">&#41;</span>
Then I should see my <span style="color: #0000ff;">&quot;Shopping Cart&quot;</span> <span style="color: #009900;">&#40;</span>PENDING<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>This means that none of the scenario requirements have been implemented as yet. But the test result should be green to show there are no problems with our setup.</p>
<p>Now lets add the implementation to the tests.</p>
<p>7. Add the body to the ShoppingSteps. I have added the comments with the methods.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #666666; font-style: italic;">// Given that a user(param) is loggen in</span>
	@Given<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user $username is logged in&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> logIn<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		checkInSession<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Check if the user is allowed on the server</span>
	@When<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;if user $username is $permission to shop&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> isUserAllowed<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName, <span style="color: #003399;">String</span> permission<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		getUser<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isUserAllowed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>permission<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
       <span style="color: #666666; font-style: italic;">// finally then let him use the shopping cart.</span>
	@Then<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;show $username his Shopping cart&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> getMyCart<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName, <span style="color: #003399;">String</span> cart<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		getUserCart<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>8. Now you should try to run the scenario again. And all of the test should be green. I have not implemented the actual methods so they will be red. <img src='http://www.shaafshah.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2009/08/12/getting-started-with-jbehave-in-8-steps/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Logging with log4J isDebugEnabled</title>
		<link>http://www.shaafshah.com/2009/08/12/logging-with-log4j-isdebugenabled/</link>
		<comments>http://www.shaafshah.com/2009/08/12/logging-with-log4j-isdebugenabled/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 12:39:52 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[best practise]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=248</guid>
		<description><![CDATA[Alot of times I have seen the questions popping up whether to use isDebugEnabled property or not. Arguably most of the times or rather always about performance. Some of the stuff that I feel important about using it follows.]]></description>
			<content:encoded><![CDATA[<p>Alot of times I have seen the questions popping up whether to use isDebugEnabled property or not. Arguably most of the times or rather always about performance. Some of the stuff that I feel important about using it follows.</p>
<p>The answer is simple. It is made to be used. However using it has to be with caution.</p>
<p>For instance<br />
If I am using the following line in my code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am there&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Thats an example of a good practise</p>
<p>However I can really blow it out of proportions if I do the following.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Not good practise</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>log.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am there&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And why exactly is that so. This is because the method debug in the class Category i.e. extended by Logger in the log4j libraries explicitly checks the mode for the logging itself.</p>
<p>Extract from the class org.apache.log4j.Category is below</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>repository.<span style="color: #006633;">isDisabled</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">DEBUG_INT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">DEBUG</span>.<span style="color: #006633;">isGreaterOrEqual</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getEffectiveLevel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
forcedLog<span style="color: #009900;">&#40;</span>FQCN, Level.<span style="color: #006633;">DEBUG</span>, message, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Okay so thats the case where we say dont use it. What about the one where we actually do use the isDebugEnabled property.</p>
<p>For instance you have a large parameter going in the debug.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Bad Practise</span>
 log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;XML &quot;</span><span style="color: #339933;">+</span>Tree.<span style="color: #006633;">getXMLText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In such a case it can clearly be said &#8220;No dont do it!&#8221; If Tree.getXMLText is going to pull out all the hair out of the app then there is no use. As it will be constructed first and if the debug level is not enabled that would be a performance cost.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Good Practise</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>log.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;XML &quot;</span><span style="color: #339933;">+</span>Tree.<span style="color: #006633;">getXMLText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Thus in the example above it would be wiser to use log.isDebugEnabled() just to make sure the mileage or cost stays lesser. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2009/08/12/logging-with-log4j-isdebugenabled/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Calling wsadmin scripts from ant</title>
		<link>http://www.shaafshah.com/2008/12/11/calling-wsadmin-scripts-from-ant/</link>
		<comments>http://www.shaafshah.com/2008/12/11/calling-wsadmin-scripts-from-ant/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 08:17:46 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Administrator]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[IBM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jython]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[websphere]]></category>
		<category><![CDATA[wsadmin]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=148</guid>
		<description><![CDATA[You can simply add the following to a target. For the following wsadmin should be in your PATH env. < exec dir="." executable="wsadmin.bat" logError="true" failonerror="true" output="wsconfig.out" > < arg line="-lang jython -f ../../createQFactory.py"/ > < /exec > All output will be logged to wsconfig.out]]></description>
			<content:encoded><![CDATA[<p>You can simply add the following to a target.<br />
For the following wsadmin should be in your PATH env.</p>
<p>< exec dir="." executable="wsadmin.bat" logError="true" failonerror="true" output="wsconfig.out" ><br />
   < arg line="-lang jython -f ../../createQFactory.py"/ ><br />
< /exec ></p>
<p>All output will be logged to wsconfig.out</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/12/11/calling-wsadmin-scripts-from-ant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Command, Singleton, JMenuItem, JButton, AbstractButton &#8211; One Listener for the app</title>
		<link>http://www.shaafshah.com/2008/11/17/command-singleton-jmenuitem-jbutton-abstractbutton-one-listener-for-the-app/</link>
		<comments>http://www.shaafshah.com/2008/11/17/command-singleton-jmenuitem-jbutton-abstractbutton-one-listener-for-the-app/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 19:17:24 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[AbstractButton]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[GOF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JButton]]></category>
		<category><![CDATA[JMenuItem]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Singleton]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=124</guid>
		<description><![CDATA[Here I would like to demonstrate a simple use of JMenuItems being used with Single Listener for the entire system. A simple sample of use would probably be SingleInstance Desktop Application. Lets see how that is done here. 1. First lets create a OneListener class that should be able to listen to ActionEvents and also [...]]]></description>
			<content:encoded><![CDATA[<p>Here I would like to demonstrate a simple use of JMenuItems being used with Single Listener for the entire system.<br />
A simple sample of use would probably be SingleInstance Desktop Application.</p>
<p>Lets see how that is done here.</p>
<p>1. First lets create a OneListener class that should be able to listen to ActionEvents and also be able to add Commands to itself. Please refer to my previous post on Command,Singleton if you would like to see more about this patterns and there usage.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.shaafshah.jmenus</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.ActionEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.ActionListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.AbstractButton</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Implements the ActionListener and is a Singleton also.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OneListener <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">ActionListener</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> OneListener oneListener <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Holds the list of all commands registered to this listener</span>
	<span style="color: #000000; font-weight: bold;">private</span> ArrayList<span style="color: #339933;">&lt;</span>Command<span style="color: #339933;">&gt;</span> commandList <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// A private constructor</span>
	<span style="color: #000000; font-weight: bold;">private</span> OneListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		commandList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Command<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Ensuring one instance.</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> OneListener getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>oneListener <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>	
			<span style="color: #000000; font-weight: bold;">return</span> oneListener<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">return</span> oneListener <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> OneListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Add the command and add myself as the listener</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addCommand<span style="color: #009900;">&#40;</span>Command command<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			commandList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>command<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">AbstractButton</span><span style="color: #009900;">&#41;</span>command<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addActionListener</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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">// All Events hit here.</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> actionPerformed<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Command<span style="color: #009900;">&#41;</span>e.<span style="color: #006633;">getSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the above code, the addCommand method adds the command Object and adds a listener to it.<br />
Now how is that possible.<br />
Basically because I am categorizing my UI objects as Commands with in the system having some UI. And I am also assuming that these commands are Currently AbstractButton i.e. JMenuItem, JButton. Lets have a look at the Command Interface and its Implementation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Command <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And the implementation, note that the Command is an interface where as the class is still extending a UI object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JMenuItem</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestCmd <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">JMenuItem</span> <span style="color: #000000; font-weight: bold;">implements</span> Command<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> TestCmd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</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>
		OneListener.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addCommand</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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HelloWorld&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Personally don&#8217;t like calling the OneListener in the constructor of the class but just for the sake of simplicity of this post I have left it that way. There are many different ways to omit it.</p>
<p>So the TestCmd is a JMenuItem but is also a Command and thats what the OneListener understands.<br />
As this commads Listener is also OneListener all ActionEvents are thrown there and from there only the Command.execute is called on.</p>
<p>So now you dont have to worry about what listeners are you in. The only thing you know is that when execute is called you need to do your stuff.</p>
<p>You can download the code from <a href='http://www.shaafshah.com/wp-content/uploads/2008/11/jmenus.zip'>Here</a>.</p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/11/17/command-singleton-jmenuitem-jbutton-abstractbutton-one-listener-for-the-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Doing the Locale &#8211; Danmark</title>
		<link>http://www.shaafshah.com/2008/11/12/doing-the-locale-danmark/</link>
		<comments>http://www.shaafshah.com/2008/11/12/doing-the-locale-danmark/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 12:52:08 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[locale]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[utils]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=110</guid>
		<description><![CDATA[The following illustrates how to get the Number format working with a danish locale. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.text.NumberFormat; import java.util.Currency; import java.util.Locale; &#160; &#160; public class TestLocale &#123; &#160; public static void main&#40;String [...]]]></description>
			<content:encoded><![CDATA[<p>The following illustrates how to get the Number format working with a danish locale.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.NumberFormat</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Currency</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Locale</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestLocale <span style="color: #009900;">&#123;</span>
&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><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">// Create a Locale for Danmark</span>
 <span style="color: #003399;">Locale</span> DANMARK <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Locale</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;da&quot;</span>,<span style="color: #0000ff;">&quot;DK&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// get the currency instance for this locale.</span>
 Currency krone <span style="color: #339933;">=</span> Currency.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>DANMARK<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Get a Number format for the locale.</span>
 <span style="color: #003399;">NumberFormat</span> krFormat <span style="color: #339933;">=</span> <span style="color: #003399;">NumberFormat</span>.<span style="color: #006633;">getCurrencyInstance</span><span style="color: #009900;">&#40;</span>DANMARK<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// A symbol for the currency</span>
 <span style="color: #003399;">String</span> symbol <span style="color: #339933;">=</span> krFormat.<span style="color: #006633;">getCurrency</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSymbol</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// A double amount</span>
 <span style="color: #000066; font-weight: bold;">double</span> amount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10000.25</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// print it out after formatting.</span>
 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>krFormat.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>amount<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/11/12/doing-the-locale-danmark/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to read a file from the JAR?</title>
		<link>http://www.shaafshah.com/2008/10/31/how-to-read-a-file-from-the-jar/</link>
		<comments>http://www.shaafshah.com/2008/10/31/how-to-read-a-file-from-the-jar/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 18:57:54 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[utils]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=103</guid>
		<description><![CDATA[Someone just asked me this question today. And I thought might as well put it down for info. 1 2 3 4 public TestReadFileFromJar&#40;&#41; throws FileNotFoundException, IOException &#123; InputStream is = getClass&#40;&#41;.getResource&#40;&#34;txtData/states.properties&#34;&#41;; read&#40;is&#41;; &#125; In the case above txtData is placed in the jar on the root. Remmember to add the path with the /]]></description>
			<content:encoded><![CDATA[<p>Someone just asked me this question today. And I thought might as well put it down for info.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> TestReadFileFromJar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span>, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">InputStream</span> is <span style="color: #339933;">=</span> getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;txtData/states.properties&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        read<span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the case above txtData is placed in the jar on the root. Remmember to add the path with the /</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/10/31/how-to-read-a-file-from-the-jar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Command</title>
		<link>http://www.shaafshah.com/2008/10/31/command/</link>
		<comments>http://www.shaafshah.com/2008/10/31/command/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 18:45:04 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[GOF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=14</guid>
		<description><![CDATA[By using the command pattern you are seperating the operation from the invoking object. And just because of that it becomes easier to change the command without chagning the caller/s. This means that you could use Command pattern when you might have the following situation You want to parameterize objects to perform an action You [...]]]></description>
			<content:encoded><![CDATA[<p>By using the command pattern you are seperating the operation from the invoking object. And just because of that it becomes easier to change the command without chagning the caller/s.<br />
This means that you could use Command pattern when you might have the following situation</p>
<p>You want to parameterize objects to perform an action<br />
You want to specify, execute and queue requests at different times.</p>
<p>Just to quickly start you need a command object, An interface will keep it easy going in this case, thus providing you with the option of extending other classes e.g. Swing MenuItem or Button.<br />
Below the execute Method is the one invoked to do something when this command is called or asked to do its stuff.<br />
Where as the getCommandName is assumed as a unique name how ever I am sure we can always come up with a better implementation for uniqueness.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Command <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCommandName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And example implementation of the Command should look as follows<br />
A Command Name, and and execute Method to tell what happens when this command is called.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ForwardCmd <span style="color: #000000; font-weight: bold;">implements</span> Command <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> COMMAND_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Back&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> BackCmd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCommandName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">return</span> COMMAND_NAME<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Your wish, my command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The command manager is the controller in this case. It registers command objects. the &#8220;registerCommand&#8221; will simply take a command and store it in a list or something alike. This means you could load it out of a jar file, or an xml or path and just pass the object to the &#8220;registerCommand&#8221; AS a command offcourse.</p>
<p>the &#8220;execute&#8221; Command will simply execute the Command passed to it. </p>
<p>And the &#8220;getCommand&#8221; returns a command by looking up a COMMAND_NAME. So if you provide a name to it through you system it should give you an object of type Command and simple pass it to execute. Again this would be a controller logic and not the client one.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><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> AbstractCommandManager <span style="color: #009900;">&#123;</span>
&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> registerCommand<span style="color: #009900;">&#40;</span>Command command<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #003399;">Collection</span> getAllCommands<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <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> execute<span style="color: #009900;">&#40;</span>Command command<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> Command getCommand<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/10/31/command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing the adapter</title>
		<link>http://www.shaafshah.com/2008/10/29/implementing-the-adapter/</link>
		<comments>http://www.shaafshah.com/2008/10/29/implementing-the-adapter/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 18:47:56 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[adapter pattern]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[GOF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=94</guid>
		<description><![CDATA[Typically when implementing an interface you would have to implement all the methods that exist in that interface. A very good example is the MouseListener in the java Swing. When you need to implement more then one method where as typically you might be catching only one of them. Saying that you would also find [...]]]></description>
			<content:encoded><![CDATA[<p>Typically when implementing an interface you would have to implement all the methods that exist in that interface.  A very good example is the MouseListener in the java Swing. When you need to implement more then one method where as typically you might be catching only one of them.  Saying that you would also find a Mouse Adapter provided as well. Some of us use that often. And that is part of the Adapter pattern. It makes life easier for me sometimes.</p>
<p>Adapter a structural pattern will let you adapt to a different environment. The joining between different environment is called Adapter. Thus basically giving others the interface that they expect or vice versa when your program becomes the client.</p>
<p>For example the following class expects that the implementing class should be implementing all three methods.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> RecordListener <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eventPrePerformed<span style="color: #009900;">&#40;</span>RecordEvent recordEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eventPerformed<span style="color: #009900;">&#40;</span>RecordEvent recordEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eventPostPerformed<span style="color: #009900;">&#40;</span>RecordEvent recordEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So lets say our implementing class is a rude one and only wants to implement one method. What do you do as an API designer. hmmm</p>
<p>Thats where we step in with the Adapter.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><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> RecordAdapter <span style="color: #000000; font-weight: bold;">implements</span> RecordListener <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eventPrePerformed<span style="color: #009900;">&#40;</span>RecordEvent recordEvent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eventPerformed<span style="color: #009900;">&#40;</span>RecordEvent recordEvent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eventPostPerformed<span style="color: #009900;">&#40;</span>RecordEvent recordEvent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> MyAdapterImpl <span style="color: #000000; font-weight: bold;">extends</span> RecordAdapter<span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eventPerformed<span style="color: #009900;">&#40;</span>RecordEvent recordEvent<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now the only thing left to do is use the adapter. And override any method that you might need .</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> MyClientClass <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> MyClientClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">addRecordListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MyAdapterImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/10/29/implementing-the-adapter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wasprofile -create -delete</title>
		<link>http://www.shaafshah.com/2008/10/24/wasprofile-create-delete/</link>
		<comments>http://www.shaafshah.com/2008/10/24/wasprofile-create-delete/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 11:38:00 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Administrator]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[IBM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jython]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[wasprofile]]></category>
		<category><![CDATA[websphere]]></category>
		<category><![CDATA[wsadmin]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=91</guid>
		<description><![CDATA[Sometimes you require to do things silently, without any questions asked and &#8220;Just Do It&#8221; attitude is required. I often find my self with this problem. If you want to delete or create a Websphere profile from your command line try the following. (I have tried on RSA only) # deleteing a profile wasprofile -delete [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you require to do things silently, without any questions asked and &#8220;Just Do It&#8221; attitude is required.</p>
<p>I often find my self with this problem.</p>
<p>If you want to delete or create a Websphere profile from your command line try the following. (I have tried on RSA only)</p>
<p># deleteing a profile<br />
<strong>wasprofile </strong>-delete -profileName MyProfile</p>
<p>You should get the following message on deletion</p>
<p><strong>INSTCONFSUCCESS: Success: The profile no longer exists.</strong></p>
<p>Creating a websphere profile</p>
<p><strong>wasprofile </strong>-create -profileName MyProfile -profilePath \<br />
[PROFILE PATH] -templatePath \<br />
[RSA HOME]runtimes\base_v6\profileTemplates\default \<br />
-nodeName [NODE NAME] -cellName [CELL NAME] -hostName [HOSTNAME].</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/10/24/wasprofile-create-delete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Abstract Factory pattern</title>
		<link>http://www.shaafshah.com/2008/10/10/abstract-factory-pattern/</link>
		<comments>http://www.shaafshah.com/2008/10/10/abstract-factory-pattern/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 08:20:10 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[GOF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Singleton]]></category>
		<category><![CDATA[Singleton Pattern]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=76</guid>
		<description><![CDATA[Factories have been a key pattern in building applications, its fascinatingly simple, effective and to the point. When starting to learn a design oriented approach to applications or API, I would always recommend a factory pattern as one of the key starting notes of highlight in your design. So today I am talking about the Abstract Factory [...]]]></description>
			<content:encoded><![CDATA[<p>Factories have been a key pattern in building applications, its fascinatingly simple, effective and to the point. When starting to learn a design oriented approach to applications or API, I would always recommend a factory pattern as one of the key starting notes of highlight in your design.</p>
<p>So today I am talking about the Abstract Factory pattern. Its not an &#8220;abstract&#8221; class or object that you call a pattern. But its a Factory of facotries and that is what exactly makes it so much wordingly abstract. Having &#8220;abstract&#8221; classes is there but just some other side of the coin.</p>
<p><strong>When should I use an Abstract Factory:</strong></p>
<ul>
<li>Independence of how products are created, composed or represented</li>
<li>Should be configurable with one of the multiple families or products</li>
<li>You need enforcable constraints for the products used as a group</li>
<li>You need to reveal only the interfaces of products and not thier implementation as part of a bigger picture.</li>
</ul>
<p>So lets begin with the fun.</p>
<p><strong>This is how I plan to implement it:</strong><br />
Has A:<br />
Product has a Specification<br />
Factory has a Product<br />
FactoryManager has FactoryConstants<br />
FactoryManager has ComputerFactory</p>
<p>Is A:<br />
BFactory is a ComputerFactory<br />
AFactory is a ComputerFactory</p>
<p>Not shown.<br />
ProductA is a Product<br />
ProductB is a Product</p>
<p>Diagram:<br />
<div id="attachment_84" class="wp-caption alignnone" style="width: 310px"><a href="http://www.shaafshah.com/wp-content/uploads/2008/10/abstractfactory.jpeg"><img src="http://www.shaafshah.com/wp-content/uploads/2008/10/abstractfactory-300x180.jpg" alt="AbstractFactory" title="AbstractFactory" width="300" height="180" class="size-medium wp-image-84" /></a><p class="wp-caption-text">AbstractFactory</p></div></p>
<p>Creating a simple factory that returns products.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><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> ComputerFactory <span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> Product<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getProducts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> Product getProduct<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> ProductID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Implementation of the ComputerFactory</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AFactory <span style="color: #000000; font-weight: bold;">extends</span> ComputerFactory <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;A&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> Product<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getProducts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> Product getProduct<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> productID<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>productID<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ProductA<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ProductB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sorry you hit the wrong factory, we closed down in 1600 BC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A register base for factories. Refer to the main method for use later in this post.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> FactoryConstants <span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> A <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> B <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The main Entrant class. the Factory Manager that will give the ComputerFactory resultant. Its assumed to be a Singleton as it registers as a Creator in the system (assumption).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FactoryManager<span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> FactoryManager factoryManager <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">private</span> FactoryManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> FactoryManager getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>factoryManager <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> factoryManager<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">return</span> factoryManager <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FactoryManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> ComputerFactory getFactory<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> factory<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>factory<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">case</span> FactoryConstants.<span style="color: #006633;">A</span><span style="color: #339933;">:</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> IBMFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">case</span> FactoryConstants.<span style="color: #006633;">B</span><span style="color: #339933;">:</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SUNFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
   <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sorry you hit the wrong factory, we closed down in 1600 BC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A main method to test the AbstractFactory</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"> <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><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>FactoryManager.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getFactory</span><span style="color: #009900;">&#40;</span>FactoryConstants.<span style="color: #006633;">A</span><span style="color: #009900;">&#41;</span>.<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>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>FactoryManager.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getFactory</span><span style="color: #009900;">&#40;</span>FactoryConstants.<span style="color: #006633;">B</span><span style="color: #009900;">&#41;</span>.<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>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>FactoryManager.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getFactory</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>.<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>
&nbsp;
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You can find the complete code listing here:<br />
<a href='http://www.shaafshah.com/wp-content/uploads/2008/10/abstractfactory.zip'>AbstractFactory source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/10/10/abstract-factory-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
