Design Patterns : Singleton Pattern

Previous post in the series – Design Patterns : Observer Pattern


In the first post, we went about mentioning some design patterns. In the previous post, we discussed ‘Observer Pattern’ in detail, its usage and implementation. In this post we will discuss ‘Singleton Pattern’. Singleton pattern is one of the simplest design patterns. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

Sometimes, in applications, it is important for some classes to have exactly one instance and this instance must be easily accessible. A global variable would make an object accessible, but it does not prevent from instantiating multiple objects. Hence, we want to make the class itself responsible for keeping track of its sole instance. The class itself would need to ensure that no other instance can be created, and it would need to provide a way to access the instance. This is the crux behind Singleton pattern.

Usage

As a general rule, Singleton pattern must be used when:-

  • there must be a single instance of the class and it must be accessible from a well-defined access point
  • when the sole instance should be extensible by sub-classing

Implementation

This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

Let us create a class called SingleObject which would have its constructor as private and would contain an instance of itself.

SingleObject class provides a static method to get its static instance to outside world. SingletonPatternDemo, the demo class will use SingleObject class to get an instance of SingleObject.

SingleObject.java

public class SingleObject {

   //creating an object of the SingleObject class
   private static SingleObject instance = new SingleObject();

   //making the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}

   //getting the sole object available
   public static SingleObject getInstance(){
      return instance;
   }

   public void showMessage(){
      System.out.println("Hello World!");
   }
}

Getting the sole object from the Singleton class:-

SingletonPatternDemo.java

public class SingletonPatternDemo {
   public static void main(String[] args) {

      //this would be an illegal construct
      //Compile Time Error: The constructor SingleObject() is not visible
      //SingleObject object = new SingleObject();

      //getting the sole object available
      SingleObject object = SingleObject.getInstance();

      //displaying the message
      object.showMessage();
   }
}

Benefits

Singleton design pattern has many benefits:-

  1.  It provides a controlled access to the sole instance: It has strict control over how and when the clients access a Singleton class as it encapsulates its sole instance.
  2.  It allows a reduced namespace: Avoids polluting the namespace with global variable that store sole instances.
  3.  It permits refinement of operations and representation: If the Singleton class is extended, it is easy to configure the application with an instance of this extended class.
  4.  It permits variable number of instances: The same approach (of maintaining a single instance) can be used to control the number of instances that an application uses.
  5.  It is more flexible than class operations: Another way to package the singleton functionality is to use static member functions in C++. But this prevents overriding as the C++ member functions are never virtual.

First post in the series – Design Patterns : Introduction

Previous post in the series – Design Patterns : Observer Pattern

 

Advertisements

One thought on “Design Patterns : Singleton Pattern

  1. Pingback: Design Patterns : Observer Pattern | Harshit Shrivastava

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s