0

Possible Duplicates:
What is so bad about Singletons
Problems with Singleton Pattern

Are there any downsides to the singleton pattern? I heard this was an interview question and i am coming up short on what was meant.

imho, it's about the usage and nothing in the pattern itself is problematic

Community
  • 1
  • 1
Kumar
  • 9,481
  • 10
  • 74
  • 126
  • 1
    Read here http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Roman Feb 26 '10 at 18:05
  • 3
    There's nothing wrong with the pattern. But it is probably the pattern that is most misused when it is not appropriate. – Mark Byers Feb 26 '10 at 18:06
  • Poss duplicate http://stackoverflow.com/questions/1448393/singleton-design-pattern-pitfalls and http://stackoverflow.com/questions/1392315/problems-with-singleton-pattern – t0mm13b Feb 26 '10 at 18:06
  • You should be checking to see if this has been answered already....when you type in a question using keyword(s), SO will inform you of the possible questions that matches the keyword(s), check there first before proceeding.... – t0mm13b Feb 26 '10 at 18:08
  • I don't like the way it always leaves the lid off the mayonnaise jar when it puts it back in the fridge. And what's up with the towels all over the bathroom floor? – tvanfosson Feb 26 '10 at 18:08
  • sorry, it didn't show up in list due to different wording perhaps – Kumar Feb 26 '10 at 18:31

3 Answers3

1

That's a very generic question, it really depends on your use cases. I'd cite the following:

  • You always need a mutex (or synchronized block) to protect the initial getInstance() call, which can be problematic in some cases.
  • It is a hack to hide the fact that you are creating a global variable, and global variables are generally bad. However, there are many cases where the simplicity that they bring is appreciable, but pay attention not to abuse them.
Zorglub
  • 1,987
  • 1
  • 19
  • 21
1

From Wikipedia:

Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application.

Personally, since I've started using Spring to auto-wire my application I've never had the need to write a singleton.

Adamski
  • 51,827
  • 12
  • 103
  • 150
0
  1. Not testable or at least it's difficult
  2. Hard to find dependent classes
  3. Does not allow subclassing
  4. ...
whiskeysierra
  • 4,761
  • 1
  • 25
  • 36