Re: Diff. between singleton class and static class

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Jeff Louie <jeff_louie@xxxxxxxxx> wrote:
> First, the Singleton Pattern generally creates a single instance of a
> class, but this is not absolute. In fact, you can argue that some of
> the flexibility of the Singleton Pattern is that it allows you the
> programmer to return more than one instance of a class at a later
> date without breaking the client code.

I'd argue that in that case it's a factory. Certainly the GoF book
defines a singleton in terms of only providing one instance. I agree
that using a singleton allows you to change it into a factory at a
later date without breaking client code though. (Assuming the client
code isn't relying on it being a singleton.)

<snip>

> There _are_ advantages to the Singleton pattern and they include:

<snip>

> 3) Allows sub-classing.

It allows the singleton to be a subclass of something else. The normal
patterns that ensure that only a single instance is ever created don't
work well with subclasses. It can be done, but it's somewhat messy.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: A Singleton with Inheritance
    ... a JavaScript singleton pattern. ... However, I would argue that creating an object with methods to be invoked (something reasonably like a singleton), rather than creating an function to be instantiated, might help someone who uses my code to understand how I intended them to use it, though I guess there is nothing like good documentation. ... It is prototype based inheritance, where you inherit from a specific instance. ...
    (comp.lang.javascript)
  • Re: [PHP] Singleton pattern question
    ... singleton pattern the result would be what you expect. ... >class reads the system configuration and returns the ... Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. ...
    (php.general)
  • Re: Singleton
    ... Responding to Mingins... ... > question was more to do with alternatives to the Singleton pattern. ... there are multiple contexts where it can instantiated. ...
    (comp.object)
  • Re: Are Singletons Worth Using?
    ... I am a Java expert. ... >> The Singleton pattern is not about instanciation, ... > 1/ the client class has to know that it is using a singleton because ... The Singleton pattern, as defined in the GoF book, inherently has this ...
    (comp.object)
  • Re: where do I put global data?
    ... IMK, the singleton instance will be created in heap memory, and static class ... > In what ways is the Singleton pattern better for this (I really want to ... >> contain a Globals singleton, a DatabaseManager singleton, a MapPointWS ... >> private static Globals instance; ...
    (microsoft.public.dotnet.framework.compactframework)