Expanding my comment...
The question is poorly worded, singletons seem to work well in many situations. It should be "what are some specific situations in which singletons don't expose their negative properties?" Briefly, "when are global variables good"?
A global variable is a global variable.
As soon as you use
void f()
{
X* x = X::getInstance();
...
}
instead of void f(X*)
, your order for the spaghetti dish has been accepted.
Singletons proliferate, and like to depend on each other. SessionManager
uses EventManager
which uses LogManager
. Someone wants the log files to mention the name of the current user. The LogManager
maintainer adds SessionManager::getInstance()->getUser()->getName();
, everything's roses until the LoginManager
, which also uses LogManager
, wants to log a login failure.
Singletons inhibit testability.
The single-instance property is semi-useful only in production code. You may be unable to test the void f()
function at all, and probably will have (few) tests only for the happy path.
As you can guess, my answer to when are global variables good? is "never". What is yours?