Re: Are Singletons Worth Using?
From: Ilja Preuß (preuss_at_disy.net)
Date: 07/19/04
- Next message: Phlip: "Re: Rework [Was: Static vs. Dynamic typing...]"
- Previous message: Robert C. Martin: "Re: Rework [Was: Static vs. Dynamic typing...]"
- In reply to: Bruno Desthuilliers: "Re: Are Singletons Worth Using?"
- Next in thread: iamfractal_at_hotmail.com: "Re: Are Singletons Worth Using?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 19 Jul 2004 19:11:18 +0200
Bruno Desthuilliers wrote:
> Ilja Preuß wrote:
>> Bruno Desthuilliers wrote:
>>
>>
>>> Firstly, this seems quite specific to a certain category of
>>> languages... In hi-level languages, you can have 'transparent'
>>> Singletons. Look here for a Python exemple :
>>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558
>>
>>
>>> We're in the 21th century now, it would be time for some people
>>> around
>>> to realize that OO does'nt imply Java...
>>
>>
>> Why couldn't the same be implemented in Java??? :confused:
>
> That's a good question. Rereading the code, I'm not sure it couldn't.
> Ask a Java expert...
Well, I am a Java expert. ;)
If I understand the code correctly, it could be implemented in Java in the
same way. But unfortunately I am no Python expert, so I could be missing a
subtle point...
> Anyway the current Java idiom for Singletons seems to be this
> class::getInstance() stuff.
Well, that's what we are used to because of the GoF book, I guess. The
MonoState pattern simply is much less well known, probably because every
book and course on patterns typically just is another take on GoF...
:rolleyes:
>>>> The
>>>> question is whether the extra coupling is a price worth paying for
>>>> enforcing the one and only one instance rule?
>>>
>>> Now this 'extra coupling' is not a Singleton-or-not-singleton issue,
>>> it's a general design problem about concrete class knowledge's
>>> responsability. This issue arise whenever an object instanciate
>>> another one, be it a singleton or not. And then again, there are
>>> patterns to
>>> help solve this (AbstractFactory etc...).
>>
>>
>> The Singleton pattern is not about instanciation, but about a global
>> access point to a single instance.
>
> And ? please re-read more carefully !-)
I did, but it didn't help... ;)
Ah, doing it the fourth time helped, possibly. :)
Yes, the same coupling happens when you call a constructor instead of the
getInstance method (so your phyton solution actually doesn't solve *this*
problem ;). Therefore in some circumstances I would prefer to pass around
the one instance instead of providing a global access point.
Does that compute?
> What this paper mainly says is that
> """
> singleton are bad because
> 1/ the client class has to know that it is using a singleton because
> in
> Java you [have to | usually] use some kind of class::getInstance()
> syntax 2/ the client class knows the concrete class of the singleton
> """
>
> (Well, that's at least what I understood...)
>
> So I was pointing out that
> 1/ may be a Java limitation but has nothing to do with the Singleton
> pattern itself
I don't think this is true - the implementation presented in the above
recipe simply *is not* a canonical Singleton, but some enhanced variation.
The Singleton pattern, as defined in the GoF book, inherently has this
limitation. The Python recipe actually looks much more like a MonoState than
a Singleton. They both solve similar problems, but in different ways.
http://c2.com/cgi/wiki?MonostatePattern
> 2/ is a not a Singleton issue, but an instanciation issue
It's a "global access point" issue, which constructors are a subset of. And
it's amplified by the Singleton pattern, as there is only one instance of
the class (that is, only one single instanciation involved), but typically
dozens to hundreds of calls to the getInstance method (the globale access
point).
Cheers, Ilja
- Next message: Phlip: "Re: Rework [Was: Static vs. Dynamic typing...]"
- Previous message: Robert C. Martin: "Re: Rework [Was: Static vs. Dynamic typing...]"
- In reply to: Bruno Desthuilliers: "Re: Are Singletons Worth Using?"
- Next in thread: iamfractal_at_hotmail.com: "Re: Are Singletons Worth Using?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|