Jobs
Blogs
Courses
Mentorship
Partners
For Employers
e2e logo

Singleton Design Pattern

mac

Singleton Design Pattern

The singleton design pattern is used to restrict the instantiation of a class and ensures that only one instance of the class exists in the JVM. In other words, a singleton class is a class that can have only one object at a time per JVM instance.

Why We need Singleton Design Pattern:

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, drivers objects, caching and thread pool.

Where We Use : 

It is used where only a single instance of a class is required to control the action throughout the execution. A singleton class shouldn't have multiple instances in any case and at any cost. Singleton classes are used for logging, driver objects, caching and thread pool, database connections.


  • Class-level Member (Eager Initialization Method): 
  1. Make constructor private.
  2. Make a private constant static instance (class-member) of this Singleton class.
  3. Write a static/factory method that returns the object of the singleton class that we have created as a class-member instance.
  4. We can also mark a static member as public to access constant static instance directly. But, I like to access class/instance members via methods only.
  5. So, the singleton class is different from a normal Java class in terms of instantiation. For a normal class, we use a constructor, whereas for singleton class we use the getInstance() method.

 Example

public class SingletonClass {
    private static final SingletonClass SINGLE_INSTANCE = new SingletonClass();
    private SingletonClass() {}
  public static SingletonClass getInstance() {
      return SINGLE_INSTANCE;
    }
}
  • Class-level Member (Lazy Initialization Method):
  1. Make constructor as private.
  2. Make a private static instance (class-member) of this singleton class. But, we cannot instantiate it.
  3. Write a static/factory method that checks the static instance member for null and creates the instance. At last, it returns an object of the singleton class.
  4. Example 
public class SingletonClass {
    private static SingletonClass SINGLE_INSTANCE = null;
    private SingletonClass() {}
    public static SingletonClass getInstance() {
        if (SINGLE_INSTANCE == null) {  
          synchronized(SingletonClass.class) {
          SINGLE_INSTANCE = new SingletonClass();
          }
        }
        return SINGLE_INSTANCE;
    }
}

Example Program :

class Database {
   private static Database dbObject;


   private Database() {      
   }


   public static Database getInstance() {


      // create object if it's not already created
      if(dbObject == null) {
         dbObject = new Database();
      }


       // returns the singleton object
       return dbObject;
   }


   public void getConnection() {
       System.out.println("You are now connected to the database.");
   }
}
class Main {
   public static void main(String[] args) {
      Database db1;


      // refers to the only object of Database
      db1= Database.getInstance();
      
      db1.getConnection();
   }
}
OUTPUT
You are now connected to the Database.