Implementing Singleton with an Enum (in Java)

前端 未结 6 819
南笙
南笙 2020-11-22 11:44

I have read that it is possible to implement Singleton in Java using an Enum such as:

public enum MySingleton {
     INSTANCE;   
}         


        
6条回答
  •  野性不改
    2020-11-22 12:39

    In this Java best practices book by Joshua Bloch, you can find explained why you should enforce the Singleton property with a private constructor or an Enum type. The chapter is quite long, so keeping it summarized:

    Making a class a Singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type. Recommended approach is implement Singletons by simply make an enum type with one element:

    // Enum singleton - the preferred approach
    public enum Elvis {
    INSTANCE;
    public void leaveTheBuilding() { ... }
    }
    

    This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks.

    While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

提交回复
热议问题