The singleton design pattern is a member of the gang of four, a group of design patterns that solve common problems that programmers often face. At its essence, the pattern is used to ensure that there is only one instance of a class at any given time.
Imagine a grocery store with a long line of cash registers, such as walmart. If there are many customers they will open more lines. Similarly, if you have lots of code, you might create more instances of classes that you are using. Many stores find it costly to have so many lines open, and therefore are transitioning to self checkout. In the same spirit, sometimes you want to limit the number of class instances you have, even reducing it all the way down to one. That is where the singleton design pattern comes into play. Sometimes you need to make sure that there is only one instance of your class at any given time. This can be because it is costly to create them, or something else such as wanting to cache certain data and store it as that object. Regardless of why you want to instantiate at most one instance of an object, this is a common problem, so common in fact that there is a design pattern named after it.
The basic idea behind the various ways to implement a singleton pattern is to first check if there already is an instance created, if there is not then create a new one and return it, if there is then return the one that already exists.
When implementing the pattern the first thing you should do is to have a static member of your class which will be a reference to the instance of your object. We will use this static member to check if there already exists an instance of the class or not. If the variable is null then we need to create one, if it is not null then we simply return it as it is holding the instance already.
The second thing that we need is a constructor. The important thing about the constructor is that it should be private, preventing anyone outside of the class from using it. We need to be in control of how instantiation happens if we are to ensure that only one instance of the class is created.
The third and last thing that is needed is a static method that will be used for creating the instance of the object, that means that instead of calling a constructor directly, we will use a method to create the object.
An implementation of the singleton design pattern can be done as follows:
Only having one instance of an object is a very common problem, and luckily the solution can be quite easy. It can be a bit more tricky if you are wiring multi-threaded code and more than one thread can potentially create an instance of the object. If that is the case, we need to use a lock so that other threads can not access the object or area that we want to use.
A thread-safe implementation can be done as follows:
In conclusion, use the singleton pattern when you want to ensure that only one instance of your class is created. There can be a multitude of reasons why why you want to do this, such as only having one object accessing a database, having a logging class so that only one instance writes to the data file, and if you want to use a class to store data such.