Singleton Design Pattern.

Gawesh Prabhashwara
4 min readJun 26, 2022

Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.Singleton has almost the same pros and cons as global variables. Although they’re super-handy, they break the modularity of your code.

A lot of developers consider the Singleton pattern an antipattern. That’s why its usage is on the decline in Java code.

Implementation.

Classic Implementation,

Here we have declared getInstance() static so that we can call it without instantiating the class. The first time getInstance() is called it creates a new singleton object and after that it just returns the same object. Note that Singleton obj is not created until we need it and call getInstance() method. This is called lazy instantiation.
The main problem with above method is that it is not thread safe. Consider the following execution sequence.

This execution sequence creates two objects for singleton. Therefore this classic implementation is not thread safe.

make getInstance() synchronized,

Here using synchronized makes sure that only one thread at a time can execute getInstance().
The main disadvantage of this is method is that using synchronized every time while creating the singleton object is expensive and may decrease the performance of your program. However if performance of getInstance() is not critical for your application this method provides a clean and simple solution.

Eager Instantiation,

Here we have created instance of singleton in static initializer. JVM executes static initializer when the class is loaded and hence this is guaranteed to be thread safe. Use this method only when your singleton class is light and is used throughout the execution of your program.

(Best): Use “Double Checked Locking”,

If you notice carefully once an object is created synchronization is no longer useful because now obj will not be null and any sequence of operations will lead to consistent results.
So we will only acquire lock on the getInstance() once, when the obj is null. This way we only synchronize the first way through, just what we want.

We have declared the obj volatile which ensures that multiple threads offer the obj variable correctly when it is being initialized to Singleton instance. This method drastically reduces the overhead of calling the synchronized method every time.

Despite this, there are quite a lot of Singleton examples in Java core libraries:

Singleton (single-threaded).

It’s pretty easy to implement a sloppy Singleton. You just need to hide the constructor and implement a static creation method.

Singleton.java: Singleton
DemoSingleThread.java: Client code
RESULT:

FOO
FOO

Singleton (multithreaded).

The same class behaves incorrectly in a multithreaded environment. Multiple threads can call the creation method simultaneously and get several instances of Singleton class.

Singleton.java: Singleton
DemoMultiThread.java: Client code
RESULT:

FOO
BAR

Thread-safe Singleton with lazy loading.

To fix the problem, you have to synchronize threads during first creation of the Singleton object.

Singleton.java: Singleton
DemoMultiThread.java: Client code
RESULT:

BAR
BAR

Reference :-

Krishantha Dinesh, Head First Design Patterns book (Highly recommended)
https://en.wikipedia.org/wiki/Singleton_pattern, https://refactoring.guru/design-patterns/singleton/java/example#example-2.

Author :-

E-Mail :- gawesh2020java@gmail.com

Blog :- https://gaweshprabhashwara.blogspot.com/

Youtube :- https://www.youtube.com/channel/UCwm7djDtBaueTDqXt_GIFKw

Linkedin :- https://lk.linkedin.com/in/gawesh-prabhashwara-792ab1205

Facebook :- https://www.facebook.com/gawesh98

Twitter :- https://twitter.com/gawesh_98

Instagram :- https://www.instagram.com/gawezh/

Tiktok :- https://www.tiktok.com/@gawesh_prabhashwara?lang=en

--

--