Singleton Design Pattern -A Creational Design Pattern easy way

Singleton Design Pattern -A Creational Design Pattern easy way

Singleton Design Pattern -A Creational Design Pattern easy way As the name suggest, Singleton means ‘Single Instance’.  As it is a creational design pattern so this design pattern provides the solution to create only one object (instance) of a class throughout the application’s life time.That means you can restrict the creation of an object of a given class by providing a static member function to create only one object for that class. 

Singleton Design Pattern -A Creational Design Pattern easy way
Singleton Design Pattern -A Creational Design Pattern easy way

Once the object is created for the first time, then if you will try to create another object for the same class, then it will just return the existing object’s reference (live object’s reference) to you. In this way it can restrict to one object of a particular class. To restrict only one object creation of a class, you have to follow below steps.

his is the article of Singleton Design Pattern -A Creational Design Pattern easy way

Steps to Create a Singleton Class

  1. Create a Class (You can create a class using C++/Java/.Net programming languages)
    1. Make the default constructor as private, so that no one can create any object using operator new/calloc/malloc operator etc.
    2. Use a private  variable of same class type to track if the object is already created or not, Just check the null value of the variable, if it is not null before then just return the existing reference of that object. If the object is not created, then just use normal new operator to create one object of the class for the first time only and then return the reference of the created object.
    3. Define a static member function, which can be accessed by other classes to make a call for creating the object of this class.

his is the article of Singleton Design Pattern -A Creational Design Pattern easy way

Example of Singleton Design Pattern (Singleton Class)

Have a look on  example and read in line comments with code for better clarity. This is java code, you can use same procedure for C++ and .net programming also.

public class SingletonDemo
{  
      //Make the constructor private, so no other call can create object of         	
	  //this class directly using new operator.      

	 private SingletonDemo (){}

	/*Create a private member variable of same class type(SingletonDemo Class here),
    so that we can store the single object value in this variable
    and maintain it throughout the application life time*/

	private static SingletonDemo objSingletonDemo;

    /*Create a static method to access from other classes which returns
	the existing objects reference. Check the null values of the object
	as below code, and if it is null then create the object for the first time
	only and return it. If it is not null, then just return the previous value.*/

    public static SingletonDemo getInstance()
	{
      if(null == objSingletonDemo)
	  {
        objSingletonDemo = new SingletonDemo();
      }
      return objSingletonDemo;
    }

    public void testFun()
	{
      // do something here 
      System.out.println("Hello SingletonDemo....");
    }
}

Now it is time to test the above Singleton Class code.

this is the article of Singleton Design Pattern -A Creational Design Pattern

   // Now it is time to use the above singleton class
    public static void main(String a[])
    {
      // Create an object of the SingletonDemo class by calling getInstance()      //static function of that class and use it's functionality.

        SingletonDemo objSingleTone1 = SingletonDemo.getInstance();
        objSingleTone1.testFun ();

     //Note: If you will call like below, then it will give error message.
     // SingletonDemo objSingletonDemo = new SingletonDemo();
   }

his is the article of Singleton Design Pattern -A Creational Design Pattern easy way

Uses of Singleton design pattern

  1. This design pattern is commonly use for below application types. It holds network specific data and if one server will be down at a particular time, then also it can manage your request with other server at the same time.
  2. If you want to share the current data or update of a object to all other modules, then this singleton design pattern is the best method for it.
  3. You can also develop different types of applications using singleton pattern as mentioned below.
    1. Banking application
    2. Financial application
    3. Travel based application
    4. System services classes
  4. Interview Question on Singleton Design Pattern

I would like to share my real time interview experience, There is 99% chances that you will face a question on Singleton Class in your telephonic interview or face to face interview.  So be ready for the answer. Find some common questions on Singleton pattern or Singleton class.

1. How to create a Singleton class.

Answer: Please check the above code (SingletoneDemo Class) for your reference.

2. How to restrict an application to create an object of that class using new operator.

Answer: By declaring the constructor as private and provide a static method to create new object of that class. Please check the above class (SingletoneDemo Class) for your reference.

3. How to handle only one instance of a class throughout an application.

Answer: By using Singleton Class. Please check the above class (SingletoneDemo Class) for your reference.

Creational Design Patterns

This design pattern provides the thanks to create an object of a category as per your project requirement. It means you’ll control the creation and life cycle of an object in your application. As we’ve already discussed about different creational design patterns in my previous post, lets have a recall about the creational patterns as below then we’ll discuss differing types of creational patterns during this post later.

this is the article of Singleton Design Pattern -A Creational Design Pattern easy way


1. Creational Design Patterns


*Singleton Design Pattern

Singleton Design Pattern ensures that a category has just one instance during a application, and it provides just one point of access (a global point of access) to access the running instance of the thing .

*Factory Pattern


Factory Pattern ensures that you simply can create an object of a category by employing a factory method (a static method) rather than using new operator, in order that you’ll control all objects and memory employed by that specific objects.


*Abstract Factory Pattern


Abstract Factory Pattern may be a set of factory classes from which we will use/implement new factory classes as per our requirements. Then we will derive a replacement class form newly created factory classes rather than directly derived it from Abstract Factory class.


*Builder Pattern


Builder pattern insure that, once we create a posh object, It uses step by step creation process and therefore the final step returns the thing of the category .

his is the article of Singleton Design Pattern -A Creational Design Pattern


*Prototype Pattern


Prototype Pattern insures the cloning properties of an object. The cloned object could be different from the first object. for instance you would possibly copy a document from your friend and alter some points as per your requirement.

his is the article of Singleton Design Pattern -A Creational Design Pattern

FOLLOW US ON FACEBOOK INSTAGRAM TEITTER

Steps to Download Android Apps from Google Play Store

Fully Automatic Washing Machine makes washing easy

Communication Between Service and Activity in Android Example

Mozilla Firefox OS – New Competitor in Smartphone Market

Insert New Contact in Android Code Example

Leave a Comment