<?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; Design</title>
	<atom:link href="http://www.shaafshah.com/tag/design/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>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>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>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>
		<item>
		<title>Quick start Singleton &#8211; Walk through</title>
		<link>http://www.shaafshah.com/2008/06/23/quick-start-singleton-walk-through/</link>
		<comments>http://www.shaafshah.com/2008/06/23/quick-start-singleton-walk-through/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 10:56:37 +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=3</guid>
		<description><![CDATA[This being my first existence on the network and I just want to make sure that I would come back to this blog page again sometime and keep on writing. For now this is a quick 5 min walk through of getting your hands dirty on the Singleton Pattern. As any ones first pattern Singleton [...]]]></description>
			<content:encoded><![CDATA[<p>This being my first existence on the network and I just want to make sure that I would come back to this blog page again sometime and keep on writing. For now this is a quick 5 min walk through of getting your hands dirty on the Singleton Pattern. As any ones first pattern Singleton always seems to be the easiest to adapt and ironically always the mistress of your pains; when you realize the act wasn&#8217;t right in the first place.<br />
More details on that later.</p>
<p>This post should help you to get your hands right on the Singleton Pattern and find the kind there off.</p>
<p>Like any other pattern Singleton also has an objective behind it. What is that?<br />
<strong><br />
Motivation:</strong><br />
A Singleton ensures that a class has only one instance, and provides a global point of access to that class.</p>
<p><strong>Benefits</strong><br />
The very simple benefits of a singleton can be:</p>
<p>* Controlled access<br />
* Permits a variable number of instances<br />
* Reduced name space</p>
<p><strong>When to use:</strong><br />
There must be exactly once instance of a class</p>
<p><strong>How to use: Walk Through</strong></p>
<p>1. Create a class</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;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimplySingleton<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>2. Declare a member variable. This variable will be used for keeping the singleton instance.</p>
<p>It has to be private so that it is not accessible from anywhere else. It has to be static so that it holds only one instance in all entirety.</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;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> SimplySingleton simplySingleton <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>3. Declare a private constructor.</p>
<p>Creating a private constructor would mean no one else can instantiate this class.</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;"><span style="color: #000000; font-weight: bold;">private</span> SimplySingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>4. So now everything seems private how do we access it. Create a global access point.</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;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SimplySingleton getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>How would I access it from outside SimplySingleton.getInstance();</p>
<p>This method should return a SimplySingleton instance.</p>
<p>So here comes the logic to create the one and only instance.</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;"><span style="color: #666666; font-style: italic;">// 4a. is the variable null?</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>simplySingleton <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> simplySingleton<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 4b. if not assign it an instance.</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">return</span> simplySingleton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimplySingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Following is the complete code listing for writing a Singleton.</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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Declaring the class</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimplySingleton <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 1. a private and a static member variable</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> SimplySingleton simplySingleton <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;">// 2. a private constructor</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> SimplySingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 3. a global access point</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SimplySingleton getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// 4a. is the variable null?</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>simplySingleton <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> simplySingleton<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 4b. if not assign it an instance.</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">return</span> simplySingleton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimplySingleton<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>Following are some good resources for in depth peek into the Singleton Pattern.</p>
<p><strong>More Resources:</strong></p>
<p><a title="Singleton Pattern on wikipedia" href="http://en.wikipedia.org/wiki/Singleton_pattern">http://en.wikipedia.org/wiki/Singleton_pattern</a></p>
<p><a title="Implementing the Singleton Pattern in Java" href="http://radio.weblogs.com/0122027/stories/2003/10/20/implementingTheSingletonPatternInJava.html">http://radio.weblogs.com/0122027/stories/2003/10/20/implementingTheSingletonPatternInJava.html</a></p>
<p><a title="OO Design" href="http://www.oodesign.com/singleton-pattern.html">http://www.oodesign.com/singleton-pattern.html</a></p>
<p><a title="SINGLETON PATTERN - 1. THREAD SAFETY" href="http://www.oaklib.org/docs/oak/singleton.html">http://www.oaklib.org/docs/oak/singleton.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2008/06/23/quick-start-singleton-walk-through/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
