CodeNewbie Community 🌱

Cover image for Java Quickies. Singleton Pattern
Tristan
Tristan

Posted on

Java Quickies. Singleton Pattern

Introduction

  • This series is going to be dedicated to the basic understanding of Java. When ever I find myself asking, "How does this work ?". I will create a blog post and put it here. This series will not be in order so feel free to read whatever post you find most relevant. All my references for this blog post can be found HERE

Singleton Pattern

  • By the time that this blog post is over I want you and I to be able to understand what the singleton pattern in Java is and be able to explain the code below.
public class Singleton {
    private static Singleton uniqueInstance;
 // other useful instance variables here

    private Singleton() {}
    public static Singleton getInstance() {
       if (uniqueInstance == null) {
          uniqueInstance = new Singleton();
       }
       return uniqueInstance;
    }

 // other useful methods here
}
Enter fullscreen mode Exit fullscreen mode

What is the Singleton pattern?

  • Well as Wikipedia states, In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance.

Why use the Singleton pattern?

  • Stated here, A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.

  • Ok now putting the copy pasta and technical definitions aside. We can conclude that to use a singleton pattern means that we only want to create one instance of our class. How can we do that? Well the answer lies in the static keyword and using a private constructor. However, before I can explain more I must establish that you and I have the same level of understanding when it comes to what the static keyword means.

What is the static keyword ?

  • Normally when we create an object each object has its own distinct copy of variables and methods. However, sometimes we want to have variables or methods that are common to all objects, this is where static comes into play.

Static variables

  • A variable that has static as its modifier is called a class variable or a static variable. Theses class variables are commonly used when we want to share a variable across all instances. I should also point out that our class variable private static Singleton uniqueInstance; is static for another reason but more on that later.

Static methods

  • Static methods look like normal methods but they have the static keyword in their declarations. A static method should be invoked by using the class name. So to invoke our static method we would do this, Singleton.getInstance(). Another important thing to note is that static methods can only access other static method/variables, you can NOT access instance method/variables from a static method. You also can not use the this keyword inside of a static method.

  • Now that we both have a basic understanding of how the static keyword works we can move forward.

Main style of the Singleton patter

  • There are a few different versions of the singleton pattern but they all have 3 common attributes:

1) : Private constructor to restrict instantiation of the class from other classes.

2) : Private static variable that is the only instance of the class

3) : Public static method that returns the instance of the class

The private constructor

  • I want to talk about what the private constructor is doing. So when you define a constructor as private, we are shutting off its access to the outside world. This means that we can not create a traditional class through instantiation. So how do we create a new class? With a static method and then we use that method to instantiate a single instance. Now we have to use a static method because the private constructor guarantees that initially there will be no instantiated class. So the only way to access a method is to make it static. As I noted earlier static methods allow us access to them through the class name, Singleton.getInstance()
private Singleton() {}
    public static Singleton getInstance() {
       if (uniqueInstance == null) {
          uniqueInstance = new Singleton();
       }
       return uniqueInstance;
    }
Enter fullscreen mode Exit fullscreen mode
  • As you can see we have our private constructor, the uniqueInstance == null will guarantee that we only have one instance of the class. The new instance is assigned to the private static Singleton uniqueInstance; variable. If you are wondering why it is static, it is because we created the new instance inside of a static method and static methods only have access to static variables. So the static method and variable are all necessary because we have a private constructor.

  • This is just the basics of the singleton pattern and design patterns in general, so I encourage you to read more about them.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Oldest comments (0)