2

I recently came to know that enum is a more effcient way to implement singleton.

Singleton with enum:

public enum Singleton{
INSTANCE;
    public void doStuff(){
        //......
    }
    public void doMoreStuff(){
        //......
    }
}

Singleton with class:

public class Singleton{
    private static final INSTANCE = new Singleton();

    private Singleton(){}

    public static Singleton getInstance(){
        return INSTANCE;
    }

    public void doStuff(){
        //......
    }
    public void doMoreStuff(){
        //......
    }
}

QUESTION: What are the possible advantages or disadvantages of using enum over class to implement the singleton ?

Govinnage Rasika Perera
  • 2,134
  • 1
  • 21
  • 33
  • 2
    The Gospel on this is from Joshua Bloch: [Item 3: Enforce the singleton property with a private constructor or an enum type](http://www.informit.com/articles/article.aspx?p=1216151&seqNum=3) – Duncan Jones Sep 11 '14 at 06:45
  • 1
    Singletons are evil. Use injection instead. – ethanfar Sep 11 '14 at 06:58
  • @Duncan: so, according to the article, "a single-element enum type is the best way to implement a singleton"? eitanfar: may be, but i need to know which is better. – Govinnage Rasika Perera Sep 11 '14 at 07:23
  • possible duplicate of [What is an efficient way to implement a singleton pattern in Java?](http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) What you're asking about is discussed pretty extensively there. – Jean-François Corbett Sep 11 '14 at 08:28
  • @Jean-François Corbett: Nope, I wanted some counter facts of `enums` as well. So, [this](http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) tell us; there are no options, just use `enums` is it? – Govinnage Rasika Perera Sep 11 '14 at 08:45
  • 1
    Look at all the answers and comments. Various singleton patterns are discussed. – Jean-François Corbett Sep 11 '14 at 08:50
  • An `enum`eration might feel a bit wrong when used for a *single* ton. However, since it’s an established, well-known and accepted pattern, that shouldn’t matter. Hey, we got used to say `print` to transfer data to the standard output stream which is almost never a printer… – Holger Sep 12 '14 at 12:19

2 Answers2

2

As explained by Joshua Bloch, the two approaches are functionally identical if your singleton is not serializable. Although you may wish to add code to your private Singleton constructor to prevent reflection being used to create a second instance.

If your singleton is serializable, then the enum approach will provide all the necessary plumbing for free, whereas with the static field approach, you have to add that yourself.

In my view, there is no downside to adopting the enum approach.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
1

I believe enums are the best way to create singletons. If you are looking for downsides

  1. It is not possible to have lazy load with enums
  2. If you serialize and deserialize the enum, the members in the enums are reset to default value.
  3. If you change your mind and want to make it non-singleton, It is easy with double checked synchronized singleton.
venkatesh
  • 59
  • 4