Member-only story
Creational Design Patterns
4 min readNov 22, 2023
Creational design patterns focus on how objects are created, instantiated, and managed. They provide mechanisms for controlling the object creation process to ensure flexibility, efficiency, and reusability. There are five commonly recognized creational design patterns:
1. Singleton Pattern:
- Purpose: Ensures that a class has only one instance and provides a global point of access to that instance.
- Use Cases: When you want to ensure a single point of control or coordination (e.g., a configuration manager, or a database connection pool).
- Implementation: Typically involves a private constructor, a static method to access the instance, and a private static variable to hold the unique instance.
public class Singleton {
private static Singleton instance;
private Singleton() {
// Private constructor to prevent external instantiation.
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
In this example, the Singleton
class ensures that only one instance of itself can be created. The getInstance
method provides access to that instance, creating it if it doesn't exist.