QA InfoTech » Business » Singleton concept

Singleton concept

Singleton is an object oriented concept but not well known to most of the developers. Many a times while coding, you come across a requirement wherein you want to restrict the instance of a class to the maximum of one i.e. only one instance of class should exist at a time. Normally we try to resolve this problem through various workarounds. But the perfect solution to this problem is using the “Singleton Class”.

Singleton Class is a class that allows only one instance of itself to be created. It provides global access point throughout the application. It will not allow the object to be created if the one already exists throughout its lifetime.

Singleton class is never instantiated with new method but directly with the class name. It should contain private constructor (to ensure that it is not instantiate in any other way) and a public method that returns the instance.

Any class will be known as Singleton if it satisfies single instance and global access principles.

 

public class Singleton

{

//declaring the unique instance that will exist

private static Singleton uniqueInstance = new Singleton();

//private constructor of the class

private Singleton(){}

//public method that returns instance

public static Singleton getInstance()

{

return uniqueInstance;

}

}

 

Lazy Singleton

In this lazy allocation is done. First the instance is checked if it exists then its reference is returned else the new instance is created.

 

public class Singleton

{

//declaring the unique instance that will exist

private static final Singleton uniqueInstance;

//private constructor of the class

private Singleton(){}

//public method that restricts the duplicate instantiation

public static Singleton getInstance()

{

// if the instance is not yet created, then it creates and returns the new instance

if (uniqueInstance == null)

{

uniqueInstance = new Singleton();

}

//else it just returns the already existing instance

return uniqueInstance;

}

}

Thus from the above code it’s obvious that this class’ object can be created only from one point that is its getInstance() static method and it will be allowed only if no other instance already exists and if it exists, then no other instance will be created but the already existing one is returned. All this is done through:

Singleton singletonObj = Singleton.getInstance();

 

This concept is used in LoadBalancing.

Load Balancing machines implement Singleton concept so that the clients can access its unique instance. Though servers go off or on-line dynamically but there requests should go through the unique object that has knowledge about the state of the web.

Other Examples are:

  • Logger Classes – Instantiate once and then throughout used for logging details.
  • Factories
  • Configuration Classes
  • Shared access to resources

When to use Singleton Class:

  • Singleton is used for state objects i.e. If you are developing an application where the central object’s state matters, Singleton should be used.
  • If your object is heavy and consumes large amount of memory, then its multiple instances should be restricted via Singleton.
  • If you want to prevent multiple instances of an object.

Just in Singleton, you need to design so that it is thread safe if you application is multi-threaded.

Mostly, Singleton is confused with Static Class. But they are different in following aspects:

  • Singleton can implement interfaces while Static can’t.
  • Singleton can be loaded lazily whereas static is initialized whenever it is loaded.
  • Singleton uses Polymorphism concept of Object Oriented whereas static can’t.
  • Singleton objects are stored in heap whereas static objects are stored in stack.

 

A very basic and everyday example of Singleton is:

If you open Notepad++ and write something on it. Now again try to open Notepad++ (i.e. try to instantiate another instance of it), then also it returns the same instance in which you have typed in.

 

About the Author