Search This Blog

Sunday, 15 January 2017

Singleton Design Pattern in Java

Singleton Design Pattern in Java
Intent
Ensure a class only has one instance, and provide a global point of access to it.

Motivation
It's important for some classes to have exactly one instance.
There are some instances in the application where we have to use just one instance of a particular class
A very simple example is Logger, suppose we need to implement the logger and log it to some file according to date time. In this case, we cannot have more than one instances of Logger in the application otherwise the file in which we need to log will be created with every instance.
Singleton pattern is used for logging, drivers objects, caching and thread pool.

Applicability
Use the Singleton pattern when
·         There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
·         When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Consequences
The Singleton pattern has several benefits:
1. Controlled access to sole instance. Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it.
2. Reduced name space. The Singleton pattern is an improvement over global variables. It avoids polluting the name space with global variables that store sole instances.
3. Permits refinement of operations and representation. The Singleton class may be subclassed, and it's easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time.
4. Permits a variable number of instances. The pattern makes it easy to change your mind and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change.

How do we ensure that a class has only one instance and that the instance is easily accessible?
·         A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects.
·         A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created, and it can provide a way to access the instance.

Creating Singleton design pattern
To create the singleton class, we need to have static member of class, private constructor and static factory method.

  • Static member: It gets memory only once because it is static, and the only instance of the Singleton class.
  • Private constructor: It will prevent the instantiation of the Singleton class from outside the class.
  • Static factory method: It will provide the global point of access to the Singleton object and returns the instance to the caller.
Implementation

package com.jasdhir.blog.single;

public class SingletonObject {
      //create an object of SingletonObject class
         private static SingletonObject single = new SingletonObject();

         //make the constructor private so that this class cannot be instantiated
         private SingletonObject(){}

         //Get the only object available
         public static SingletonObject getInstance(){
            return single;
         }
}

package com.jasdhir.blog.single;

public class Main {
public static void main(String[] args) {
      SingletonObject Obj1=SingletonObject.getInstance();
      System.out.println("Object 01 : "+Obj1);
     
      SingletonObject Obj2=SingletonObject.getInstance();
      System.out.println("Object 02 : "+Obj2);
}
}

Output :
Object 01 : com.jasdhir.blog.single.SingletonObject@6d06d69c
Object 02 : com.jasdhir.blog.single.SingletonObject@6d06d69c

Both the objects Obj1 and Obj2 are referring to same instance

No comments:

Post a Comment