DEV Community

Nishchya Verma
Nishchya Verma

Posted on

Creational Patterns : Singleton

Creational Patterns

Singleton Pattern

"One of a kind!!"

Singleton pattern is used when we want to create one-of-a-kind objects for which there is only one instance.

Often there are many objects which we just want at most one instance, for eg. Services, Objects that act as drivers or device drivers etc. And if more than one instance of these objects exist we might end up with more problems or weird program behavior.

Why do we need Singleton?

Now I know most of you will be thinking about Global objects, Right?

Well lets discuss why not use global. Suppose you use a global object instead. This global object will be instantiated at the start of your program. Now what if your application never ends up using it? What if we want our object (one of a kind) to be created only when its needed.

Interesting right?
So to conclude why not use global object, we want one-of-a-kind object to be instantiated only when its needed.

Lets get back to Singleton.

Note- The examples discussed will be in Java.

Generally, we create an object using new keyword in Java like new NotSingleton();. However another classes can also create new instances on NotSingleton class.

So how do we implement Singleton pattern?
Lets consider below example which demonstrate Singleton Pattern in action.

public class Singleton() {
  private static Singleton singletonInstance;
  //other variables here

  private Singleton() {}

  public static Singleton getInstance(){
    if(singletonInstance==null) {
      singletonInstance =  new Singleton();
    }
    return singletonInstance;
  }
  //other useful methods here
}
Enter fullscreen mode Exit fullscreen mode

Now lets talk about the above example in details.
Firstly, we have declared a variable singletonInstance of class Singleton type. This static variable will hold one single instance of our Singleton.class.

Secondly, as you may have already noticed the constructor is declared private. Now you might ask well then how are we suppose to create an instance of Singleton class. Well if we think about it we only want Singleton class to be able to create an instance of itself. Since the constructor is private it cannot be accessed from outside the class however we still can access it from the inside.

Now the last part in above example is the answer to the question If Constructor is private how can we create an instance of Singleton class?. Method getInstance() return the only Singleton object ever created inside the Singleton class. If its not already created, it first create an instance, assign it to singletonInstance variable and then return our singletonInstance . And since method Singleton.getInstance() is static, it can be accessed from other parts of our code to get this unique instance of Singleton Class. Also since no public constructor exist we cannot create another instance of Singleton class anywhere else.

Mission: create one-of-a-kind objects: Done!.

Lets summarize what we have learn so far about Singleton. *Singleton pattern ensures that a class has only one instance, and provide a global point of access to it. In other words Singleton pattern is used when we want our object to be unique such that it only have one instance when needed and that unique instance is used throughout our code when required. To achieve this we declare the constructor of such class as private to prevent anyone from making new instance of our Class (ie. new object of same class). So we cannot instantiate a Singleton object we can just ask for its instance. We do that using the *getInstance() method, our global access point.
So if I put it in more simple words, we are taking a class and letting it manage a single instance of itself. We are also preventing any other class from creating a new instance on its own. And to get an instance, you have got to go through the class itself.

Note - I've demonstrated a classic example of singleton class, however if you'll further explore this topic (that I hope you will) you will find that above mentioned example will resemble the actual use case most of the times however when dealing with multithreading we might need to make some changes however the concept will remain the same.

That's all Folks!
Remember,
image
We will be discussing about other Creational patterns in the upcoming posts. Next will be Factory Pattern.

Stay tuned!

Note - Be part of the discussion and also leave a heart if you found it helpful.

Discussion (0)