Singleton Classes

Singleton classes are a design pattern used in software engineering which restricts the instantiation of a class to one object. At any time, at most one object of a singleton class can exist.

There are numerous ways to achieve this. Below is a simple approach implemented in Java:

public class Singleton
    private static Singleton instance = null;

    /* Since the constructor is private,
     * other classes cannot instantiate this class explicitly
    private Singleton()

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

        return instance;

Leave a Reply

Your email address will not be published. Required fields are marked *