Re: Singleton VS Static Class



howachen@xxxxxxxxx wrote:

Which one you prefer....

Neither.

First off, it's very, very, unlikely that a class with a name like Person
should have only one instance (or act as if it had only one instance).

Secondly, I dislike collecting complicated state into a class. If there is a
coherent cluster of data in the system then it should be owned by a real
object, not attatched to a class as static data. Still, at least your version
uses explicit static methods, rather than the "monostate" pattern which has
been discussed elsewhere in this thread -- and which is a total abomination
IMO.

Thirdly, I dislike the classical version of the Singleton pattern. For too
many people the emphasis is on there only being one instance permitted, and
that is wrong IMO. The valuable thing about Singleton might better be
expressed with a name like "Distinguished Instance" -- i.e. there is one (or
maybe more than one) instance which is special in some way. Your code, then,
follows the classic pattern in that it hard-wires the dangerous (and pointless)
assumption that only one Person should ever be created. Now, if analysis of
the problem had shown the need for a special instance which should be easiliy
available to arbitrary callers (i.e. the equivalent of a global -- and there is
nothing wrong with globals if handled correctly), then the method for getting
that instance would not be the bland getInstance() but would be something
meaningful -- getGlobalLogger() or getSystemOwner() or something like that.
So, while I have nothing against my version of Singleton, the code you exhibit
not something I would like to see in a real system.

-- chris


.



Relevant Pages

  • Re: where do I put global data?
    ... ways is the Singleton pattern better for this (I really want to know, ... > contain a Globals singleton, a DatabaseManager singleton, a MapPointWS ... > leveraged from all forms and other classes in the app. ...
    (microsoft.public.dotnet.framework.compactframework)
  • 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 Pattern
    ... I have a question about singleton pattern. ... the Plant class is now coupled to LightSourceSingletonFactory and the ... LightSource interface. ...
    (comp.lang.java.programmer)
  • 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)
  • Re: [PHP] nested, referenced foreach & implicit current array pointer issues
    ... If PHP was statically typed, global variables would still be a bad ... They are bad smell in C++ and Java, ... Nopthing wrong with globals as long as they aren't used to punt data ... class Foo extends Singleton ...
    (php.general)