Following examples are the implementations of singleton pattern.
- EagerSingleton
It is simplest and thread safe version of Singleton.
However, the instance is created even though application might not be using it.
- LazySingleton
Overcome the drawback of EagerSingleton, but it's NOT thread-safe.
- SynchronizedSingleton
Thread-safe but reduces the performance because of cost associated with the synchronized method.
- DoubleCheckSingleton
Warning: DON'T use it. This implement is incorrect!
- VolatileSingleton
Warning: this implement is limited by JDK version("volatile" semantics guarantee).
Work fine while JDK is not less than 1.5.
- InnerStaticClassSingleton
Use inner static class helpler to initialize instance.
It's prefer to implement Singleton using this approach.
- EnumSingleton
Effective Java suggests to implement singleton using this approach.
However it may occur some memory-waste issues.
Use LazySingleton If there is not thread-safe requirement;
Otherwise use InnerStaticClassSingleton.