5 ms·
I strongly agree. This is one of the better essays on why: http://blog.plover.com/2006/09/11/ http://blog.plover.com/2006/09/11/ One point it unfortunately do
by sah 19y ago
I strongly agree. This is one of the better essays on why: http://blog.plover.com/2006/09/11/ http://blog.plover.com/2006/09/11/
One point it unfortunately doesn't cover is the large percentage of Go4 patterns that are just plain bad ideas. Singleton, for example, should just never be used: it's like relying too much on global variables, but worse.
- bayareaguy 19y agoBut some of the very things you need to live will kill you if you consume too much of them. The problem is people often use the Design Patterns book as a design manual but that's not what it really is - it's an attempt to catagorize ideas. The very fact that you used the term Singleton means that now you and I can argue the merits of using that pattern in different situations. Would you rather us attempt to do that without common terminology?
- sah 19y agoThere's nothing wrong with having common terminology. But when people are being taught about bad ideas without any cautionary comments, that's a problem. By the same token, there's nothing wrong with knowing what a goto statement is. But I'm glad we don't have a popular book that implicitly advocates its use, or college classes that teach it to students as if it's the state of the art in control flow.
- boucher 19y agoI agree that the book is way too overused. I was immediately turned off when I would interview people who started talking in design patterns (not to mention people who would say the wrong ones). However, I think there are completely valid uses of the "Singleton" concept. It can be an elegant solution to a lot of problems.
- sah 19y agoI'd like to hear about valid uses of Singleton. I'm not completely sure there aren't any, but I don't think I've ever seen one.
- deleted 19y ago[deleted]
- deleted 19y ago[deleted]
- icky 19y agoIt has the same use as most of the patterns: working around the brokenness of the popular OO languages of the time.
- Tichy 19y agoSo how do you avoid Singleton? I hope not with another pattern...
- sah 19y agoUsually 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.