4 ms·
Religious wars against particular design patterns just seem so, well, religious! To me, a singleton is a particular implementation of a global variable. Nothin
by eludwig 13y ago
Religious wars against particular design patterns just seem so, well, religious!
To me, a singleton is a particular implementation of a global variable. Nothing more. You use it one when you need one. It isn't magic or evil. It's a global variable.
The problem comes in when you are using one but don't know it. It's the beginner Spring problem. Developers don't realize that some libraries use them by default and start storing state there. Needless to say, not good.
- fhd2 13y ago> To me, a singleton is a particular implementation of a global variable. Except that it's not. Global variables aren't initialised lazily, and their type can be an interface just fine. I can think of a few valid use cases for singletons, but it's not a great replacement for global variables IMO. Service locators are closer, and I think they have far more valid use cases than singletons.
- dllthomas 13y agoIsn't that basically "A singleton is a particular, better implementation of a global variable"? Does "global variable" imply a primitive type?
- eludwig 13y agoA getInstance() call can return a pre(statically)-initialized instance if it wants to. Laziness is optional. The pattern gives you the freedom to do it either way. Maybe not so in all languages?
- masklinn 13y ago> Except that it's not. Global variables aren't initialised lazily Depends on the language, and the global. In java, a static attribute is initialised when the class is first used. Until the class itself is accessed and used, the attribute remains uninitialized. In clojure, delay[0] will invoke (and cache) its body the first time it's deref'd. [0] http://clojuredocs.org/clojure_core/clojure.core/delay http://clojuredocs.org/clojure_core/clojure.core/delay
- sharpneli 13y agoSingleton pattern feels a bit like someone once heard that global variables were bad. Nonetheless people still need some sort of global state in some problems, but one couldn't use global variables because they were bad. So they invented alternative way to do exactly the same thing. And not surprisingly singletons are being abused just like good 'ol global variables by bad programmers, and thus eventually they will be considered bad. And then a new way of handling some global state will be invented because singletons are bad and we still need some way of accessing some global state in some cases. And so the cycle continues.
- dllthomas 13y agoIt's not quite exactly the same thing. It provides some control over initialization and (depending on the language) possibly eases synchronization, compared to a scattering of globals or a static class. It has the other problems of global variables, though.
- sharpneli 13y agoIt shares the crucial feature which makes global variables considered to be so bad. Namely: "Where on earth is this set to X, it should be Y at this point!"
- kabdib 13y agoWhereupon you can put tracing in to find out when the state was screwed up. Singletons aren't bad, they're just misused a lot by bad programmers. It's like saying "Food is bad," and sure, people misuse food and hurt themselves, but that doesn't mean we should ban food.
- dllthomas 13y agoExcept it's nothing like saying "food is bad". If you don't ever eat food, you die. If you don't ever use singletons, some of your code has an additional parameter uglily threaded through it.
- 13y ago