Re: Confused about a thread-safe singleton example.
Alan Johnson <awjcs@yahoo.com> writes:
1. Don't use singletons. Ever. Pretty much all of the value of the GoF
Design Patterns book is negated by the fact that they chose to
legitimize Singleton as a design pattern. Singleton is just a fancy
name for global variable. We should call it the Global Variable
(anti)Pattern.
In the C++ "Hello world!" program, usually ?::std::cout? is used,
which seems to be a ?global variable? to me.
(In Java, ?java.lang.System.out? has the same r?le.)
Do you see a way to get rid of it (Possibly by a redesign of
the library)?
By a redesign of the language we could use:
int main( ::std::env & env )
{ env.out << "Hello World!\n"; }
This env would have to be passed to every function that wants
to print something to env.out. Assume that f and g do not need
to print something:
int main( ::std::env & env )
{ f(); }
void f(){ g(); }
void g(){ }
However, if one detects later that one wants to use ?env.out?
in g, one not only needs to change g, but the whole ?call chain?
(here: f), to allow this:
int main( ::std::env & env )
{ f( env.out ); }
void f( ::std::ostream & stdout ){ g( stdout ); }
void g( ::std::ostream & stdout ){ stdout << "alpha"; }
Possibly, one might say that ?env? has low cohesion, because
it is a collection of different things, which only share that
they used to be global before.