3 ms·
> 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
by 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