Re: Singleton --- almost

From: Andrew McDonagh (news_at_andrewcdonagh.f2s.com)
Date: 03/09/05


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



Relevant Pages

  • Re: design pattern
    ... The pattern is called Inversion of Control. ... and calls a factory to get an object instance; ... > settings and, in runtime, the factory reads settings and return the ... > -Singleton/singlecall: the returned object can be singleton or singlecall ...
    (microsoft.public.dotnet.framework)
  • Re: RMI, singletons and multiple classloaders in Weblogic
    ... With the Singleton ... >>pattern, that registry is within the class itself. ... GoF pattern: I'd nominate one of "Decorator", "Template Method", or ... "Factory / Factory Method" for that honor. ...
    (comp.lang.java.programmer)
  • Re: Singleton
    ... I don't usually go to a string factory or a list factory to ... If you have an abstract DOM interface, for example, and the implementation ... To me it's very clear to go to the DomFactory or singleton ... As mentioned in my posting you unit test a class separate from the ...
    (comp.object)
  • Re: Software design patterns: encapsulation & object identity
    ... >> constructs, maintains, and issues GOF Singleton instances of a Widget. ... >> Clients of the Factory instance obtain the Widget Singleton instance ... >> APIs) the fact that the instances issued to clients are Singletons? ...
    (comp.object)
  • Re: relationship of Factory method and singleton design patterns?
    ... > A factory method is one that invariably creates an object. ... strong definition of a singleton!). ... be marked as singleton or single call, from the client perspective there is ...
    (comp.object)