Singleton vs Proxies DP (was Re: Solution: Direct access to Printer I/O lines)

Alex Martelli aleaxit at yahoo.com
Sun Dec 17 18:46:55 CET 2000


"Dinu C. Gherman" <gherman at darwin.in-berlin.de> wrote in message
news:3A3CB83D.45C8775E at darwin.in-berlin.de...
    [snip]
> The GOF book defines a Singleton rather clearly as a
> *unique instance* of a class (while it does not exclude
> that a limited number > 1 of instances might also be
> useful, sometimes). IMHO, a unique identity is part of
> the pattern as you will often have the case where the

I agree that unique id is part of the Singleton Pattern:
that is what makes it problematic.  One does not really
_care_ about that identity, only about the state and
behavior it represents.

> *usage* of a Singleton, i.e. the time windows in which
> it is used by multiple clients do overlap. In fact, I

Yes, that often happens.

> think this is the regular case for this pattern, while
> you seem to argue in favour of use-cases where clients
> immediately drop their handle to that one or *same*
> instance and there is no overlap.

No!  I'm not saying two clients can't hold their
'handles' (lw proxies) at the same time.  It IS
pretty typical.  But it's not at all typical for client
code to be interested in ensuring _identity_ --
state and behavior are typical.


> Of course, you're free to implement the pattern in what-
> ever way you like, but if, as you seem to do, define your
> Singleton pattern as multiple instances sharing behaviour
> *and* state, i.e. while allowing for different identities
> as well (in the general case) I'm tempted to suggest, may-

Right, it gets the gist, except that I don't see where
you get the non-overlap idea you mentioned above.

> be, a different name, like Clone, Replicant or something
> similar. A Proxy, at least to me, always means a place-
> holder for some *other* object, like "intelligent poin-
> ters" or the like. Well, if you add a Decorator to your
> pool of Clones you might get a Singleton again, but then,
> why would I do that? Doesn't a large pool of Clones mean
> a waste of memory?

I know 'Proxy' is the name of other patterns -0- it's just
that I dunno where I placed the C++ Report issue where
the pattern I prefer was first presented, to check up how
the author named it.

A small object takes a tiny amount of memory.  In C++,
a typical 'lightweightclone' would take 8 bytes -- 4 for
the virtual-table pointer, 4 for the data-pointer.  I think
in Python it would be 16 or so.  How many of those *at
the same time* will you have active?  A few hundreds
on a typical PC of today, at most?  I wish my worries
were all as bad as this one!-)


Alex


>
> Anyway, it probably all depends on the the problem you
> want to solve and the forces being at work, as well as
> your personal preferences...
>
> Glad-to-see-more-pattern-threads-again'ly,
>
> Dinu
>
> --
> Dinu C. Gherman
> ................................................................
> "The only possible values [for quality] are 'excellent' and 'in-
> sanely excellent', depending on whether lives are at stake or
> not. Otherwise you don't enjoy your work, you don't work well,
> and the project goes down the drain."
>                     (Kent Beck, "Extreme Programming Explained")





More information about the Python-list mailing list