3

Since Java 5 it is said that the best way to create a singleton is by a single-element enum type.

Example:

public enum SuperSingleton implements Zooma{
    INSTANCE;

    /**
     */
    public void fightTheBattle(){
        System.out.println("I am fighting the battle!!!");
    }

    @Override
    public void runningWild() {
        //This is method implemented from the Zooma interface.      
    }
}

According to Joshua Bloch, the single-element enum type singleton is;

  • more concise
  • provides the serialization machinery for free
  • and provides an ironclad against multiple instantiation.

I can see how it is more concise and how it provides an ironclad against multiple instantiation, but how does it provide the serialization machinery for free?

Is it something the singleton gets by being an enum?

Koekiebox
  • 5,793
  • 14
  • 53
  • 88
  • 2
    +1 although I think singletons were created by, and I quote Mr Bush, "the enemy". – Augusto Apr 22 '11 at 20:12
  • 1
    The answer is right in the [API](http://download.oracle.com/javase/6/docs/api/java/lang/Enum.html). – mre Apr 22 '11 at 20:18
  • 1
    Please see this question http://stackoverflow.com/questions/70689/efficient-way-to-implement-singleton-pattern-in-java – CoolBeans Apr 22 '11 at 20:20
  • 1
    ok, that was pretty obvious. Thanks guys. – Koekiebox Apr 22 '11 at 20:24
  • 1
    I love enums - but your Singleton won't be able to inherit anymore. @see http://stackoverflow.com/questions/5822827/the-better-java-singleton-pattern-nowadays/5843931#5843931 – simpatico Apr 30 '11 at 18:59
  • I have to disagree with JB here, because it is *he himself* who disagrees with himself. E.g. he says *"don't use interface to declare constants"*, and gives the reason that *"constant use is not an interface, it produces semantic mismatch and noise, and the syntactic sugar gained (no need to use `static final` etc.) is not worth it"*. He uses a similar argument couple of times in EJ2... By analogy, one would say *"don't use enum to declare singleton class; singleton is not an `enum` - it produces semantic mismatch and noise, and the syntactic sugar gained is not worth it"* –  Jul 14 '16 at 10:19

3 Answers3

3

Yes, Enums are all extended off of the Enum class, which implements Serializable.

Mike Yockey
  • 4,565
  • 22
  • 41
  • Wow, that sounds risky, meaning, if the singleton has some kind of state and it's persisted twice in moments, the de-serialization of those objects might be unpredictable, and the Singleton might behave in unexpected ways. – Augusto Apr 22 '11 at 20:18
  • 5
    @Augusto: Enums generally should not have state. You're asking for trouble in various ways if you make them stateful. – ColinD Apr 22 '11 at 20:19
  • 1
    But what you're talking about is an error in the serialization logic, not the support for serialization itself. Singletons by definition may only be instantiated once, which means a proper serialization strategy would only persist it once or overwrite on multiple persistence. – Mike Yockey Apr 22 '11 at 20:21
  • 1
    @ColinD 100% agree, but some singletons need to maintain state... I think what I'm trying to say that the ENUM solution might not be the **best** but a good solution in some cases. – Augusto Apr 22 '11 at 20:21
  • 1
    @Augusto: Using the static instance Singleton pattern for stateful objects or objects that you might want to not use in testing is a bad idea in general, whether you use an enum for it or not. There are lots of simple, stateless uses for a singleton where an enum singleton is nice though. – ColinD Apr 22 '11 at 20:25
  • @Augusto - singletons with anything beyond simple states are a no-no. In 17 years of work I've not seen a legitimate use of a singleton with a complex state *at the language level*. A rare singleton with a legitimate complex state is/should be an infrastructure/runtime artifact (and not an application-level one) that should belong to an underlying runtime (the JVM, a managed container or the OS.) Think java.lang.System or java.lang.Runtime. A singleton with zero or very simple state can be trivially made serializable by simply having serializable attributes (whether it is done as an enum.) – luis.espinal May 31 '11 at 02:15
  • @Augusto - con't. For anything more complex (**and legitimate as part of a well-designed model**), you need to rely on some infrastructure that gives you *singleton* management guarantees (like Spring's singleton deployment mode), **and** the singleton itself must manage its own serialization, most likely on its own custom serialization/activation/passivation lifecycle (as opposed to just simply implementing *java.io.Serializable*.) A lot of elbow grease. In reality, Singleton is abused a lot, and singletons with complex states are much more often than not, a code/modeling smell. – luis.espinal May 31 '11 at 02:20
  • @Augusto - con't - that is, in the general case (which is the one that should count the most), in java, Enum based Singletons are the *best* way to go. So, it is not as you said, that they are just a good solution *in some cases*. It is the best for the majority of cases that legitimately require a true, well-designed singleton. Anything else is a rare edge case, legitimate but rare, or a bad design artifact. – luis.espinal May 31 '11 at 02:23
1

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html

yes :)

Dan
  • 1,002
  • 12
  • 24
1

I'm not 100% sure, but I think if you deserialize a serialized singleton more then once you might end up with more than one instance. An enum instance will always stay a singleton.

So you get 'more serialization' then what you get from just implementing serialization.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348