Singleton design pattern ensure that only one object of a class is created and it provides a way for other classes to access its only object directly without instantiate an object of the class. It is one of the simplest creational design patterns as it provides a way to create only one object of a class.
Sometimes, we want only one instance of a class like when only object is needed for centralized processing of any shared resources. Other examples where singleton classes are useful are “static configuration reader class”, “Logger class”, “Shared database handler” etc.
- Singleton pattern ensure that only one instance of a class is created.
- Single object should be available for use by all classes.
- No class can create an instance(object) of singleton class using new operator.
Implementation of Singleton Design Pattern
The implementation of a singleton pattern is very simple as it involves only one class which is responsible for creating an static member of itself and the returning the reference to the static member. A Singleton class contains a private constructor, a static member of “Singleton” class and a public static method that returns a reference to the static member.
Steps to implement singleton pattern
Let, the name of singleton class is SingletonDemo.
- Define a private static member of “SingletonDemo” class. It will contain the reference of single object.
- Define all constructors of SingletonDemo class as private, so that any class cannot create instance of SingletonDemo using new operator.
- Define a public static factory method, that returns the static single instance of SingletonDemo class to the caller. Other classes can call this method to get an instance of SingletonDemo class.
We can create single instance of a class either at load time(Early initialization) or later when it is required first time (Lazy initialization).
Singleton pattern program in Java using Early Initialization
Creating a Singleton class “SingletonDemo.java”. It creates an object of SingletonDemo during load time.
public class SingletonDemo { // create an object of SingletonDemo // instance will be created at load time private static SingletonDemo singleObj = new SingletonDemo(); // A private constructor private SingletonDemo(){} // A public method to return singleton object to caller public static SingletonDemo getInstance(){ return singleObj; } public void doSomething(){ System.out.println("Single Object of SingletonDemo class"); } }
Creating a Client class “SingletonDemoClient.java” which uses the only instance of SingletonDemo.
public class SingletonDemoClient { public static void main(String[] args) { // get singleton object of SingletonDemo class SingletonDemo instance = SingletonDemo.getInstance(); // call some method of singleton object instance.doSomething(); } }
Output
Single Object of SingletonDemo class
Singleton pattern program in Java using Lazy Initialization
Creating a singleton class “SingletonDemoLazy.java”, which creates an object when it is requested first time.
public class SingletonDemoLazy { private static SingletonDemoLazy singleObj = null; private SingletonDemoLazy(){ } /* It will ceate an instance of SingletonDemo duirng first call, otherwise returns existing object */ public static synchronized SingletonDemoLazy getInstance() { if (singleObj == null) { singleObj = new SingletonDemoLazy(); } return singleObj; } }