Singleton Pattern


In software engineering, the singleton pattern is a design pattern that restricts a class from creating more than one object (Instantiation is restricted to one object). 
Such a design is generally used in a software system which operates more efficiently when only one object exists. It is useful when exactly one object is needed to coordinate actions across the system.

Read more about singleton pattern here...

Java implementation


  • Single Threaded version

Helper.java


package me.dhanoop.singleton;

/**
 *
 * @author dhanoopbhaskar
 */
public class Helper {

    public Helper() {
        System.out.println("Created Helper Object");
    }
} 

Singleton.java

package me.dhanoop.singleton;

/**
 *
 * @author dhanoopbhaskar
 */
public class Singleton {

    private Helper helper = null;

    public Helper getHelper() {
        if (helper == null) {
            helper = new Helper();
        }
        return helper;
    }

    public static void main(String[] args) {
        Singleton instance1 = new Singleton();        
        Helper helper1 = instance1.getHelper();
        Helper helper2 = instance1.getHelper();
        Helper helper3 = instance1.getHelper();
    }
}

Output:
Created Helper Object


  • Multi-threaded version
Singleton.java


package me.dhanoop.singleton;

/**
 *
 * @author dhanoopbhaskar
 */
public class Singleton {

    private static Helper helper = null;

    public static Helper getHelper() {
        if (helper == null) {
            helper = new Helper();
        }
        return helper;
    }

    public static void main(String[] args) {

        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                Singleton.getHelper();
            }
        };

        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
    }
}

Output:
Created Helper Object
Created Helper Object
Created Helper Object

Note: The output may vary depending on the execution of threads. Sometimes you will get only two lines in the output.

The above is not a desired output. The Helper object should be created only once. 


  • Corrected multi-threaded version
Singleton.java
package me.dhanoop.singleton;

/**
 *
 * @author dhanoopbhaskar
 */
public class Singleton {

    private static Helper helper = null;

    public static synchronized Helper getHelper() {
        if (helper == null) {
            helper = new Helper();
        }
        return helper;
    }

    public static void main(String[] args) {

        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                Singleton.getHelper();
            }
        };

        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
    }
}

Output:
Created Helper Object

Note: The code above performs synchronization every time getHelper() is called. The double-checked locking idiom tries to avoid synchronization after the helper is allocated.

Post a Comment

0 Comments