Singleton Deployment Alternative

The standard method for implementing a singleton design template is as follows:

public class Singleton { private static Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } private Singleton() {} } 

I was wondering if it can be implemented like this:

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

and if so, which version is better?

+7
java design-patterns
source share
4 answers

None. In both cases, a trusted consumer can invoke a private constructor through reflection. Another problem is that this implementation doesn’t work well with serialization unless you take additional steps to create it (by default, if you use the naive approach, every time Singleton deserialized, it will create a new instance).

The correct solution is to use enum , which defines a single value.

 public enum Singleton { INSTANCE; // methods } 

From efficient Java:

Although this approach has not yet been adopted, a singleton enumeration type is the best way to implement singleton code.

+10
source share

Why don't you use enumeration to implement Singleton?

  public enum SingletonEnum { Instance; private static String testStr = ""; public static void setTestStr(String newTestStr) { testStr = newTestStr; } public static String getTestStr() { return testStr; } public static String sayHello(String name) { return "Hello " + name; } } 
+1
source share

In my opinion, at first it’s better, since it looks more oriented towards an object-oriented approach.

0
source share

Although there is nothing particularly bad with any solution, this Wikipedia solution should give you better compatibility and give you a thread safe singleton:

University of Maryland Computer Science Researcher Bill Pugh has written about code issues that underlie the Singleton pattern when it is implemented in Java. [11] Pew's efforts on the “Double Locked Check Idiom” led to changes in the Java memory model in Java 5 and what is generally regarded as the standard method for implementing singletones in Java. The method, known as on-demand idiomization, is as lazy as possible and works in all known versions of Java. It takes advantage of the language guarantees of class initialization and therefore will work correctly in all compilers and virtual machines compatible with Java. A nested class is referenced no earlier (and therefore loaded no earlier than the class loader) than the moment getInstance () is called. Thus, this solution is thread safe, without requiring special language constructs (i.e. Volatile or synchronized).

 public class Singleton { // Private constructor prevents instantiation from other classes private Singleton() { } /** * SingletonHolder is loaded on the first execution of Singleton.getInstance() * or the first access to SingletonHolder.INSTANCE, not before. */ private static class SingletonHolder { public static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } } 
0
source share

All Articles