Singleton Design Pattern – A Creational Design Pattern

singleton-design-pattern

singleton design pattern

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. 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.

  • Steps to Create a Singleton Class

    1. Create a Class (You can create a class using C++/Java/.Net programming languages)
    2. Make the default constructor as private, so that no one can create any object using operator new/calloc/malloc operator etc.
    3. 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.
    4. Define a static member function, which can be accessed by other classes to make a call for creating the object of this class.
  • 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.

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

  • 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
  • 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.

 

Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon

Connect

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + 5 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">