Design Patterns · Junior · Question 4 of 75
Why is Singleton often called an anti-pattern?
- ✓Correct answer: It introduces hidden global state and makes testing harder
- 2It requires too much memory to store instances
- 3It can't be implemented in JavaScript without classes
Explanation
Code that accesses a singleton directly depends on it implicitly: tests affect each other, and swapping the implementation is hard. A compromise is to create one instance but pass it explicitly (dependency injection) so tests can substitute a mock.