What is the use of enum singleton in Java?

When a gang of four introduced a singleton pattern, they also had to explain why not use static classes and methods instead. The reason was: the ability to inherit. For Java, this made sense - we usually cannot inherit the fields and methods of a class.

Later, the book "Effective Java" appeared. And now we know that the existence of a reflex destroys the singularity of a singleton class with a private constructor. And the only way to make a real SINGLEton is to make it as a separate enumeration element. Nice. I did it myself.

But the question remains: although we cannot inherit from the enumeration, what is the use of this singleton? Why don't we use these old good static / cool fields and methods?

Change Thanks to @ bayou.io, I can see that there is code in https://softwareengineering.stackexchange.com/a/204181/44104 that can also fool an enumeration and create two more instances of a singleton enumeration. Other issues are mentioned here. So, there is no need to use enum instead of the usual template of a singleton class? BTW, all the advantages of the enumeration that are mentioned here so far also work for singleton classes.

+6
source share
4 answers

what is the use of this singleton? Why don't we use these old good static / cool fields and methods?

Because enum is an object, so you can not only pass it on, but also implement interfaces.

In addition, since we are creating a class, we can use various public / private parameters available for all classes.

Thus, in practice, we can make a singleton that implements the interface and then passes it in our code, and the call code is not wiser. We can also make the enum class package private, but still pass it to other classes in other packages that expect an interface.

If we used the version of static methods, then the calling class would have to know that this object is single, and our singleton class should be publicly available so that other classes can see it and use its methods.

+2
source

There is nothing special about the fact that “a good old-fashioned singleton”, enumerative “singles” are just convenient - this saves you from having to guess with a code that looks the same in every singelton.

+2
source

For me, a singleton makes sense wherever you want to present something unique of its kind.

As an example, if we want to model the Sun , it cannot be a normal class, because there is only one Sun However, it makes sense to inherit it from the Star class. In this case, I would choose a static instance with a static getter.

To clarify, here is what I am saying:

 public class Star { private final String name; private final double density, massInKg; public Star(String name, double density, double massInKg) { // ... } public void explode() { // ... } } public final class Sun extends Star { public static final Sun INSTANCE = new Sun(); private Sun() { super("The shiniest of all", /**...**/, /**...**/); } } 

Sun can use all Star methods and define new ones. This would not be possible with an enumeration (class extension, I mean).

If you don’t need to model such inheritance relationships, as you said, enum becomes more appropriate, or at least simpler and more understandable. For example, if an application has one ApplicationContext for the JVM, it makes sense to have it as a singleton, and usually it does not need to inherit anything or be extensible. Then I would use enum .

Please note that in some languages, such as Scala, there is a special keyword for singletons ( object ), which not only makes it easy to define single numbers, but also completely replaces the concept of a static method or field.

+1
source
  • ENUM singlets are easy to write. It will take up very little code, which is clean and elegant if you compare it with the implementation of a lazy singlet with double synchronized blocks.

      public enum EasySingleton{ INSTANCE; } 
  • Creating an ENUM instance is thread safe.

  • ENUM single serializations are handled independently.

    regular singletones that implement the Serializable interface no longer remain Singleton, because the readObject() method always returns a new instance, like the constructor in Java. you can avoid this by using the readResolve() method and discarding the newly created instance by replacing Singeton

     private Object readResolve(){ return INSTANCE; } 

Look at the singleton article

0
source

All Articles