Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2008-01-18 06:01:30

Tobias Schwinger <tschwinger <at>> writes:

> Peter Dimov wrote:
> > Tobias Schwinger:
> >
> >>> Singletons *are* globals.
> >> OK, that was a bit unclear. Let's put it this way:
> >>
> >> It's not globals that are evil but accessing them all over the place
> >> (or "coupling" to use the technical term that describes the problem).
> >
> > Coupling is why people use singletons. Were there no coupling, there would
> > be no need to constrain the number of instances. The whole point of having a
> > single instance it to allow modules to couple themselves to it, and be sure
> > that they can communicate with other coupled modules via its state.
> Thanks for throwing in the wise words, Peter.

Yes. People use singletons to ensure that code is coupled to the decision that
there can be only one instance of a particular class. As you say yourself,
excess coupling is bad. In particular, most code that is coupled using
singletons has no need to know about the "one instance" constraint --- it just
needs *some* instance to use. Sometimes it needs to know that it's the same
instance as one being used elsewhere, but there are other solutions to this than
singletons (e.g. explicitly passing the reference around), which avoid the
coupling to the "there can be only one" design decision.

> With this terminology the moral of Anthony's experience report boils
> down to:
> "Don't write too much code against an interface that will eventually
> change".

That's certainly sensible advice.

In my experience, singletons introduce excessive coupling, and make the code
that uses the singleton hard to test. I am strongly opposed to the use of
singletons in almost all circumstances, and strongly opposed to the idea of any
library designed to make implementing singletons easier.


Boost list run by bdawes at, gregod at, cpdaniel at, john at