Re: Singleton --- almost
From: Andrew McDonagh (news_at_andrewcdonagh.f2s.com)
Date: 03/09/05
- Next message: Daniel Dyer: "Re: Is it possible!"
- Previous message: kyancy_at_gmail.com: "Re: How 2 get single JDOM doc of XML schema that also includes external schemas"
- In reply to: John C. Bollinger: "Re: Singleton --- almost"
- Next in thread: Andrew McDonagh: "Re: Singleton --- almost"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 09 Mar 2005 19:19:41 +0000
John C. Bollinger wrote:
> Ryan Stewart wrote:
>
>> "Andrew McDonagh" <news@andrewcdonagh.f2s.com> wrote in message
>> news:d0im8g$1pm$1@news.freedom2surf.net...
>>
>>> Most of the other replies have stated the various problems associated
>>> with general singleton usage.
>>> John Bollinger's posting is very good about the typical problems
>>> people get into with them and why.
>>>
>>> If ease of access to the single instance is truly required, then the
>>> singleton Registry can help a lot.
>>>
>>
>> As I mentioned in my reply to John's post, it's not so much a problem
>> of ease of access to an instance as to enforcing a single instance
>> instead of creating thousands of them when one will do.
>
>
> Whether the registry is in the proposed Singleton class or outside it,
> having one (which need not be explicit) addresses both problems. They're
> closely related anyway, for if an object already has ready access to an
> instance it may use, then it doesn't need to create a new one.
>
>> I've considered the factory idea. The question remains, though,
>> whether making something a singleton to prevent thousands of
>> unnecessary instances is a valid reason for using the pattern.
>
>
> No. The class should accurately abstract the thing it represents, and
> there are very few cases where a good abstraction has Singleton nature.
> The mode of use of a class and its instances is in most cases a
> question separate from the class itself -- if you want to manage that
> then you may have use for an additional class to assist you. That's
> where Andrew's suggestion of a Factory comes in, or my suggestion of a
> registry. (A Factory is a generalization of the registry idea.)
>
>> Would you use the factory to limit the number of instances of an
>> object that are handed out, i.e. making a singleton without applying
>> the pattern to a class?
>
>
> I don't know about Andrew, but I might do so if I found the number of
> instances to be a problem. However,
> (1) I would be more likely to try to handle the situation by caching one
> or a few instances where they were needed, and / or by passing them as
> method arguments, than to pursue a design where a Factory would be
> useful in limiting the number of instances; and
> (2) Ensuring that there is only one instance is not at all the same
> thing as applying the Singleton pattern. This is exactly where many of
> us started in our responses to your original message.
>
John,
You have nicely summed up my preferences there.
A good white paper by J. B. Rainsberger on singletons can be found ...
http://www-106.ibm.com/developerworks/webservices/library/co-single.html
- Next message: Daniel Dyer: "Re: Is it possible!"
- Previous message: kyancy_at_gmail.com: "Re: How 2 get single JDOM doc of XML schema that also includes external schemas"
- In reply to: John C. Bollinger: "Re: Singleton --- almost"
- Next in thread: Andrew McDonagh: "Re: Singleton --- almost"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|