Woodstock Blog

a tech blog for general algorithmic interview questions

[Java OOP] Singleton Pattern Introduction

Singleton Pattern

Singleton design pattern is one of the 2 most frequent topics for OOD.

Singleton pattern is a design pattern that restricts the instantiation of a class to one object.

in Java

Since Java 5.0, the easiest way to create a Singleton is the enum type approach. Here the code is not given.

We will instead cover a very popular implementation: Lazy initialization using double-checked locking:

public class SingletonDemo {
    private static volatile SingletonDemo instance = null;
    private SingletonDemo() { }
    public static SingletonDemo getInstance() {
        if (instance == null) {
            synchronized (SingletonDemo.class) {
                if (instance == null) {
                    instance = new SingletonDemo();
                }
            }
        }
        return instance;
    }
}

An alternate simpler and cleaner version may be used at the expense of potentially lower concurrency in a multithreaded environment:

public class SingletonDemo {
    private static SingletonDemo instance = null;
    private SingletonDemo() { }
    public static synchronized SingletonDemo getInstance() {
        if (instance == null) {
            instance = new SingletonDemo();
        }
        return instance;
    }
}