← All Quizzes
What is the Singleton pattern and what are its common criticisms?
Explanation
The Singleton pattern ensures a class has only one instance and provides global access to it. While useful for things like configuration or connection pools, it is frequently criticized: it introduces global state (hidden dependencies), makes unit testing difficult (hard to mock/replace), violates SRP (manages both instance and functionality), can cause issues in multi-threaded environments, and makes parallelization harder. Prefer dependency injection for sharing single instances, which provides the same benefit with testability and explicit dependencies.