Design Patterns — Zero to Hero — Singleton Pattern

Photo by Luca Bravo on Unsplash

Some example instances are as follows

  • Objects used for logging
  • Objects that act as device drivers to devices like printers and graphic cards
  • Objects containing registry settings and caches related details

Before getting into what is Singleton, We have to know how an object instance of a class can be created?

  • When you are creating a new object of a class that class needs to be public (Public is access modifier; anywhere in the program the class’s object can be created). If the class is not public, only classes in the same package can instantiate it. Even though, those same package classes can create many objects for that class.
  • Therefore, we need to make the constructor of the class as Private (Private can only be accessed within the declared class itself.)
  • If the constructor is private no other classes can instantiate the class. But we can make static methods in that class and try to create a new instance of the class
public Singleton{//Private Constructor
private Singleton(){
}// getInstance method will return Singleton class object
public static Singleton getInstance(){
//Implementation for creating new Singleton class objects}}
Singleton.getInstance();
public Singleton{// This static variable will hold the one instance of the      class Singletonprivate static Singleton uniqueInstance;//Private Constructor
private Singleton(){
}// getInstance method will return Singleton class object
public static Singleton getInstance(){
//Implementation for creating new Singleton class objects if(uniqueInstance == null){
uniqueInstance = new Singleton();
System.out.println("New Singleton Object Instantiated")
}
return uniqueInstance;
}}
New Singleton Object Instantiated
New Singleton Object Instantiated

Following are some of the steps to improve Multi-Threading scenario

  1. Do nothing if the performance is not critical in the program: If you the implemented way is not overhead the program you created, then leave it as it is. No issues. Synchronizing a method would be straightforward and effective. But, synchronizing will reduce the performance of the code. Keep all these in mind and implement your code
public Singleton{// This static variable will hold the one instance of the      class Singletonprivate static Singleton uniqueInstance;     //Private Constructor
private Singleton(){
} // getInstance method is set to synchronized, therfore the Threads will wait till the getInstance method is unlocked by other thread
public static synchronized Singleton getInstance(){
//Implementation for creating new Singleton class objects if(uniqueInstance == null){
uniqueInstance = new Singleton();
System.out.println("New Singleton Object Instantiated")
}
return uniqueInstance; }}
public class Singleton {//This one is a Thread-safe when instantiating class object while assigning it to the static initializer  

private static Singleton uniqueInstance = new Singleton();
private Singleton(){}private static Singleton getInstance(){
return uniqueInstance
}
}
public Singleton{       // Volatile keyword ensures multiple threads handle the variable correctly when it initilized         private volatile static Singleton uniqueInstance;       //Private Constructor
private Singleton(){}
public static Singleton getInstance(){
if(uniqueInstance == null){
synchronized (Singleton.class){ if (uniqueInstance == null){
uniqueInstance = new Singleton();
System.out.println("New Singleton Object Instantiated")
}
} }
return uniqueInstance;
}}

Reference:

--

--

--

A passionate in Tech https://www.linkedin.com/in/parathantl

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How-to sync leads from abandoned forms to Podio

Top Magento Ecommerce Development Company

Bootsgrid is Top Magento Ecommerce development firm for building larger marketplace with all necessary functions.

Refining and Stress Testing my Firing Algorithm

Waterbear Cloud Newsletter, September 2019

My Experience At Hevo - Pavan Kalyan

Getting Started With Kubernetes Operators (Ansible Based) — Part 2

Process transparency for digitization

MongoDB CRUD using flask

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Parathan Thiyagalingam

Parathan Thiyagalingam

A passionate in Tech https://www.linkedin.com/in/parathantl

More from Medium

Factory Method pattern in Java

Naming Conventions in Java

SOLID Principles