Singleton

From: Shane Mingins (shanemingins_at_yahoo.com.clothes)
Date: 10/20/03


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


Relevant Pages

  • 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: Singleton --- almost
    ... > Singleton because only one instance is needed: ... > design patterns to apply, instead of trying to judge which design patterns are ... > pattern is relatively easy to understand, ... It may be useful to have a registry of particular objects used by ...
    (comp.lang.java.programmer)
  • Re: Singleton VS Static Class
    ... I dislike the classical version of the Singleton pattern. ... the problem had shown the need for a special instance which should be easiliy ... nothing wrong with globals if handled correctly), ...
    (comp.lang.java.programmer)
  • Re: Creating framework for singleton pattern?
    ... >> question the usefulness, in the long run, of the Singleton pattern. ... The wiki page has a long quote from Kent Beck, ... "The Singleton Design Pattern Does More Harm Than Good" ... misused by allowing the creation of global variables in an OO context, ...
    (microsoft.public.dotnet.languages.csharp)