🎨 Design Patterns (Gang of Four)
Beginner
What is the Singleton design pattern?
Answer
The Singleton pattern ensures that a class has only one instance throughout the application's lifetime and provides a global access point to that instance. It is implemented by making the constructor private, storing the instance in a static variable, and exposing a static getInstance() method. Common real-world uses include a database connection pool, a logging service, or a configuration manager where having multiple instances would waste resources or cause inconsistent state. Care must be taken in multithreaded environments to prevent two threads from creating two instances simultaneously.