Singleton Design Pattern

Upanshu Chaudhary
2 min readFeb 24, 2022

Design patterns are important as they help us solve general problems with software that people face during development. These patterns help us make code maintainable, extensible, and loosely coupled. One such design pattern is the Singleton Design pattern.

In simple terms, Singleton is a class that allows us only to create a single instance of it. It makes it impossible for us to instantiate the class for the second time.

There are different Singleton design implementations available like eager Initialization, lazy Initialization, Thread-safe singleton, and many more. Here we will go through the basic lazy Initialization Singleton.

Implementing the Singleton Pattern

The first step towards making a Class Singleton is to define the class constructor as private. This means nobody from outside cannot construct that class. The private constructor will not allow us to create an instance outside the class.

Note: But here is one interesting point about this, only singletons can instantiate singletons.

But this leads to another question. How do we create the Singleton object the first time? And if somehow we are able to create it how do we access it? This problem is solved by creating a static method of the Singleton class which returns a static Singleton object.

public class SingletonExample {    private static SingletonExample singletonExampleInstance = null;    private SingletonExample() {}    public static SingletonExample getSingletonExampleInstance() {        if(singletonExampleInstance == null) {
singletonExampleInstance = new SingletonExample();
}
return singletonExampleInstance;
}
}

Now let’s check if we are able to create a Singleton object.

SingletonExample instanceOne =     SingletonExample.getSingletonExampleInstance();    System.out.println(instanceOne);

SingletonExample instanceTwo = SingletonExample.getSingletonExampleInstance();
System.out.println(instanceTwo);

I have used print statements here, but we should use loggers instead. Nevertheless, our result looks like this:

As we can see the value of the object is the same for both variables.

Why Should We Avoid Using the Singleton Pattern?

  1. A very simple one, globals are bad!
  2. The whole idea of making a single instance of a class may sound good, but there are some things we do not consider while implementing it. We are practically making it impossible for us to create another instance of the class. We are assuming that we are only going to need a single instance and close ourselves to the possibility that in the future we may need some other instance of this class. This may be a classic problem when our application is growing.
  3. It will make unit testing difficult, think about that.

So, I will not say not to use Singleton but instead avoid using Singleton if you can. Use it only if you are 100% that you will never need another instance of that class in the future which I don’t think we can be ever sure of.

That is it. You can check out my other blogs here.

The code snippet for the above example is here.

--

--

Upanshu Chaudhary

Graduate Teaching Assistant and Student at Oregon State University. Open-source enthusiast. Probably busy reading code on a Random github profile.