Singleton Pattern: The One of a Kind Design Pattern

Reading Time: 3 minutes

Hey, coders in this blog we are going to talk about Singleton Pattern, and believe me, Singleton Pattern is one of the easiest patterns among all the design patterns in terms of its class diagram.

Singleton Pattern has an interesting mechanism in the way it is implemented. So before implementing the singleton pattern in our code first let’s dive into its definition which states that 

“The Singleton Pattern ensures a class has only one instance and provides a global point of access to it.”

There are two things to which the above definition is pointing, let’s break them down:-

  • You have a class and you are letting it manage a single instance of itself and also you are preventing any other class from creating its own new instance, in order to get an instance you have to go through the class itself.
  • Second thing is that you are also providing a global access point to the instance so that whenever you need an instance, just ask the class and it will hand you back the single instance. So from this, we can see that you are creating Singleton in a lazy manner, which is very important for resource-intensive object. 

Now in order to get a more clear picture of the singleton pattern let’s understand the class diagram of the singleton pattern.

Singleton
static Singleton instance
// Other useful Singleton Data …
static Singleton getInstance()
// Other useful Singleton Methods …
Singleton Class Diagram

Singleton Class Diagram

In the above diagram, you have a Singleton class and what goes inside the class is pretty interesting, and here’s a thing the key about the singleton pattern is that we make the constructor of the singleton class “private” which is very unique and the result of this is that from outside people can’t construct a new class and the only singleton can instantiate singleton.

Now you all will be wondering if we can’t make a singleton using the new Singleton, then how on Earth we are gonna create the first singleton and that’s when the Static method comes in.

Static Method is a class method that is under the namespace of singleton class and therefore it has access to a private constructor and therefore you can instantiate like

Singleton.getInstance()

Now as we have understood how the singleton pattern works it’s time to see the pseudo-code:

Pseudo Code -:

public class Singleton {
    static private Singleton instance;
    /*
    static private variable called instance that hold something of type Singleton
     */

    private Singleton() {}
    /*
    private constructor which result's you can't instantiate class from outside
     */

    public static Singleton getInstance() {
        if(instance == null) {
            instance = new Singleton();
        }
        return instance;
        /*
        public static method that return the singleton and is called getInstance
         */
    }
    ...
}

Now as we have an understanding of the singleton pattern works, let’s hop into IntelliJ and try to implement the singleton pattern in our code.

Scala Program using Singleton Pattern

package com.knoldus.singleton

import java.util.Calendar
import java.text.SimpleDateFormat

object DataUtils extends App{
  def getCurrentDate: String = getCurrentDateTime("Monday, July 5")
  def getCurrentTime: String = getCurrentDateTime("10:00 a.m.")

  private def getCurrentDateTime(dateTimeFormat: String): String = {
    val dateFormat = new SimpleDateFormat(dateTimeFormat)
    val cal = Calendar.getInstance()
    dateFormat.format(cal.getTime())
  }
}

Conclusion

Thank you guys for making it to the end of this blog, I hope by reading this you were able to grasp the concept of a singleton pattern, and by now you must have understood why we refer singleton pattern as “one of the kind pattern”. For more information, you can refer to the book Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software.

If you like my blogs please do check my more blogs by clicking here.

Reference

https://www.oreilly.com/library/view/head-first-design/0596007124/

Written by 

Hi community, I am Raviyanshu from Dehradun a tech enthusiastic trying to make something useful with the help of 0 & 1.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading