-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.

2
  • 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 other places.
    – Caleb
    Commented Mar 16, 2013 at 4:11
  • 1
    You may find this analysis interesting: Singletons Must Die
    – yegor256
    Commented Jun 30, 2016 at 2:56

1 Answer 1

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.

7
  • 4
    It's not the amount of use, it's the nature of the use.
    – Caleb
    Commented Mar 16, 2013 at 4: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
    Commented Mar 16, 2013 at 4: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
    Commented Mar 16, 2013 at 5:12
  • 1
    Thanks for the info on MultiTon pattern (en.wikipedia.org/wiki/Multiton_pattern). Useful to know. Commented Aug 15, 2013 at 5: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
    Commented Apr 13, 2014 at 23:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.