In this blog post we’ll first cover what a singleton design pattern is and where it is used. Then we move on to how you can implement it and show you some possible examples in java.
What is singleton design pattern?
Singleton design pattern is one of the simplest and most known software design patterns used. It lets you create only one instance of a certain class and provides an access point (getter) for the instance. It makes sure that when you access the class (no matter how many times) no other instances of the class are created.
Basically, it is an answer to the question: How can it be ensured that a class has only one instance?
Where is it used?
Singleton design pattern is used whenever you would like to make sure that only one instance of a class gets instantiated. Typically it would be used to control shared resources. Here are some examples of where you could use the pattern:
- When you want to let users write to a single log file (and/or read from it).
- When you have one configuration file for your application you want to read from.
- Read from/write to cache.
- Or lets say you have an application that represents a Household. In case you want to make sure you have only one Cat in your entire Household then the singleton pattern might be a choice for you.
How to implement singleton design pattern?
We have multiple options when it comes to implementing a singleton class. The good thing is that it’s fairly simple to implement. Following is what we basically need to do:
- Hide all constructors of the class by making them private. This prevents the possibility of instantiating the class through the default constructor.
- Define a getter that would return the instance of the class.
- Make sure the class gets instantiated only once.
Lets see some examples of how to achieve it to make it more clear.
Eager initialisation of a singleton class
public class EagerSingleton { private static final EagerSingleton INSTANCE = new EagerSingleton(); // to prevent instantiation through the default constructor private EagerSingleton(){} public static EagerSingleton getInstance(){ return INSTANCE; } }
In this case the instance of the class gets created when the class is loaded. Now lets see what happens when we call the getInstance() method twice:
System.out.println("First call: " + EagerSingleton.getInstance()); System.out.println("Second call: " + EagerSingleton.getInstance());
The output of this would be something like that:
First call: catsincode.EagerSingleton@246b179d Second call: catsincode.EagerSingleton@246b179d
As you can see the objects are the same (they have the same hashcode 246b179d). This is exactly what a singleton class should do.
Lazy initialisation of a singleton class
public class LazySingleton { private static LazySingleton instance; // to prevent instantiation through the default constructor private LazySingleton() {} public static LazySingleton getInstance() { if (instance == null) { instance = new LazySingleton(); } return instance; } }
Here the instance of LazySingleton is not instantiated until we make a call to the method getInstance(). That’s also where the name lazy instantiation comes from. In the getInstance() method we check if we already haven’t created the instance. If this is the case then we create a new instance of the class and return it. Otherwise we return the already created instance.
Please note that this is not thread safe. To make it thread safe consider synchronizing the getInstance() method.
Conclusion
Singleton design pattern makes sure a class is instantiated only once. You have various options to achieve that. For example you could use eager or lazy initialisation of a singleton class.
I hope you have found it useful to read.
If you would like to read more about the singleton design pattern, take a look at wikipedia.
Also, read my blog post about the simple factory pattern to dig deeper into the world of design patterns.
Be the first to reply