Atlas · Details
Singleton Considered Stupid
Author’s note
This was considered controversial back in the day. People really did use Singleton as a crutch for practically every design situation. And we saw it happen later in the early days of service design, as well.
In the 20+ years since I wrote this, Singleton has proven to have a small number of legitimate use cases. As we moved away from subclassing, it became less of a pain point, etc. So it may not be as bad as it once was. But it is still probably heavily overused.
AI Notes
Steve writes this in 2004 with the frustration of an Amazon interviewer who has heard one too many candidates name "Singleton" as the design pattern they know. He opens with the Simpleton Pattern: the candidate who claims to have read GoF and can only name the Singleton and possibly "the Visitater." Then comes the confession — Steve loved Singleton, built fifty Manager classes around it, wrote his own boilerplate-reduction delegation layer, and emailed Ralph Johnson to ask if static methods still counted (they do) — and found himself with a Java codebase in which classes were pure namespaces and the entire OO machinery had been swept under a rug. The objections that follow cover memory leaks, resource leaks, the impossibility of subclassing, the brittleness of static methods, the double-checked-locking trap, and the danger of accidental Doubletons under threads. The closer seeds his later essays: about a third of the GoF patterns aren't really OO patterns but workarounds for C++ deficiencies — Visitor is map, Strategy is a first-class function, Iterator is a poor man's Visitor, stateful Strategy is a closure.
The Kingdom-of-Nouns essay two years later is this last paragraph fully grown. The prescription at the end is small and practical: when in doubt, use Factory Method. Computers are fast. Treat yourself to a few extra instances. The reason the piece still gets cited is that the same anti-pattern keeps replaying itself, with the next mis-applied magic tool in the slot where "Singleton" used to be.
Related listings
-
2005
When Polymorphism Fails
Same critique from the opposite end of the OO compass. Singleton is about people skipping OO; When Polymorphism Fails is about people over-applying it. The two essays bracket Steve's case-by-case OO position.
-
2006
Execution in the Kingdom of Nouns
Two years later, the Kingdom-of-Nouns essay is the canonical version of the argument Singleton is sketching: about a third of GoF patterns are first-class functions and closures in C++/Java costume.
-
2005
Practical Magic
Same instinct — fewer patterns, fewer ceremony classes, more direct use of the language. Practical Magic is the constructive version of the case Singleton argues by negative example.