0

Most people here and on StackOverflow agree that Singletons are evil.

The main explanation I've come across against the use of Singletons, is they fact that they provide a global point of access to an object.

I admit to not understand why this is a bad thing. It's very convenient to be able to access an important and useful object from anywhere in your code, without having to 'pass it around' in order to get it.

So why is a global point of access in OOP a bad thing? Not only with Singletons, but generally. Please give concrete examples.

4
  • The "there can only ever be one of this thing" is way worse than global state.
    – Telastyn
    Commented Apr 11, 2014 at 11:36
  • @Telastyn, why? Global state is a state that there can only ever be one of. Seems indistinguishable to me.
    – user82096
    Commented Apr 11, 2014 at 11:46
  • this is a blatant duplicate
    – gnat
    Commented Apr 11, 2014 at 11:51
  • @dan1111 - because there are exceptionally few things in the world where having two of them (or at least two snapshots of their state) is a catastrophic problem that you must prevent. When you inevitably find out that your design assumption was wrong, it's harder to undo a singleton than it is to undo global state. Worse, the singleton assumption is more often worse than the global state assumption.
    – Telastyn
    Commented Apr 11, 2014 at 15:39

1 Answer 1

2

Disadvantages are that:

  1. it is not obvious where it is used

  2. when you use something that uses it, you cannot supply alternatives (esp. when testing, you do not want to use eg. real credit card payment)

  3. when you decide that you want two contexts in which it should be unique you must rewrite every usage.

These disadvantages are sometimes acceptable, sometimes not.

Note that problem is not in the fact that only one instance is used (it often is very usefull), but in it being hardcoded and invisible from outside.

3
  • I disagree that this is a duplicate (Singletons do not require global state), and with the religious idea that Singletons are always wrong. In many cases services want to be Singletons. For instance interfaces to an audio or network service. These can be provided via dependency injection (no global state) but enforce only one shared instance, so that, for example, multiple components do not try to independently send samples to an audio output device without being mixed. Real world code is filled with cases of Singletons, and it's a common argument that they are synonymous with global objects.
    – Jason Boyd
    Commented Apr 11, 2014 at 17:19
  • 'It is not obvious where it is used.' Why not? It's used everywhere the line SomeClass.getInstance() appears. Am I missing something?
    – Aviv Cohn
    Commented Apr 12, 2014 at 12:28
  • When you are looking at source of place where it is used, its clear that it is used. But when looking at only what is seen from outside, you don't see it. This can be said about any function call, but singletons/global data usually represents something you'd like to know that it is used. Some other approaches (lookup, field injection) has same disadvantage, some like constructor injections have their own problems.
    – user470365
    Commented Apr 19, 2014 at 11:37

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