Friday, May 22, 2020

5 Ways to implement Singleton Design Pattern in Java

Oracle Java Tutorial and Material, Oracle Java Study Material, Oracle Java Learning, Oracle Java Exam Prep

This article introduces the singleton design pattern and its 5 implementation variations in Java You will learn different ways to implement a singleton pattern and also understand the challenges with creating a singleton class like thread-safety issues and serialization issues.

Problem


At most one instance of a class must be created in an application.

Solution


That class (singleton) is defined including its own instance, and the constructor must be private.

What is Singleton Design Pattern in Java?


Singleton Design Pattern is a popular pattern to keep a global variable. It's a class with a getInstance() method which is both public and static so that any class which needs a reference to this class can call this method and get the global reference.

Here is the UML diagram of Singleton design pattern in Java and then we'll see how you can implement Singleton pattern in Java:

Oracle Java Tutorial and Material, Oracle Java Study Material, Oracle Java Learning, Oracle Java Exam Prep

1. Lazy initialization, non-thread-safe:


This is the classical version, but it's not thread-safe. If more than one thread attempts to access instance at the same time, more than one instance may be created.

public class Singleton {
    private static Singleton instance = null;
    public static Singleton Instance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
    private Singleton() {}
}

2. Non-lazy initialization, thread-safe


This is the simplest thread-safe version, but it does not support lazy initialization.

public class Singleton {
    private static volatile  Singleton instance = new Singleton();
    public static Singleton Instance() {
        return instance;
    }
    private Singleton() {}
}

3. Lazy initialization, thread-safe


This version supports both properties but has performance problems. Once a thread uses a singleton instance, the others have to wait because of the lock.

public class Singleton {
    private static Singleton instance = null;
    private static readonly object lockObj = new object();
    public static Singleton Instance() {
        lock (lockObj) {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
    private Singleton() {}
}

4. Double-check locking


An improved version of the third solution. Two null controls prevent lock waits for the most time, but not always. Also, it does not work properly for Java because of the Java memory management approach.

public class Singleton {
    private static Singleton instance = null;
    private static object lockObj = new object();
    public static Singleton Instance() {
        if (instance == null) {
            lock (lockObj) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
    private Singleton() {}
}

5. Nested initialization


A nested class is used for lazy initialization. This version is also thread-safe, but a bit complex. For most situations, solutions 2 or 4 will be suitable according to performance parameters.

public class Singleton {
    public static Singleton Instance() {
         return Nested.instance;
    }
    private Singleton() {}

    class Nested {
        static Nested() {}
        internal static readonly Singleton instance = new Singleton();
    }
}

Usage:


public static void main (string[] args) {
    Singleton instance = Singleton.Instance();
}

That's all about how to implement the Singleton Design Pattern in Java. I have shown you 5 different implementations of the Singleton pattern in Java. You can use any of the above methods to create Singleton for your project, though I recommend Enum Singleton pattern because of its simplicity and full proof nature.

Related Posts

0 comments:

Post a Comment