This annoyed me, but then I wondered…”Why have I never used a singleton?” I am aware of the GoF patterns and value their use. Sometime I use them as a solution and sometimes they emerge while refactoring. In my opinion their greatest value is they provide a means of communication.
After some research my conclusion is that of many others: If you are tempted to use a singleton: DON’T. When you cannot come up with a different solution: Google for it or discuss your problem with some colleagues. When the best solution still is a singleton: Let me know.
The problems with the singleton are summed very well in This post from Scott Denssmore:
- They are mostly used as a global static.
(We agreed these are bad right?)
- They allow you to limit the creation of your objects
(This is not the responsibility of the class)
- They promote tight coupling
(While we strive for low coupling, tight cohesion)
- They carry state the entire lifetime of the program.
(Object state is the source of very annoying bugs)
The singleton I encountered suffers from problem 1,3,4. Time allowing I will gradually factor out this beast that is littered throughout the entire application. But this will require some negotiation with the rest of the team.
To provide some perspective I suggest also reading the comments and the following response from Matt Berther: Singletons are not evil. I have to agree with him. The singletons pattern is not evil, it is just a pattern. But I do think using singletons is, thus my conclusion is that ’a good singleton’ is an oxymoron.