Re: Singletons
On 11/25/2012 12:58 AM, Daniel Kr?gler wrote:
Signeton is an *object* that is accessible from anywhere in the
application, and *that* instance is surely the same.
This is certainly also some design pattern, but it is not the classic
singleton pattern. The classic source ("Design Pattern" from the Gang of
Four) defines the intent as
"Ensure a class only has one instance, and provide a global point to
access it"
Similarly the definition found here
http://en.wikipedia.org/wiki/Singleton_pattern
starts with
"the singleton pattern is a design pattern that restricts the
instantiation of a class to one object"
For some definition of "classic". GOF did not invent design patterns,
just tried to describe those already established in practice. Doing a
fine job in general, as it helped to clear the view and streamline
communication.
Particularly for Singleton they went sideways. As we used them in
different forms, but i can;t recall anyone struggling with the 'class
ensures single instance' part (at least on the C++ scene) ever. (It
might make nmore sense in java not having free functions, there
MyClass::Instance makes some sense.)
After the book people started to aim implementing its content, so some
singleton class implementations surfaced (I recall Meyers and
Alexandrescu singletons, and the leaky one without a name attached).
Those were fine additions, but IMO should no cloud our general
judgement, or especially replace the originally established uses to the
new-and-shiny ones mindlessly.
Like std::cout, cerr are proper singletons. But they share a class, and
you can have any number of other ostreams.
You may describe them as singletons but they are not so by the classic
design pattern definition.
My GOF book states (C) 1995. While std::cin is really younger, but it's
original iostream.h version is way older, so how the late-comer grabs
the 'classic'? ;-)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]