# Purpose
The Singleton pattern ensures that a class has only one instance, and provides a global point of access to it.
# Problem
The need to control access and creation of an instance of a class. It is useful when a single instance of a class must be shared and coordinated throughout the system.
# Solution
The solution proposed by Singleton is to limit the creation of a class to a single object and provide a global access point to this object:
-
Single instance: Ensures that a class has only one instance.
-
Global access: Provides easy and global access to that single instance.
# Structure
# Participants
-
Singleton: A class that has a method to create or access its only instance.
The details to note are:
-
Private constructor: This prevents instances from being generated, since it cannot be invoked outside the class itself.
-
Static attribute of the class itself: which the static method getInstance() will return when invoked.
-
Attributes and operations of the class itself: what gives meaning to the instance, the data and operations it handles.
Applying a private constructor and a static attribute of the class itself with a static method that returns it, we can convert a class into a Singleton.
# When to Use It
This pattern is recommended when:
-
There must be only one instance of a class and it must be accessible to clients from any point.
# Advantages
-
verified
Instance Control
Ensures that only one instance of a class exists.
-
verified
Reduced Resource Usage
Prevents unnecessary creation of objects, which can save resources.
-
verified
Global Access
Provides an access point to the instance from anywhere in the code.
# Disadvantages
-
warning
Single Responsibility
Can lead a class to have multiple responsibilities (ensuring its single instance).
-
warning
Multithreaded Environments
Access to the single instance must be synchronized in concurrent environments.
# Example: Configuration Parameters
We must develop a class to maintain the configuration parameters during system execution, which must be globally accessible and consistent.
Problem
Clients should be able to access the configuration parameters at all times, so we could keep them in an object, loaded at startup (so we don't have to load them more than once). Having a single instance of this object could be a good solution.
Proposed Solution
We implement a Singleton, because it guarantees us a single instance of the object that maintains the configuration parameters, and gives us access to it.
We create the Configuration class that contains the configuration parameters, and we convert it into a singleton, through a private constructor, a static attribute instance of the same class Configuration and a static method to access this instance getInstance().
Application is one of the classes that needs to access the values of the configuration parameters.
# Java Code
We encode in Java what we prepared in the diagram. We define the singleton class:
public class Configuration {
//Static attribute of the class type
private static Configuration instance;
//Class own attributes
private String configValue;
private Configuration() {
// Private constructor
configValue = "Initial Config";
}
//Static method to access the single instance
public static Configuration getInstance() {
if (instance == null) {
instance = new Configuration();
}
return instance;
}
//Class own methods
public String getConfigValue() {
return configValue;
}
public void setConfigValue(String configValue) {
this.configValue = configValue;
}
}
The client code uses the configuration:
public class Application {
public static void main(String[] args) {
Configuration config = Configuration.getInstance();
System.out.println(config.getConfigValue());
config.setConfigValue("Updated Config");
Configuration anotherConfig = Configuration.getInstance();
System.out.println(anotherConfig.getConfigValue()); // Will output "Updated Config"
}
}
# Mapping Participants
-
Configuration (Singleton): Maintains the values of the configuration parameters and ensures its only instance.
-
Application (Client): Is the class that uses the configuration parameters, accesses the singleton through the static method getInstance().
# Conclusions
We needed to store parameters, we saw how to do it with a singleton, which guarantees us a single object that maintains them throughout the execution and grants access to it.
# Related Patterns
Creational
Factory Method, Abstract Factory, Builder (sometimes implemented as Singleton).
Structural
Facade (can be implemented as Singleton).