Singleton Design Pattern

Sanduni Jayaweera
3 min readMay 23, 2021
Reference: https://refactoring.guru/design-patterns/singleton

In singleton pattern, one instance is created per JVM instance. That means, how many objects you have tried to create, only one is created.

The Singleton pattern helps to solve 2 problems at one time.

  • Usually we may need to provide access restrictions over different users on same or shared resource. In that case, singleton is very useful since it creates a single object for any call.
  • As well, it provides singleton pattern allows you to access with the created object from anywhere in the application.

In order to make a singleton pattern with the class in Java, we need to comply with the 3 requirements.

  1. The default constructor should be private,
  2. There should be private static variable,
  3. There should be public static method.

Also, there are 2 ways which singleton pattern can be implemented. They are,

  1. Lazy instantiation
  2. Early instantiation

Let’s go with the real time scenario to implement the Singleton Design Pattern.

Case Scenario:

There is a educational web application. But to enter that page you must need to register with this application by providing with your email. So, if you are not enter email then you will get an error message. Which means, whenever a candidate try to go through the site without registering, he/she is enforced by this message and there by navigated back to register.

I created Singleton class as main class and CheckEmail class as singleton implementation class.

Let’s go first with the CheckEmail.

As you can see, to restrict the creation of objects from CheckEmail, the default method is created as private. Then you can not instantiate from CheckEmail. In order to make an instance, we can create the static instance called “email”.

Finally, to get the object, we can create a static method which can be accessed by without creating an object. I called it here as getEmailInstance() and its return type is CheckEmail. In side of this method, we need to check first that is the instance email is already created or not. Because, at the very first time only, we created an object. Otherwise, it returns the already created object which simulate the behavior of Singleton.

In that case, our very first object is created during the run-time inside of our static method. Since it creates on run-time we called it as “Lazy Instantiation”.

Now, we are going to go our main to see how can we deal with that created instance.

Now you can see that we can access with the CheckEmail class without creation of any object. So, multiple objects can access with the same method without creating any object. Let’s prove that.

We create ce1, ce2 by calling directly getEmailInstance(). The output is shown below.

This proves that same instance is retrieved despite how many instances called.

Now let’s see how we can build singleton pattern via “Early Instantiation”. For that what we can do is create an object during creating private static instance.

Alright… now you can get an idea about how singleton can be used.

But ….

Have you ever thought what will happen if this second thread will access to above getEmailInstance() while the very first thread is creating an instance??

To solve such a problems, we can synchronize each thread before going to the object creation. Let’s see how we can do this by simply adding a block of codes.

Now, before creating an instance a thread have to be wait until previous thread finishes its execution.

So, I hope you could get a better understand about the singleton pattern with this real world example that you might face. Let’s discuss about the Factory Method design pattern with my next article by using a real world example.

Stay Safe !!!

References:

--

--