Singleton — The beauty of Single Instance of the Class

Arosha Perera
3 min readDec 29, 2021

What is Singleton?

Let’s start with a simple definition of Singleton Design Pattern.

This simplest, meaningful design pattern comes under Creational Design Pattern. In this design pattern, the single class is responsible for creating only one instance at a time.

What does it mean? What is the use of this design pattern?

Well, as I said before, in this design pattern, the class is responsible for creating only one instance at a time. So that the class doesn’t allow to create multiple instances at once. This process mainly effects the memory allocation of the class. Which means the object can be created at once no matter how much request are coming. So that only one instance is saved in the memory at each request. And that instance is reused again and again.

Let’s explain this via an example.

Suppose, we need to create a student class which gets student details as user input and prints those student details when needed. Following example clearly shows how to implement the following scenario using Singleton Design Pattern. There are two forms of this design pattern. One is Lazy Instantiation, which is responsible for creating the instance when needed. (I’ve used Lazy Instantiation to explain this design pattern via a simple example) And other one is Eager Instantiation, which is responsible for creating the instance at load time.

Student Class

From the above code segment, we’re creating Student class and make it’s constructor as private. Then creating a static instance (obj) of itself.

Then we create a static method (getInstance) to get the instance of this class to be referenced by other classes. Inside this getInstance method, you can see that first it checked whether the instance is created or not (or the instance is null), then if it’s null or not created, we make this class as synchronized or locking the class once and then checked again if the instance is created or not (or the instance is null). If we get the result again as null or not created, then we create the new instance. This special method is referred to as “Double Checked Locking”. Then we’ll create the main class as Singleton to refer this Student class and it’s methods. Following code segment clearly explains how to do this task.

Singleton Class

Let’s run this program and see whether we got the right answer according the the explanation given for this design pattern.

Result

If you carefully check this above result, the one which I highlighted in red color, you can see that the instance is created once. (To check this, I’ve printed “Instance Created” inside the private constructor of the Student class) So if we try to create the instance of the Student class more than once (from the above example Singleton class, I’ve tried to create the instance of the Student class more than once as student and student1), it will create the instance once and that instance is referenced more than once. Then I’ve used setStudentDetails method to insert student details manually and then the inserted details can be viewed using getStudentCGPA method.

One more important thing about this design pattern is it can be used only in some special areas such as Database Applications, Logging, Thread Pools etc.

Further References:

That’s it for now. Feel free to comment if you have any doubts or if anything to add.

--

--