Why I really dislike the Singleton design pattern

September 14, 2005

To quote Glyph Lefkowitz (from here):

Aah, the singleton. Global variables for the new millennium.

I'm not against global variables, but I am against misleading people about them. The Singleton pattern lies about them; it looks like you have a normal object, but you really have a global variable without knowing it.

(And if everyone knows you have a faux global variable, you are just tarting up a global variable in 'object-oriented' clothes. This is like respecting the letter of the law ('globals bad, objects good') without understanding its spirit.)

If your language doesn't have global variables, you have my sympathies and condolences. In such circumstances, Singletons and other things become necessary hacks.

Note that I consider 'Singleton' not the same as a class where you only ever instantiate one object from in the course of your program. With Singleton, your program appears to instantiate multiple objects from the same source, but they are all the same thing.

(I've seen some people use 'Singleton' to describe what I would call a cached lookup. This is where you have descriptors of some sort and turn them into objects, but repeatedly calling the factory function with the same descriptor always gives you the same object back instead of making a new one every time.)

(Sort of continued from PointlessClasses.)

Written on 14 September 2005.
« Concurrency is tricky
Efficiently distributing huge files to lots of workstations »

Page tools: View Source, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Sep 14 01:55:50 2005
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.