Design Patterns · Junior · Question 4 of 75

Why is Singleton often called an anti-pattern?

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.

Further reading

Problem with a question

Design Patterns · Junior · question 4

Test yourself

Take the Design Patterns · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz