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:

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.

share|improve this question

marked as duplicate by gnat, Scott Whitlock, dan1111, Telastyn, MichaelT Apr 11 at 11:50

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.

    
The "there can only ever be one of this thing" is way worse than global state. –  Telastyn Apr 11 at 11:36
    
@Telastyn, why? Global state is a state that there can only ever be one of. Seems indistinguishable to me. –  dan1111 Apr 11 at 11:46
    
this is a blatant duplicate –  gnat Apr 11 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 Apr 11 at 15:39

1 Answer 1

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.

share|improve this answer
    
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 Apr 11 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 Apr 12 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 Apr 19 at 11:37

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