4 ms·
Usually when I see Singleton used, what the author really wants is either a value that is cached after the first time it's created (so the correct pattern is "c
by sah 19y ago
Usually when I see Singleton used, what the author really wants is either a value that is cached after the first time it's created (so the correct pattern is "cache"), or more often just a global variable.
What I find offensive about Singleton is the idea that that the author knows there is no context in which you might want more than one of some object. This is especially bad in libraries, where having objects enforce their own uniqueness breaks the ability for me to use two FooManagers where the author only envisioned using one. Designs are generally improved by accounting for the possibility of more than one instance, even if it isn't immediately necessary.
- Tichy 19y agoI must admit I can't remember when I have last seen it. I guess the usual case is the ConnectionPool for the database, which for most Java Web projects is being handled by the container (not sure how Rails does it). But in that case it does not seem the end of the world to have two connection pools. Somehow you have to still manage the whole picture, though... Maybe two are OK, but what if suddenly you have 1000 connection pools? Maybe then suddenly you start wishing you had restricted them to 1 from the start. How is a global variable different from a singleton, anyway? Seems to me, in distributed and multithreaded environments the singleton problem is still a hard problem.
- sah 19y agoConnectionPool is an excellent example of a bad case for Singleton: what if you have two databases? What if you want to use different pools for different types of queries? A global variable is different from a Singleton because if you write a Singleton class in some module that I want to use, I have to fork your module if I want to make it possible to use two of them. If you use a global variable, I can always make a second global variable.
- Tichy 19y agoOK, I am not even sure anymore what the singleton pattern is _(it's been a while). If it is the class constructor that makes sure you can only ever create one instance, it might be bad. I thought any mechanism for ensuring singleness would qualify for the pattern. Then you examples wouldn't invalidate the connection pool, you could just have to different pools, one for each database. I agree that with the "class instantiation implementation" you could probably not do that.
- bayareaguy 19y agohttp://c2.com/cgi-bin/wiki?SingletonPattern http://c2.com/cgi-bin/wiki?SingletonPattern has some good discussion on it.
- danw 19y agoA difference between Singleton and a global variable is that a singleton can have lazy instantiation
- deleted 19y ago[deleted]
- bayareaguy 19y agoI think Singletons are ok when the core language's runtime promises to take care of them. They are also ok for things like the abstract state objects in a state machine and for special immutable Sentinal objects (e.g. Lisp nil, Python None, ...).
- sah 19y agoGood point: things like states, true, false, and nil make sense. I think the commonality there is that those are all immutable values whose only purpose is to have a fast object identity comparison. In a way, they're similar to global variables which are all members of the same class, except that it's nice for them to have separate types.
- sah 19y agoIn fact, you could think of Python's None as being a singleton, but True and False being the two allowed instances of a, uh, doubleton? And similarly, states might be implemented as a limited set of allowed values of a single class. This reminds me of Haskell's algebraic types. This maybe isn't quite "Singleton", but it is at least a useful case for limiting the number of allowed values of a class.