Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question
4  
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 Mar 16 '13 at 4:11
    
Thanks for the explanation caleb. Agree. –  Chandra Mohan Mar 16 '13 at 11:36
add comment

marked as duplicate by Caleb, Yannis Rizos Mar 16 '13 at 4:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
4  
It's not the amount of use, it's the nature of the use. –  Caleb Mar 16 '13 at 4:02
1  
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 4:29
1  
@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 5:12
    
Thanks for the info on MultiTon pattern (en.wikipedia.org/wiki/Multiton_pattern). Useful to know. –  Chandra Mohan Aug 15 '13 at 5:33
    
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 at 23:54
add comment

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