I started this page as something of a joke, but mostly as a comment on the _overuse_ of singletons. The codebase I had been working on had dozens of singletons from a variety of sources, which made testing and configuration extremely difficult. It seems to me that an application needs at most one singleton, which might be a dependency injection point. Glad to see that so many feel similarily. Sad to see such low methods of argumentation being used.
The below is a little harsh, but this is a rather big issue that is the bane of programmers everywhere.
I have a feeling that the people arguing in favor of singletons are not going to understand the arguments against, but I will try.
All Globals are bad. All singletons are (for this purpose) Globals. Therefore all Singletons are bad.
There are exceptions to every rule, no system can be complete and logical, etc. However, as a practical programming matter the exceptions would be rare enough and subtle enough (if they even exist) that anyone who does not think SingletonsAreEvil
are very unlikely to recognize the instance anyway. They are already clearly in 'FalsePositiveVille?
The link to this (made by someone else) describes the SimpletonPattern
as the 'overuse of singletons. As a programmer in many languages for many years, I can say without qualification that Singletons per se are to be avoided entirely.
One Singleton is too many. Arguments that 'it has to start somewhere', etc. are simply arguments out of ignorance and invalid. Nothing meaningful that you are going to program will not have 'context'. It is within 'context' that information and behavior that you want to assign to a Singleton belongs.
Someone countered that many things are singletons, such as this web site. If this web site is a singleton, then how, precisely, did that person manage to arrive here and enter a comment?
Is a bootstrapping program a singleton? No. It is passed context from the operating system. Is a bootstrapping operating system a singleton? No. It is passed context from the BIOS. The BIOS, then, surely the BIOS? Nope. Yours is not the only BIOS in the world and there is no reason at all to suppose that your global environment does not include more than one BIOS.
Before people go nuts attempting to come up with shoddy counter examples or whatever, I note the following:
Even if you could find something that was VERY, VERY, VERY compelling as a singleton I might want to simulate multiples within the same environment for debugging purposes (or whatever, I'm the user, I will drive).
Even if there were one great 'god singleton' in the universe, the concept would be of no use to you in programming. It already exists within your context, you'll not need to create another (that's your argument, not mine, it is by definition a 'singleton'). You therefore have no business creating a singleton except as a sloppy 'stopgap' while you attempt to fix your broken (or forming) context structure.
For practical purposes, everything has a context as far as your burning desire for a singleton is concerned. Therefore, instead of asking the name-space of the entire universe for some arcane object, simply ask your local context. If it is a convenience (typing) issue, then believe me you have other business to attend to rather than argue with more experienced software developers.
The argumentation is handwaving and has failed to even attempt to convince me -- it's arguing from a set of assumptions and possibly prejudices that are not even made explicit, but the reader is expected to guess what those are and buy into them. Further discussion in SimpletonPatternDiscussion has convinced me that the author confuses the notion of global variables with anything at all in global scope. --CostinCozianu
Singletons are an extreme version of the ReverseFlyweightPattern
, when done wrong. In some cases, you really do
need a fixed number of instances/mapped instances, but in other cases, you just need a nice dynamically-scoped variable.
Discussion moved to SimpletonPatternDiscussion
Really? There seems to be a lot of discussion right here.
In the EmbeddedSystems
world there are lots of cases where Singletons are the only real way to model a piece of hardware that is unique to the system. Hardware resources can only be accessed by one component at a time and that access has to be managed. A Singleton provides the point of contact for such a resource.