<?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>ingokallenbach.de &#187; singleton</title>
	<atom:link href="http://www.ingokallenbach.de/tag/singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ingokallenbach.de</link>
	<description>About Mac OS X Leopard, Java, software development and other stuff...</description>
	<lastBuildDate>Sun, 03 Jan 2010 15:21:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Singleton in Java</title>
		<link>http://www.ingokallenbach.de/2008/08/17/singleton-in-java/</link>
		<comments>http://www.ingokallenbach.de/2008/08/17/singleton-in-java/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 11:53:24 +0000</pubDate>
		<dc:creator>ingo</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.ingokallenbach.de/?p=56</guid>
		<description><![CDATA[Der Artikel Singleton Pattern in Java beschreibt einige Implementierungen des Singletons in Java, bevor am Ende auf die bisher oft angetroffene und favorisierte Lösung eingangen wird. Aber auch diese favorisierte Lösung hat in bestimmten Situationen Nachteile: Bei entsprechend privilegiertem Zugriff &#8230; <a href="http://www.ingokallenbach.de/2008/08/17/singleton-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Der Artikel <a title="Singleton Pattern in Java" href="http://www.theserverside.de/singleton-pattern-in-java">Singleton Pattern in Java</a> beschreibt einige Implementierungen des Singletons in Java, bevor am Ende auf die bisher oft angetroffene und favorisierte Lösung eingangen wird.</p>
<p>Aber auch diese favorisierte Lösung hat in bestimmten Situationen Nachteile:</p>
<ul>
<li>
    Bei entsprechend privilegiertem Zugriff können per Reflection immer noch zusätliche Instanzen der Singleton-Klasse erzeugt werden.
  </li>
<li>
    Soll das Singleton <code>Serializable</code> sein, so müssen alle Felder als <code>transient</code> markiert werden und die Methode <code>readResolve()</code> muss implementiert werden:</p>
<pre class="brush: java; title: ;">
private Object readResolve()
  return INSTANCE; // Just return the single instance..
}
</pre>
</li>
</ul>
<p>Setzt man Java 1.5 (oder später) ein, kann man das Singleton Pattern über einen Enum realisieren! Hier bekommt man eine sehr kleine &quot;Klasse&quot;, die beide obigen Nachteile <strong>nicht</strong> hat!</p>
<pre class="brush: java; title: ;">
public enum Singleton {
  // Guaranteed to be the single instance
  INSTANCE;

  public void doSomething() {...}
}
</pre>
<p>Diese Art des Singletons ist laut Item 3 in Effective Java die beste Art das Singleton Pattern in Java zu implementieren.</p>
<h3>Literatur</h3>
<h4>Bücher</h4>
<ul>
<li>
    BLOCH, J.: <em><a href="http://java.sun.com/docs/books/effective">Effective Java</a></em>. Addison-Wesley, 2nd Edition, 2008.
  </li>
</ul>
<h4>Links</h4>
<ul>
<li>
    <em><a href="http://www.ibm.com/developerworks/java/library/j-dcl.html">Double-checked locking and the Singleton pattern</a></em>. Mai 2002.
  </li>
<li>
    <em><a href="http://www.theserverside.de/singleton-pattern-in-java">Singleton Pattern in Java</a></em>. August 2006.
  </li>
</ul>
<div class="shr-publisher-56"></div>]]></content:encoded>
			<wfw:commentRss>http://www.ingokallenbach.de/2008/08/17/singleton-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

