What are enums and why are they useful?

后端 未结 27 1310
一整个雨季
一整个雨季 2020-11-22 07:06

Today I was browsing through some questions on this site and I found a mention of an enum being used in singleton pattern about purported thread safety benefits

27条回答
  •  终归单人心
    2020-11-22 07:35

    The enum based singleton

    a modern look at an old problem

    This approach implements the singleton by taking advantage of Java's guarantee that any enum value is instantiated only once in a Java program and enum provides implicit support for thread safety. Since Java enum values are globally accessible, so it can be used as the singleton.

    public enum Singleton {
        SINGLETON; 
        public void method() { }
    }
    

    How does this work? Well, the line two of the code may be considered to something like this:

    public final static Singleton SINGLETON = new Singleton(); 
    

    And we get good old early initialized singleton.

    Remember that since this is an enum you can always access to instance via Singleton.INSTANCE as well:

    Singleton s = Singleton.INSTANCE;
    
    Advantages
    • To prevent creating another instances of singleton during deserialization use enum based singleton because serialization of enum is taken care by JVM. Enum serialization and deserialization work differently than for normal java objects. The only thing that gets serialized is the name of the enum value. During the deserialization process the enum valueOf method is used with the deserialized name to get the desired instance.
    • Enum based singleton allows to protect itself from reflection attack. The enum type actually extends the java Enum class. The reason that reflection cannot be used to instantiate objects of enum type is because the java specification disallows and that rule is coded in the implementation of the newInstance method of the Constructor class, which is usually used for creating objects via reflection:
    if ((clazz.getModifiers() & Modifier.ENUM) != 0)
        throw new IllegalArgumentException("Cannot reflectively create enum objects");
    
    • Enum is not supposed to be cloned because there must be exactly one instance of each value.
    • The most laconic code among all singleton realization.
    Disadvantages
    • The enum based singleton does not allow lazy initialization.
    • If you changed your design and wanted to convert your singleton to multiton, enum would not allow this. The multiton pattern is used for the controlled creation of multiple instances, which it manages through the use of a map. Rather than having a single instance per application (e.g. the java.lang.Runtime) the multiton pattern instead ensures a single instance per key.
    • Enum appears only in Java 5 so you can not use it in prior version.

    There are several realization of singleton pattern each one with advantages and disadvantages.

    • Eager loading singleton
    • Double-checked locking singleton
    • Initialization-on-demand holder idiom
    • The enum based singleton

    Detailed description each of them is too verbose so I just put a link to a good article - All you want to know about Singleton

提交回复
热议问题