0

Is singleton a design pattern or a design antipattern?

From here http://neugierig.org/software/chromium/notes/, it said 'Globals and singletons are already well-known as a design antipattern, but they have an interesting additional cost.'

But the GoF book said singleton a design pattern.

So can you please tell me when does using singleton is NOT okay?

Thank you.

michael
  • 93,094
  • 111
  • 230
  • 334
  • possible duplicate of [Who needs singletons?](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323) – Gordon Aug 31 '11 at 19:18

1 Answers1

2

The singleton is a design pattern.

However, since it is one of the simplest, it is also one that is most abused and in that sense, a design anti-pattern.

The difficulties in writing testable code that uses singletons is another reason people consider it to be an anti-pattern.

A singleton should only be used when you must have one instance of the class running in the application space. That is, when you have to guarantee that is can only be one such instance.

Oded
  • 463,167
  • 92
  • 837
  • 979