Why Enum Singleton are better in Java
Singleton using Enum in Java
This is the way we generally declare Enum Singleton , it may contain instace variable and instance method but for sake of simplicity I haven't used any, just beware that if you are using any instance method than you need to ensure thread-safety of that method if at all it affect the state of object. By default creation of Enum instance is thread safe but any other method on Enum is programmers responsibility.
/**
* Singleton pattern example using Java Enumj
*/
* Singleton pattern example using Java Enumj
*/
public enum EasySingleton{
INSTANCE;
}
INSTANCE;
}
You can acess it by EasySingleton.INSTANCE, much easier than calling getInstance() method on Singleton.
Read full article from Why Enum Singleton are better in Java
No comments:
Post a Comment