<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Singleton in Java &#8211; the proper way</title>
	<atom:link href="http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/feed/" rel="self" type="application/rss+xml" />
	<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/</link>
	<description>From programming to day dreaming</description>
	<lastBuildDate>Fri, 06 Nov 2009 16:45:38 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Singletons, java 5 et enum</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-239</link>
		<dc:creator>Singletons, java 5 et enum</dc:creator>
		<pubDate>Fri, 11 Sep 2009 13:50:21 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-239</guid>
		<description>[...] Singleton in Java – the proper way ]   Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] Singleton in Java – the proper way ]   Share and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jirka</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-129</link>
		<dc:creator>Jirka</dc:creator>
		<pubDate>Mon, 04 May 2009 21:41:48 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-129</guid>
		<description>Fausto Mancini,

your quote says that one should use enums anytime one needs &quot;a fixed set of constants&quot;. 

Well, singleton is a pretty fixed set isn&#039;t it? Actually, in Set theory, the term &quot;singleton&quot; reffers to a set with one member.</description>
		<content:encoded><![CDATA[<p>Fausto Mancini,</p>
<p>your quote says that one should use enums anytime one needs &#8220;a fixed set of constants&#8221;. </p>
<p>Well, singleton is a pretty fixed set isn&#8217;t it? Actually, in Set theory, the term &#8220;singleton&#8221; reffers to a set with one member.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fausto Mancini</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-116</link>
		<dc:creator>Fausto Mancini</dc:creator>
		<pubDate>Wed, 25 Feb 2009 11:20:09 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-116</guid>
		<description>mmh...
I can use some books on my chair to lift up my sitting... But that&#039;s not their purpose (isn&#039;t it?). Using enum for a singleton is not a more elegant way I think... It&#039;s only an escamotage...

From http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

&quot;So when should you use enums? Any time you need a fixed set of constants. That includes natural enumerated types (like the planets, days of the week, and suits in a card deck) as well as other sets where you know all possible values at compile time, such as choices on a menu, rounding modes, command line flags, and the like. It is not necessary that the set of constants in an enum type stay fixed for all time. The feature was specifically designed to allow for binary compatible evolution of enum types. &quot;

That&#039;s why enums doesn&#039;t fit for singleton...</description>
		<content:encoded><![CDATA[<p>mmh&#8230;<br />
I can use some books on my chair to lift up my sitting&#8230; But that&#8217;s not their purpose (isn&#8217;t it?). Using enum for a singleton is not a more elegant way I think&#8230; It&#8217;s only an escamotage&#8230;</p>
<p>From <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html" rel="nofollow">http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html</a></p>
<p>&#8220;So when should you use enums? Any time you need a fixed set of constants. That includes natural enumerated types (like the planets, days of the week, and suits in a card deck) as well as other sets where you know all possible values at compile time, such as choices on a menu, rounding modes, command line flags, and the like. It is not necessary that the set of constants in an enum type stay fixed for all time. The feature was specifically designed to allow for binary compatible evolution of enum types. &#8221;</p>
<p>That&#8217;s why enums doesn&#8217;t fit for singleton&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-110</link>
		<dc:creator>Rick</dc:creator>
		<pubDate>Wed, 14 Jan 2009 19:16:17 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-110</guid>
		<description>Call me blasphemous but I think Dependency Injection is a bit over-hyped. So many people dis on a simple singleton because they heard &quot;you shouldn&#039;t use it&quot; without even understanding why. They aren&#039;t that bad. Most people using DI aren&#039;t EVERY even swapping out implementations of their classes.. they just it because they can, and it solved nothing but actually in some cases make things more difficult (just one more place to have to figure out where I&#039;m getting things from.) 

I&#039;m not against DI, but use it because it makes sense to do so - ie you plan to swap out in testing and use different implementations of your interfaces. I&#039;m curious what % of people are doing this? I test my services and daos but I&#039;m not testing  mock ones.. I want to test the real thing. 

For the poster commenting about static methods instead, there is a big difference: If I uses singletons I can implement an Interface and thus provide a different implementation of my singleton if need be. You can&#039;t do that with a class with static methods.</description>
		<content:encoded><![CDATA[<p>Call me blasphemous but I think Dependency Injection is a bit over-hyped. So many people dis on a simple singleton because they heard &#8220;you shouldn&#8217;t use it&#8221; without even understanding why. They aren&#8217;t that bad. Most people using DI aren&#8217;t EVERY even swapping out implementations of their classes.. they just it because they can, and it solved nothing but actually in some cases make things more difficult (just one more place to have to figure out where I&#8217;m getting things from.) </p>
<p>I&#8217;m not against DI, but use it because it makes sense to do so &#8211; ie you plan to swap out in testing and use different implementations of your interfaces. I&#8217;m curious what % of people are doing this? I test my services and daos but I&#8217;m not testing  mock ones.. I want to test the real thing. </p>
<p>For the poster commenting about static methods instead, there is a big difference: If I uses singletons I can implement an Interface and thus provide a different implementation of my singleton if need be. You can&#8217;t do that with a class with static methods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viktoras Agejevas</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-83</link>
		<dc:creator>Viktoras Agejevas</dc:creator>
		<pubDate>Mon, 29 Sep 2008 11:03:22 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-83</guid>
		<description>Hi, J.F. Zarama,

I think it is safe if you&#039;re not using multiple calssloaders. 

As far as comments and ideas: 

1. I think it would be better (from design perspective) to have a separate factory (which would serve one purpose) and four separate enum singletons (also one purpose each). Of course all four objects should be polymorphic, in other case I don&#039;t see any point in having a factory at all.

2. Your enum doesn&#039;t use constructor parameters, so you can leave out the &#039;()&#039; near instances, like KEYVALUEPAIRS {...

3. Idea from 2. - you can create needed objects in enum constructor like:
[sourcecode language=&#039;java&#039;]
public enum ZFactory { 
    ...
    KEYVALUEPAIRS(new KeyValuePairs()),
    JTABBEDPANE(new JTabbedPane());

    ZFactory(Object obj) {
        this.obj = obj;
    }

    private Object obj;

    public Object get() {
        return obj;
    }
}
[/sourcecode]
And of course it&#039;s better to refactor it to 1. with separate factory.</description>
		<content:encoded><![CDATA[<p>Hi, J.F. Zarama,</p>
<p>I think it is safe if you&#8217;re not using multiple calssloaders. </p>
<p>As far as comments and ideas: </p>
<p>1. I think it would be better (from design perspective) to have a separate factory (which would serve one purpose) and four separate enum singletons (also one purpose each). Of course all four objects should be polymorphic, in other case I don&#8217;t see any point in having a factory at all.</p>
<p>2. Your enum doesn&#8217;t use constructor parameters, so you can leave out the &#8216;()&#8217; near instances, like KEYVALUEPAIRS {&#8230;</p>
<p>3. Idea from 2. &#8211; you can create needed objects in enum constructor like:</p>
<pre class="brush: java;">
public enum ZFactory {
    ...
    KEYVALUEPAIRS(new KeyValuePairs()),
    JTABBEDPANE(new JTabbedPane());

    ZFactory(Object obj) {
        this.obj = obj;
    }

    private Object obj;

    public Object get() {
        return obj;
    }
}
</pre>
<p>And of course it&#8217;s better to refactor it to 1. with separate factory.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J.F. Zarama</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-82</link>
		<dc:creator>J.F. Zarama</dc:creator>
		<pubDate>Sun, 28 Sep 2008 17:11:32 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-82</guid>
		<description>To use or not use a Sigleton seems to generate lots of discussion. 

For a Swing app I needed single-instances of certain classes at the application level.

I use Guice for other apps but guice.jar is 544k; I used pico also but pico.jar is 249k so I decided on an enum-based class to implement this Singleton/Cache function at the application level.

Enclosed below is the class I am using now; it works but I do not know how safe it is and whether it is useful/safe in an environment such as a servlet for application-level objects: any comments, ideas; thanks for the post:
[sourcecode language=&#039;java&#039;]
public enum ZFactory {

    KEYVALUEPAIRS() {
        public Object get() {
            if (kvp == null) kvp =  new KeyValuePairs();
            return kvp;
        }
    },
    ENCODERDECODER() {
        public Object get() {
            if (ed == null)   ed =  new EncoderDecoder();
            return ed;
        }
    },
    BARCODEIMAGE() {
        public Object get() {
            if (bci == null) bci =  new BarCodeImage();
            return bci;
        }
    },
    JTABBEDPANE() {
        public Object get() {
            if (tp == null)   tp =  new JTabbedPane();
            return tp;
        }
    };
    
    protected KeyValuePairs kvp = null;
    protected EncoderDecoder ed = null;
    protected BarCodeImage  bci = null;
    protected JTabbedPane    tp = null;

    public abstract Object get();
}[/sourcecode]</description>
		<content:encoded><![CDATA[<p>To use or not use a Sigleton seems to generate lots of discussion. </p>
<p>For a Swing app I needed single-instances of certain classes at the application level.</p>
<p>I use Guice for other apps but guice.jar is 544k; I used pico also but pico.jar is 249k so I decided on an enum-based class to implement this Singleton/Cache function at the application level.</p>
<p>Enclosed below is the class I am using now; it works but I do not know how safe it is and whether it is useful/safe in an environment such as a servlet for application-level objects: any comments, ideas; thanks for the post:</p>
<pre class="brush: java;">
public enum ZFactory {

    KEYVALUEPAIRS() {
        public Object get() {
            if (kvp == null) kvp =  new KeyValuePairs();
            return kvp;
        }
    },
    ENCODERDECODER() {
        public Object get() {
            if (ed == null)   ed =  new EncoderDecoder();
            return ed;
        }
    },
    BARCODEIMAGE() {
        public Object get() {
            if (bci == null) bci =  new BarCodeImage();
            return bci;
        }
    },
    JTABBEDPANE() {
        public Object get() {
            if (tp == null)   tp =  new JTabbedPane();
            return tp;
        }
    };

    protected KeyValuePairs kvp = null;
    protected EncoderDecoder ed = null;
    protected BarCodeImage  bci = null;
    protected JTabbedPane    tp = null;

    public abstract Object get();
}</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gunni Rode</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-74</link>
		<dc:creator>Gunni Rode</dc:creator>
		<pubDate>Wed, 27 Aug 2008 10:23:33 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-74</guid>
		<description>Well... 

Actually, ahem, I also came up with the idea of using a single enum constant for the Singleton implementation in Java 5/6 around May 2007 during the writing of my Master Thesis, which has been publicly available on the net since January 2008. Well before Bloch published the second version of his excellent book (which I have not read yet!) - though he most certainly thought of it looong before, I know! :)

So, in my megalomania I dubbed this as the Singleton-as-Single-Constant idiom... :) 

See for yourself at http://www.rode.dk/thesis

Regards - Gunni

(flaming commence...)</description>
		<content:encoded><![CDATA[<p>Well&#8230; </p>
<p>Actually, ahem, I also came up with the idea of using a single enum constant for the Singleton implementation in Java 5/6 around May 2007 during the writing of my Master Thesis, which has been publicly available on the net since January 2008. Well before Bloch published the second version of his excellent book (which I have not read yet!) &#8211; though he most certainly thought of it looong before, I know! :)</p>
<p>So, in my megalomania I dubbed this as the Singleton-as-Single-Constant idiom&#8230; :) </p>
<p>See for yourself at <a href="http://www.rode.dk/thesis" rel="nofollow">http://www.rode.dk/thesis</a></p>
<p>Regards &#8211; Gunni</p>
<p>(flaming commence&#8230;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ilikeblues.com &#187; Archivo del Blog &#187; Singleton en Java</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-73</link>
		<dc:creator>ilikeblues.com &#187; Archivo del Blog &#187; Singleton en Java</dc:creator>
		<pubDate>Mon, 18 Aug 2008 10:20:39 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-73</guid>
		<description>[...] [Este artículo es una traducción de Singleton in Java - the proper way.] [...]</description>
		<content:encoded><![CDATA[<p>[...] [Este artículo es una traducción de Singleton in Java - the proper way.] [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viktoras Agejevas</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-70</link>
		<dc:creator>Viktoras Agejevas</dc:creator>
		<pubDate>Thu, 14 Aug 2008 06:01:39 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-70</guid>
		<description>jianwu_chen, it&#039;s not my idea, it&#039;s Joshua Bloch&#039;s :)

I agree about the source code confusion, but we have comments for that.

And about &quot;this kind of singleton is no different&quot; is not true, it&#039;s different. For classic Singleton to be serialiazable you have to implement Serializable and declare all instance fields transient and provide a readResolve method. In other case after serialization deserialization you will get a new instance.

With enum approach you don&#039;t have to think about it.</description>
		<content:encoded><![CDATA[<p>jianwu_chen, it&#8217;s not my idea, it&#8217;s Joshua Bloch&#8217;s :)</p>
<p>I agree about the source code confusion, but we have comments for that.</p>
<p>And about &#8220;this kind of singleton is no different&#8221; is not true, it&#8217;s different. For classic Singleton to be serialiazable you have to implement Serializable and declare all instance fields transient and provide a readResolve method. In other case after serialization deserialization you will get a new instance.</p>
<p>With enum approach you don&#8217;t have to think about it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jianwu_chen</title>
		<link>http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/#comment-69</link>
		<dc:creator>jianwu_chen</dc:creator>
		<pubDate>Thu, 14 Aug 2008 05:52:38 +0000</pubDate>
		<guid isPermaLink="false">http://electrotek.wordpress.com/?p=74#comment-69</guid>
		<description>Creative idea. 

But as what others mentioned to use an enum represent something which is not an enum will obfuscate the code. Cause un-necessary confusion for the source code reader.

Another issue is this kind of singleton is no different as using class with static members. 

One reason why we use singleton is to make the class lazy instantiate to break possible recursive initialization of classes because of cross dependencies. Singleton objects should be created only when it&#039;s used rather than when the class is loaded.</description>
		<content:encoded><![CDATA[<p>Creative idea. </p>
<p>But as what others mentioned to use an enum represent something which is not an enum will obfuscate the code. Cause un-necessary confusion for the source code reader.</p>
<p>Another issue is this kind of singleton is no different as using class with static members. </p>
<p>One reason why we use singleton is to make the class lazy instantiate to break possible recursive initialization of classes because of cross dependencies. Singleton objects should be created only when it&#8217;s used rather than when the class is loaded.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
