Pacific-Design.com

    
Home Index

1. Design Patterns

2. Singleton Pattern

Design Patterns / Singleton Pattern /

Singleton Pattern

In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
/* SingletonEnum.java */

package singleton;
import java.util.Arrays;

public enum SingletonEnum {

    INSTANCE1, INSTANCE2;
    private final String[] name = {"one", "two", "three"};
    private int counter = 0;
    
    public void display() {
        System.out.println(Arrays.toString(name));
    }
    
    public void getCounter() {   
        System.out.println(counter);
    }

    public void setCounter(int counter) {
        this.counter += counter;
    }
}


/* SingletonNew.java */

package singleton;

// Thread save Singleton Instance
public class SingletonNew {

    private static final SingletonNew instance = new SingletonNew();

    private SingletonNew() {
    }

    public static SingletonNew getInstance() {
        return instance;
    }

    public void connect() {
        System.out.println("Connecting to Instance");
    }

}


/* SingletonThreadSafe.java */

package singleton;

public class SingletonThreadSafe {

    private static SingletonThreadSafe instance = null;
    
    private synchronized static void createInstance() {
        if(instance == null) instance = new SingletonThreadSafe();        
    }
    
    public static SingletonThreadSafe getInstance() {
        if(instance == null) createInstance();
        return instance;
    }
    
    public void Hello()  {
        System.out.println("Hello from synchronized singleton");
    }
}


/* SingletonLazy.java */

package singleton;

// Lazy Singleton instance is not thread safe
public class SingletonLazy {

    private static SingletonLazy instanceLazy;

    private SingletonLazy() {
    }

    // Lazy instance does not happen until last posible moment
    public static SingletonLazy getInstance() {

        if (instanceLazy == null) {
            instanceLazy = new SingletonLazy();
        }
        return instanceLazy;
    }

    public void connect() {
        System.out.println("Connecting to Lazy Instance");
    }
}


/* Main.java */

package singleton;

public class Main {

    public static void main(String[] args) {

        System.out.println("-------------- Enum Singleton --------------");
        SingletonEnum.INSTANCE1.setCounter(5);
        SingletonEnum.INSTANCE1.getCounter();
        SingletonEnum.INSTANCE1.display();
        SingletonEnum.INSTANCE2.getCounter();        
        
        System.out.println("\n------------- New Singleton ---------------");
        SingletonNew db = SingletonNew.getInstance();
        db.connect();
        SingletonNew.getInstance().connect(); // Methods chaining:w        

        System.out.println("\n---------- Thread Safe Singleton ----------");
        SingletonThreadSafe.getInstance().Hello();
        
        System.out.println("\n------------  Lazy Singleton --------------");
        SingletonLazy lazy = SingletonLazy.getInstance();
        lazy.connect();

    }

}

/*
Output

-------------- Enum Singleton --------------
5
[one, two, three]
0

------------- New Singleton ---------------
Connecting to Instance
Connecting to Instance

---------- Thread Safe Singleton ----------
Hello from synchronized singleton

------------  Lazy Singleton --------------
Connecting to Lazy Instance
*/