<?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>Christian Schenk&#187; pattern</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Singletons with AspectJ</title>
		<link>http://www.christianschenk.org/blog/singletons-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/singletons-with-aspectj/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 11:18:41 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/singletons-with-aspectj/</guid>
		<description><![CDATA[Although I wouldn't recommend it this post shows you how to implement the Singleton-Pattern with AspectJ 5]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://www.stumbleupon.com/url/www.sophisticated-it.de/blog/index.php/2007/07/17/singleton-pattern-mit-aspectj/">stumbled</a> upon this <a title="Singleton-Pattern mit AspectJ" rel="nofollow" href="http://www.sophisticated-it.de/index.php/2007/07/17/singleton-pattern-mit-aspectj/">post</a> (<em>german</em>) that shows how to implement the singleton pattern with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a>. Although you may want to use <a title="Category: Dependency Injection" href="/blog/tag/dependency-injection/">dependency injection</a> with <a title="Springframework.org" href="http://www.springframework.org/">Spring</a> or <a title="google-guice" href="http://code.google.com/p/google-guice/">Guice</a>, there might be (hopefully rare) occasions on which you opt for this solution. In this post we&#8217;ll check out how to do this with AspectJ 5.</p>
<p>The Eclipse project with the sample code for this post can be downloaded as <a href="http://data.christianschenk.org/singletons-with-aspectj/SingletonWithAspectJ-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/singletons-with-aspectj/SingletonWithAspectJ-1.0.zip">zip</a>; make sure to install the <a title="Eclipse AspectJ Development Tools" href="http://www.eclipse.org/ajdt/">AspectJ Development Tools</a>. You can browse the code online <a title="Singletons with AspectJ - Code Reference" href="http://data.christianschenk.org/singletons-with-aspectj/xref/">here</a>.</p>
<p><span id="more-37"></span></p>
<h2>Classic singleton pattern</h2>
<p>The traditional way to implement a singleton consists of a <code>private</code> constructor, a <code>static final</code> field that holds the instance of the class and a <code>getInstance</code> method that returns this instance. In Java this looks like this:</p>

<div class="wp_syntax"><div 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> Singleton <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Singleton singleton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<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;">private</span> Singleton<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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Singleton 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;">return</span> singleton<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This gets really annoying if you&#8217;ve got this code more than once in your application. So lets have a look at an aspect that resembles this functionality.</p>
<h2>Implementing the aspect</h2>
<p>If you&#8217;ve got more than one singleton in your application you may think of a singleton as a crosscutting concern. Consequently, you&#8217;ll write an aspect that intercepts calls to the constructor of the singleton class and instead of creating a new instance every time you&#8217;ll return the instance that was created at the very fist call to the constructor.</p>
<p>The logic for this idea can be expressed with the following code fragment:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>Class<span style="color: #339933;">&lt;?&gt;</span>, Object<span style="color: #339933;">&gt;</span> singletons<span style="color: #339933;">;</span>
...
<span style="color: #006633;">Class</span><span style="color: #339933;">&lt;?&gt;</span> singletonClass <span style="color: #339933;">=</span> joinPoint.<span style="color: #006633;">getSignature</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDeclaringType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Object</span> singletonObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">singletons</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>singletonClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>singletonObject <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>
  singletonObject <span style="color: #339933;">=</span> joinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">singletons</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>singletonClass, singletonObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">return</span> singletonObject<span style="color: #339933;">;</span></pre></div></div>

<p>We have a map that holds references to all instances of our singleton classes. The method that intercepts the calls to the constructor of a given class stores a reference to the created instance in the map and returns that instance on the following calls to the constructor of the same class.</p>
<p>So, you can do the following in the code and will get references to the same instance of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Singleton s1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Singleton s2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// s1 == s2</span></pre></div></div>

<p>All that&#8217;s left to do, is to define a pointcut and to remove the boilerplate code from the singleton class. A pointcut can look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;call(*.SingletonAop.new(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> singletons<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></div></div>

<h2>Conclusion</h2>
<p>We <a title="Singletons with AspectJ - Code Reference" href="http://data.christianschenk.org/singletons-with-aspectj/xref/">implemented</a> a solution for the singleton pattern with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a> 5. The used approach caches the instances of the singleton classes and returns the cached ones once the constructor has been called for the first time. Note: to simplify matters we left out thread safety.</p>
<p>Although this solution is somehow elegant and may be useful to clean up some code in a legacy system, we should consider the following points:</p>
<dl>
<dt>Can we use a dependency injection framework?</dt>
<dd>Spring uses singletons by default and with Guice you can mark singletons with the <code>@Singleton</code> annotation. So, we might want to choose one of these frameworks instead of implementing this functionality on our own.</dd>
<dt>Java skill of the team?</dt>
<dd>The presented implementation should be well documented, because nothing would be more irritating than a novice developer in your team who doesn&#8217;t know about this <em>magic</em> behind the scenes and relies on different instances of the <em>singleton</em> class (<em>&#8220;I thought <code>new</code> would be doing&#8230;&#8221;</em>).</dd>
</dl>
<p>There are <em>other</em> solutions available to this problem. So, I wouldn&#8217;t recommend to implement this kind of <em>caching</em> with AspectJ.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/singletons-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
