Skip to content

Latest commit

 

History

History

singleton

Following examples are the implementations of singleton pattern.

Singleton Pattern List

  1. EagerSingleton

It is simplest and thread safe version of Singleton.
However, the instance is created even though application might not be using it.

  1. LazySingleton

Overcome the drawback of EagerSingleton, but it's NOT thread-safe.

  1. SynchronizedSingleton

Thread-safe but reduces the performance because of cost associated with the synchronized method.

  1. DoubleCheckSingleton

Warning: DON'T use it. This implement is incorrect!

  1. VolatileSingleton

Warning: this implement is limited by JDK version("volatile" semantics guarantee).
Work fine while JDK is not less than 1.5.

  1. InnerStaticClassSingleton

Use inner static class helpler to initialize instance.
It's prefer to implement Singleton using this approach.

  1. EnumSingleton

Effective Java suggests to implement singleton using this approach.
However it may occur some memory-waste issues.

Summary

Use LazySingleton If there is not thread-safe requirement;
Otherwise use InnerStaticClassSingleton.