Singleton
Singleton provides a way to make sure there is only one instance of a class in one JVM instance.
Because many people used Singleton "everywhere", it became an anti-pattern. Singleton became an anti-pattern because it is difficult to create tests for code that has hundreds of singletons that make mocking really difficult. Another point against singletons are frameworks like Spring can make sure a bean is created only once and we do not need to write any extra code to achieve that.
Before Java had enums, we used to implement singletons to mimic enums.
Example - Thread-safe singleton
Here is an example of thread-safe Singleton. Lets explain key points of this implementation:
volatile is used to keep singleton in main memory, if volatile is not used, it could be in CPU cache, which could cause issues in multi-threaded environment
constructor is private, only the singleton can instantiate it's self
call of the constructor is synchronized on class level, to avoid two threads doing the same thing
Here is how we get the instance of such a singleton.
Hack singleton
Anyway, there is a way to hack singleton and create new instances. We can use reflection and set the constructor as accesible.
Last updated
Was this helpful?