juniorDesign Patterns
What is the Singleton Pattern?
Updated Apr 28, 2026
Short answer
A creational pattern that ensures a class has only one instance, while providing a global access point to this instance.
Deep explanation
Singleton solves two problems: ensuring that a class has just a single instance (e.g., controlling access to a shared resource like a database), and providing a global access point to that instance. However, it violates the Single Responsibility Principle because it solves two problems at the same time.
Real-world example
A single database connection pool or a global configuration manager object in an application.
Common mistakes
- Overusing singletons, creating hidden dependencies (global state) that make unit testing difficult.
Follow-up questions
- How do you make a Singleton thread-safe?