Singleton Design Pattern

Back To Index

Overview

The Singleton Design Pattern ensures that a class has only one instance and provides a global point of access to it. It is often used to manage shared resources like configurations, logging, or thread pools.

Key Characteristics

Implementation

The following is an example of a Singleton implementation in Java:


public class Singleton {
    private static Singleton instance;

    // Private constructor to prevent instantiation
    private Singleton() {}

    // Public method to provide access to the instance
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
    

When to Use

Advantages

Disadvantages

Back To Index