What is Singleton pattern and what is the advantage of it.

Verdiyev Mahammad
Star Gazers
Published in
3 min readOct 25, 2020

As we know, design patterns are created to solve a specific problem.

Singleton design pattern is one of the most popular design patterns. It solves the problem of creating only one object of one class and that object can be used where ever required.

Singleton is a creational pattern. This pattern ensures that there is only one instance of a class and provides a global access point for this object.

This pattern should be applied in two cases:

1. When your program needs that no more than one object of a specific class should be created. For instance, say for a Company application, there is only one CEO. If you want to create or access CEO object, you should return the same CEO object every time. Or say for a computer game, this game might have a Hero class and only one Hero object that describes the single hero in the game.

2. When you need to provide a point for global access to an object. In other words, you need to make the object available from anywhere in the program.It’s not enough to simply create a global variable, if it’s not write-protected: anyone can change the variable’s value, so the object’s global access point might be lost. These properties of a Singleton are necessary, for example, when you have an object that works with a database, and you need to access the database from different parts of the program. A Singleton will ensure you that no one can write code that replaces the previously created instance.

So, Singleton fullfill these two needs: there must be only one of a certain kind of object in the program and there must be global access to it.

How Singleton Design Pattern Works ?

Singleton pattern has private constructor, we can access to the constructor only inside a class. So, it restricts to create the object of class outside the class.

Singleton pattern has static member, as static member gets space in memory only once. And this member contains the instance of singleton class.

Singleton pattern has static factory method, which gives global access to the singleton only object. It is responsible for returning object to the caller so that they can use it.

Singleton Pattern with Eager initialization.

In eager initialization instance of Singleton class is created much before it is actually required. Mostly it is done on system startup. This is easy way to create a singleton class but it has a disadvantage that object is created even though your application might not be using it .If this instance is not a big object you can live with it without using, this is the best approach.

Here is the sample code for creating Singleton class with this approach.

Singleton Design Pattern with Lazy Initialization

With lazy initialization you crate instance only when its needed and not when the class is loaded. It is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process, until the first time it is needed . It simply follows the logic of lazy person, he never does any thing until its urgently required :). So you escape the unnecessary object creation.

What are the Benefits of using Singleton Design pattern?

Instance control: Singleton prevents other objects from instantiating their own copies of the Singleton object, ensuring that all objects access the single instance.

Flexibility: Since the class controls the instantiation process, the class has the flexibility to change the instantiation process

Saves Memory: It is obvious that when we create object of any class, it occupies space in memory, so creating many objects means more space is occupied in memory. Singleton class may have only one object, so it saves memory.

--

--

Verdiyev Mahammad
Star Gazers

Highly motivated Data Scientist, Co-Founder of Star Gazers Medium publication