Singleton Design Pattern

The singleton design pattern is classified as a creational pattern. It is appropriate for scenario where only one copy of the class is needed because singleton enforces that only one instance of the class gets created. Singleton pattern is useful when the situation calls for centralizing management of resources and provides a global point of access. These situation could be a window manager on a desktop, an application configuration object that holds the key-value mapping, and perhaps a logger object that logs the activities of the application. Below is the lazy implementation of the singleton pattern. The object is not created until it is needed.

Design

designPatterns-SingletonPattern.png

Implementation

public class Singleton {
    private Singleton instance = null;
    private Singleton() { }

    public static Singleton getInstance() {
        if(instance == null)
            instance = new Singleton();

        return instance;
    }
}

This article is my 26th oldest. It is 141 words long, and it’s got 1 comment for now.