Singleton Design Pattern comes under the Creational Design Patterns, it is one of the simplest design pattern in Java. According to singleton pattern class provides same single object for each calls i.e. it is restricting the instantiation of a class to one object and provides a global point of access to that class.
So the class is responsible to create an object and also make ensure that only single object should be created for each client call this object. This class doesn’t allow direct instantiation of object of this class. It allows you to get object instance only by exposed static method.
According to the Gang of Four:
Ensure a class has only one instance and provide a global point of access to it.
This is useful when exactly one object is needed to coordinate actions across the system. You can create single pattern using two forms as listed below:
also read:
Let’s see the following class diagram that illustrates about the Singleton Pattern.
The classes and objects participating in this pattern are:
Singleton (SingletonClass)
Singleton pattern solve only one problem that is if you have some resource that can only have a single instance, and you need to manage that single instance, then you need a singleton. Normally if you want to create database connection with given configuration in the distributed and multi thread environment, so what happens, it might be possible every thread can create new database connection with different configuration object, if you don’t follow the singleton design. With singleton pattern, each thread get same database connection object with same configuration object across the system. It is mostly used in multi-threaded and database applications. It is used in logging, caching, thread pools, configuration settings etc.
I am creating a class with a method the create a instance of this class if one does not exist. If instance is already present here then simply return reference of that object. And also I considered thread safety that is why I have used synchronized block here before creating the object of that class.
SingletonClass.java
package com.doj.patterns.creational.singleton; /** * @author Dinesh.Rajput * */ public class SingletonClass { private static SingletonClass instance = null; private SingletonClass() { } public static SingletonClass getInstance() { if (instance == null) { synchronized(SingletonClass.class){ if (instance == null) { instance = new SingletonClass(); } } } return instance; } public void showMessage(){ System.out.println("Hello Dinesh on Java!!!"); } }
One thing to be noted in the above class code, I have made private constructor of the SingletonClass class, to make sure that there is no way to create the object of that class. This example based on lazy initialization, means that program creates instance on demand at first time. So you could also eagerly instantiate the object to improve the runtime performance of your application.
Let’s see same SingletonClass.java with eager initialization:
package com.doj.patterns.creational.singleton; /** * @author Dinesh.Rajput * */ public class SingletonClass { private static final SingletonClass INSTANCE = new SingletonClass(); private SingletonClass() {} public static SingletonClass getInstance() { return INSTANCE; } public void showMessage(){ System.out.println("Hello Dinesh on Java!!!"); } }
SingletonPatternDemo.java
/** * */ package com.doj.patterns.creational.singleton; /** * @author Dinesh.Rajput * */ public class SingletonPatternDemo { /** * @param args */ public static void main(String[] args) { //The constructor SingleObject() is not visible //SingletonClass object = new SingletonClass(); //Get the only object available SingletonClass object = SingletonClass.getInstance(); //show the message object.showMessage(); } }
Hello Dinesh on Java!!!
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…