Enum class implements thread-safe
Singleton:
Singleton pattern is commonly used design patterns, the role is to ensure that a class has only one instance of it, and to provide their own examples of this instance of the entire system. Thread pool, cache, log object, the dialog object, etc. are often designed in one embodiment, select a single embodiment mode to avoid inconsistent state
Simple implementations may refer to Singleton
But to achieve this there are still shortcomings: serialization could undermine Singleton, creates a new instance each time you deserialize a serialized object instance
You can use enumeration to achieve, as follows
public enum SingletonEnum { INSTANCE; private String name; public String getName(){ return name; } public void setName(String name){ this.name = name; } }
Use direct SingletonEnum.INSTANCE来
access, regardless of the issue serialization and reflection.