Singleton
From: Shane Mingins (shanemingins_at_yahoo.com.clothes)
Date: 10/20/03
- Next message: Donald Roby: "Re: Basic books on OOA&D"
- Previous message: Jacob Atzen: "Re: Basic books on OOA&D"
- Next in thread: H. S. Lahman: "Re: Singleton"
- Reply: H. S. Lahman: "Re: Singleton"
- Reply: Michael Feathers: "Re: Singleton"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 20 Oct 2003 11:07:09 +1300
Hi
The GOF book defines the Singleton pattern as "Ensure a class only has one
instance, and provide a global point of access to it."
So if you have a class that ensures that there is only one instance, but the
point of access is not global to the entire system, is it still considered a
Singleton? Or is global to a package still global (i.e. global in context)?
A database connection is a common example used when discussing the Singleton
pattern. I am wondering what alternatives there are to the Singleton and
some of the issues around deciding whether to use a Singleton or an
alternative?
The alternatives I have read about include:
The MonoState
Pass Variables Around
A ContextObject
In PofEAA Martin describes the Registry pattern. He says that the Registry
is still global data and he is therefore uncomfortable using it. Basically
it becomes a last resort. But the couple of alternatives he mentions also
have problems.
I am in the early chapter of PofEAA so maybe there are some answers
elsewhere in the text (point me there if that is the case *grin).
In the application that I am currently (forever *grin*) working on I am
going to be using a O/R mapping tool. It will manage the database
connections (i.e. it actually manages a pool for me) but I only want one
instance of a connection to this O/R tool (so similar to examples of only
one instance to a database connection). So I think ... Singleton. And
perhaps I start with that until things say otherwise. But what other
options do I have?
In Kent's example http://www.c2.com/cgi/wiki?SingletonGlobalProblems where
he refactored out a Singleton it appears that it was the wrong pattern to
use to begin with.
"the rates were different. This is not a case where the rates actually came
from a single, external source (for instance, supplied by an outside bank).
Here you had rates and prices that could vary, so the Singleton was an
obviously wrong choice. Had there been but one path to conversion rates and
prices the Singleton would have made all the sense there is. "
When I read the example of Kent's it was not a case of choosing an
alternative to a Singleton but that this was not global data with one
instance of it.
But "Had there been but one path to conversion rates and prices the
Singleton would have made all the sense there is. " So if this had been the
situation are there also other options to the Singleton?
Thanks
Shane
-- shanemingins@yahoo.com.clothes remove clothes before replying "It is not the strongest of the species that survive, nor the most intelligent, but the one most responsive to change." --- Charles Darwin
- Next message: Donald Roby: "Re: Basic books on OOA&D"
- Previous message: Jacob Atzen: "Re: Basic books on OOA&D"
- Next in thread: H. S. Lahman: "Re: Singleton"
- Reply: H. S. Lahman: "Re: Singleton"
- Reply: Michael Feathers: "Re: Singleton"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|