Re: Singleton VS Static Class
- From: "Chris Uppal" <chris.uppal@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 10 Jul 2006 11:31:06 +0100
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
.
- Follow-Ups:
- Re: Singleton VS Static Class
- From: dsjoblom
- Re: Singleton VS Static Class
- References:
- Singleton VS Static Class
- From: howachen
- Singleton VS Static Class
- Prev by Date: Re: Singleton VS Static Class
- Next by Date: Re: tuning the GC
- Previous by thread: Re: Singleton VS Static Class
- Next by thread: Re: Singleton VS Static Class
- Index(es):
Relevant Pages
|