-6

Was reading an interesting blog post on Singleton design pattern which is so widely used. If it's stupid or as in some quora posts, its an anti-pattern why is it so widely used even in frameworks.

For e.g., Spring framework (java) makes all the classes available through dependency injection as Singleton classes and adding prototype is optional.

bschandramohan
  • 115
  • 1
  • 5
  • 5
    For what it's worth, words like *evil* and *stupid* are likely to attract close votes because they make the question subjective and controversial. I've voted to close because the question has been asked before here on SO, and I'm downvoting because this question "does not show any research effort" -- it's discussed in [many](http://c2.com/cgi/wiki?SingletonsAreEvil) [other](http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx) [places](http://blog.code-cop.org/2012/01/why-singletons-are-evil.html). – Caleb Mar 16 '13 at 04:11
  • 1
    You may find this analysis interesting: [Singletons Must Die](http://www.yegor256.com/2016/06/27/singletons-must-die.html) – yegor256 Jun 30 '16 at 02:56

1 Answers1

6

Not at all. In my opinion, if used sparingly, it's actually one of the most useful patterns you'll find. It's an easy way to solve otherwise extremely difficult problems. Sure, every criticism is true, but to be honest, I'm willing to deal with a little bit of a bad pattern in an otherwise well designed application / framework that follows the other, better patterns.

The best example of an effective singleton, in my opinion, is a LogManager—something you typically use throughout your application, but you want to have it defined, configured, and managed in only one place. In most other cases where I started out using a singleton, I eventually ended up refactoring the code into something else. But I still keep the singleton pattern in my back pocket for those rare cases where it does more good than harm.

I've never used Spring, and barely used Java, so feel free to take this answer with a grain of salt.

p.s.w.g
  • 4,135
  • 4
  • 28
  • 40
  • 4
    It's not the *amount* of use, it's the *nature* of the use. – Caleb Mar 16 '13 at 04:02
  • 3
    A log manager as a singleton is a good example of how the pattern is commonly abused; there's no reason that you couldn't have more than one log manager, so the singleton restriction isn't necessary. Log managers are sometimes set up as singletons mainly so that you get easy global access to a shared object, and that's possible even without restricting the log manager class to a single instantiation. – Caleb Mar 16 '13 at 04:29
  • 2
    @Caleb while that's true, in 90% of applications, it's not necessary. I still think it's a legitimate use for this pattern (or at least the closely related *multiton*). – p.s.w.g Mar 16 '13 at 05:12
  • 1
    Thanks for the info on MultiTon pattern (http://en.wikipedia.org/wiki/Multiton_pattern). Useful to know. – bschandramohan Aug 15 '13 at 05:33
  • 2
    This is where OOP gets completely ridiculous: when people start calling a global dictionary of things a 'pattern'. Just forget about types, right? – fatuhoku Apr 13 '14 at 23:54
  • @fatuhoku, Because it's fun to invent names. – Pacerier Jun 25 '14 at 00:46
  • @Pacerier Guess you're right. My concern is that with a name with a Wikipedia article, the practise is made legit. A pattern / anti-pattern really is "documented workarounds for the inadequacies with the expressiveness of programming languages". That in itself, is a name that's gained legitimacy! – fatuhoku Jun 25 '14 at 10:49