Skip to content

Latest commit

 

History

History

Day_27_Singleton

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Java Singleton

Singleton is a design pattern that restricts the instantiation of a class to only one instance and provides a global point of access to that instance. This pattern ensures that there is a single instance of the class throughout the entire application's lifecycle, and it is often used to control access to shared resources or manage configurations.

Here's a basic example of how to implement a Singleton in Java:

public class Singleton {
    // Private static instance variable
    private static Singleton instance;

    // Private constructor to prevent external instantiation
    private Singleton() {
        // Initialization code, if needed
    }

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

    // Other methods and attributes of the Singleton class
}

In this example:

  1. We have a private static instance variable (instance) that holds the single instance of the Singleton class.

  2. The constructor of the Singleton class is made private, which prevents external code from creating new instances of the class.

  3. The getInstance method is a public static method that provides access to the single instance of the Singleton class. If the instance has not been created yet, it is created the first time getInstance is called, and subsequent calls return the existing instance.

  4. Other methods and attributes of the Singleton class can be defined as needed.

Here's how you would use the Singleton:

public class SingletonExample {
    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();

        // Both singleton1 and singleton2 should refer to the same instance
        System.out.println(singleton1 == singleton2); // Should print true
    }
}

In this example, singleton1 and singleton2 refer to the same Singleton instance because the getInstance method ensures that only one instance is created.

Singletons are useful in various situations, such as managing database connections, controlling access to configuration settings, and implementing caches or thread pools where a single shared instance is required to maintain consistency and efficiency across the application.