Use of Singleton Class in Java
In Java, a Singleton class is a design pattern that ensures the existence of only one instance (object) of a class throughout the application and provides a global point of access to that instance. The primary goal of a Singleton class is to control the instantiation process and restrict the number of instances to just one.
Singleton classes are commonly used in scenarios where there should be only one instance of a class that needs to be shared and accessed throughout the application. They are often employed for managing resources, implementing caching mechanisms, controlling access to shared objects, or providing global configuration settings.
However, it's important to note that Singleton classes can introduce a global state, which can make code harder to test and maintain. They can also make code dependencies less explicit. Therefore, it's recommended to carefully consider the necessity of a Singleton and use it judiciously, ensuring it is truly required for the specific use case.
Example 1
In the given Java program in the main method, we first obtain the instance of the SingletonExample class using the getInstance() method. Then, we use the obtained instance to call the doSomething() method, which simply prints "Doing something..." to the console. The program demonstrates the usage of the SingletonExample class, where the getInstance() method returns the same instance every time it's called, ensuring that only one instance of the SingletonExample class exists throughout the program.
FileName: SingletonExample.java
public class SingletonExample { private static SingletonExample instance; // Private constructor to prevent instantiation from outside the class private SingletonExample() { } // Public method to get the instance of the Singleton class public static SingletonExample getInstance() { if (instance == null) { // Create a new instance if it doesn't exist instance = new SingletonExample(); } return instance; } // Other methods of the Singleton class public void doSomething() { System.out.println("Doing something..."); } public static void main(String[] args) { // Obtain the instance of the Singleton class SingletonExample singleton = SingletonExample.getInstance(); // Use the instance to call methods singleton.doSomething(); } }
Output
Doing something...
Example 2
In the above program, the main method obtains the instance of the ConfigurationManager using the getInstance() method. Then, we retrieve the current configuration using the getConfiguration() method and print it to the console.
Next, we update the configuration using the setConfiguration() method. Finally, we retrieve the updated configuration and print it again to verify the changes.
FileName: ConfigurationManager.java
public class ConfigurationManager { private static ConfigurationManager instance; private String configuration; private ConfigurationManager() { // Load configuration from a file or any other source configuration = "Default configuration"; } public static ConfigurationManager getInstance() { if (instance == null) { // Create a new instance if it doesn't exist instance = new ConfigurationManager(); } return instance; } public String getConfiguration() { return configuration; } public void setConfiguration(String configuration) { this.configuration = configuration; } public static void main(String[] args) { // Obtain the instance of the ConfigurationManager ConfigurationManager configManager = ConfigurationManager.getInstance(); // Get the current configuration String configuration = configManager.getConfiguration(); System.out.println("Current configuration: " + configuration); // Update the configuration configManager.setConfiguration("New configuration"); // Get the updated configuration configuration = configManager.getConfiguration(); System.out.println("Updated configuration: " + configuration); } }
Output
Current configuration: Default configuration Updated configuration: New configuration
Example 3
In the above program, the Logger class represents a Singleton logger that keeps track of log messages. It has a private constructor that initializes the log variable as a StringBuilder object. The getInstance() method provides a global access point to the single instance of the Logger class. It follows the lazy initialization approach by creating a new instance only if it doesn't exist yet.
FileName: Logger.java
public class Logger { private static Logger instance; private StringBuilder log; private Logger() { log = new StringBuilder(); } public static Logger getInstance() { if (instance == null) { instance = new Logger(); } return instance; } public void logMessage(String message) { log.append(message).append(" "); } public String getLog() { return log.toString(); } public static void main(String[] args) { // Obtain the instance of the Logger Logger logger = Logger.getInstance(); // Log some messages logger.logMessage("Error: An unexpected error occurred."); logger.logMessage("Warning: This operation may cause data loss."); logger.logMessage("Info: The process completed successfully."); // Get the log String log = logger.getLog(); System.out.println("Log: " + log); } }
Output
ERROR! Log: Error: An unexpected error occurred. Warning: This operation may cause data loss. Info: The process completed successfully.