Boost logo

Boost :

From: Gennadiy E. Rozental (rogeeff_at_[hidden])
Date: 2001-11-14 11:01:03


--- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> From: <scleary_at_j...>
> > Like you, I'm more inclined to just declare global resources as
normal
> > objects, and just create instances of them in main(). I have
never used
> an
> > actual Singleton except as mentioned above.
>
> I have encountered the need to solve the init order problem many
times. I
> automatically type
>
> std::list<win32::dialog*> & instances()
> {
> static std::list<win32::dialog*> i;
> return i;
> }
>
> > So, maybe we don't need one after all?
>
> _I_ don't need one provided that I can (easily) make the above
thread safe.
> This is not yet covered by Boost to the extent that I'd have liked.
>
> But if we move from technical to psychological reasons, many
developers
> think that a design pattern that has such a cool name absolutely
needs to
> have an associated class (template.) The next trap they fall into,
of
> course, is in thinking that making a global variable a 'Singleton'
somehow
> makes it less global and hence, acceptable. ;-)
>
> BTW I agree with your analysis on singleton policies. A singleton -
C++
> sense - is an object that logically always exists for the duration
of the
> program. It's as if it had been constructed at compile time and is
never
> destroyed, always ready to be used.
>
> Adding 'policies' simply means that the object is no longer a
singleton, but
> something else.

GoF defines Singleton as a pattern that ensures a class only has one
instance and provide a global point of access to it. GoF does not
elaborate the lifetime policy nor put any restriction on it. You can
have a singleton that exist from start to end, or you can have one
that exist only when used. Anyway, the name is not important. What I
am looking for is a shared resource with a managed lifetime policy, a
synchronization policy and switchable traits that will allow to
manage such things like type of instance().
  I do not propose to use the same class for every possible
situation. I hope to use generative power of C++ templates to create
different classed from the same source.
  Simple solution for simple needs. Singleton is really only a simple
implementation tool. But if you need to write a wrapperaround it
every time you use it - I would better write it from scratch. On the
other hand singletin could be made powerfull enough to cover most of
possible usage scenarios.

Gennadiy.

>
> --
> Peter Dimov
> Multi Media Ltd.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk