Site icon TestingDocs.com

How to create a Singleton class?

Overview

Using the Singleton pattern, you should have only one object of a particular class throughout your code. The Singleton class design is to make sure that you can instantiate only one object of a particular class. You need to restrict the new operator by creating more objects than 1 for the Singleton class.

Let’s see how we can achieve this with step by step approach.

Sample class

public class Singleton {
 // Only one object instance is allowed for this class.
 private String name;

public String getName(){
 return name;
 }

public Singleton(String name){
 this.name = name;
 }

public void display() {
 System.out.println("This is singleton "+ name);
 }
}

 

Try to use the singleton and obvious problem:

public class UseSingleton {
// This class will try to use singleton class
 public static void main(String[] args) {
 Singleton s1 = new Singleton("s1");
 Singleton s2 = new Singleton("s2");
 // Oops.. we have created 2 objects...
 s1.display();
 s2.display();
 }
}

 

You need to avoid creating a new object each time with the new operator on your class.Make the constructor of Singleton private. You can block the use of the new operator from any code that is not inside the Singleton class. Create a method to create one object and return the instance.

private Singleton(String name){
    this.name = name;
  }

 

Calling the getInstance method gives you an instance of Singleton class.

public static Singleton getInstance(String name)
  {
    if (singleInstance == null){
      singleInstance = new Singleton(name);
    }
    return singleInstance;
  }

 

Another aspect to keep in mind is that , when two or more threads invoke the getInstance at the same time, it is possible for both threads will create Singleton objects.

Synchronization

You can use to restrict access to getInstance to one thread at a time using Synchronization. Using the synchronized blocks access to the getInstance method, any new threads attempting to get in have to wait until the current thread is finished.Synchronization can be used for thread safety and to make sure single-threaded execution.

public class Singleton {
 // Only one object instance is allowed for this class.
 private String name;
 private static Singleton singleInstance = new Singleton("single");
 public String getName(){
 return name;
 }

private Singleton(String name){
 this.name = name;
 }

public static synchronized Singleton getInstance(String name)
 {
 return singleInstance;
 }

public void display() {
 System.out.println("This is singleton "+ name);
 }
}


public class UseSingleton implements Runnable {
 // This class will try to use singleton class
 Thread t;

public UseSingleton()
 {
 Singleton s;
 s = Singleton.getInstance("single");
 t = new Thread(this, "2");
 t.start(); 
 s.display();
 }

public void run() 
 {
 Singleton s;
 s = Singleton.getInstance("1");
 s.display();
 }

public static void main(String[] args) {
 new UseSingleton();
 }
}

 

Exit mobile version