Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Singleton Question

Reply
Thread Tools

Singleton Question

 
 
Dave Monroe
Guest
Posts: n/a
 
      06-30-2004
Can somebody point me to a spec for singleton implementation?

I'm working with an application that has a couple of singletons and
obviously the member variables are kept separate in the threads that
use the singleton, but I can't find any documentation that explains
the rules.

TIA

Dave Monroe
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      06-30-2004
Dave Monroe wrote:
> Can somebody point me to a spec for singleton implementation?


Singleton is a pattern, not a specification. There is a description of
the general pattern on page 127 of GoF. In general, Java
implementations use the static initialization time feature to avoid
overhead when retrieving an object, as such:

public class MySingletonClass
{
private static MySingletonClass instance = new MySingletonClass();

public static MySingletonClass getInstance()
{
return instance;
}

private MySingletonClass() { }

...
}

Unlike similar-looking code in other languages, this will *not* create
an instance of MySingletonClass until the first time getInstance is
called (or the first time MySingletonClass is loaded directly from a
classloader with an explicit request to initialize it). Essentially,
lazy initialization is provided for you as a feature.

> I'm working with an application that has a couple of singletons and
> obviously the member variables are kept separate in the threads that
> use the singleton, but I can't find any documentation that explains
> the rules.


What you're describing doesn't really sound like a Singleton at all.
Singletons are shared objects, and are intended that way. They
generally have little or no state at all, and thread-local state is
certainly not a part of the pattern.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      06-30-2004
Chris Smith wrote:

> Dave Monroe wrote:
>
>> Can somebody point me to a spec for singleton implementation?

>
>
> Singleton is a pattern, not a specification. There is a description of
> the general pattern on page 127 of GoF. In general, Java
> implementations use the static initialization time feature to avoid
> overhead when retrieving an object, as such:
>
> public class MySingletonClass
> {
> private static MySingletonClass instance = new MySingletonClass();
>
> public static MySingletonClass getInstance()
> {
> return instance;
> }
>
> private MySingletonClass() { }
>
> ...
> }


My normal way in Singleton handling is instead:

public class MySingletonClass
{
public static final MySingletonClass instance = new MySingletonClass();

private MySingletonClass() { }

...
}

Which are pros and cons of my vs your way ?

- Dario
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-30-2004
Dario wrote:
> Which are pros and cons of my vs your way ?
>


Not a lot. I guess I write a getXXX method because it more closely
resembles the way singleton is often implemented in other languages.
Either is equally valid, though.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Lance Lamboy
Guest
Posts: n/a
 
      06-30-2004

>> public class MySingletonClass
>> {
>> private static MySingletonClass instance = new MySingletonClass();
>>
>> public static MySingletonClass getInstance() {
>> return instance;
>> }
>> }
>> private MySingletonClass() { }
>>
>> ...
>> }
>> }

> My normal way in Singleton handling is instead:
>
> public class MySingletonClass
> {
> public static final MySingletonClass instance = new
> MySingletonClass();
>
> private MySingletonClass() { }
>
> ...
> }
> }
> Which are pros and cons of my vs your way ?


I would recommend hiding accessing to the singleton through a public
method. That way you preserve flexibility. You could update the
previously mentioned MySingletonClass to the below code without anyone
else having to change their code.

public class MySingletonClass // used to be a singleton but changed for
operational reasons
{
private static MySingletonClass a = new MySingletonClass(); private
static MySingletonClass b = new MySingletonClass();

public static MySingletonClass getInstance() {
if ( delta ( ) return a ; )
return ( b ) ;
}
}
private static boolean delta ( ) { ... }

private MySingletonClass() { }

...
}
}

> - Dario


--
Lance Lamboy

"Go F*ck Yourself" ~ Dick Cheney

 
Reply With Quote
 
Gorf
Guest
Posts: n/a
 
      06-30-2004
"Chris Smith" <> wrote in message
news:.. .
> Dario wrote:
> > Which are pros and cons of my vs your way ?
> >

>
> Not a lot. I guess I write a getXXX method because it more closely
> resembles the way singleton is often implemented in other languages.
> Either is equally valid, though.


I do it a slightly different way, lazy instantiation...

public class MySingletonClass {

private static MySingletonClass instance;

public static MySingletonClass getInstance() {
if (instance == null)
instance = new MySingletonClass();
return instance;
}

private MySingletonClass() { }

...
}

It's got an if-statement in there, but for some reason, I like it this way


--
Gorf


 
Reply With Quote
 
Keith James
Guest
Posts: n/a
 
      06-30-2004

[...]

Gorf> I do it a slightly different way, lazy instantiation...

Gorf> public class MySingletonClass {

Gorf> private static MySingletonClass instance;

Gorf> public static MySingletonClass getInstance() {
Gorf> if (instance == null)
Gorf> instance = new MySingletonClass();
Gorf> return instance;
Gorf> }

Gorf> private MySingletonClass() { }

Gorf> ...
Gorf> }

Gorf> It's got an if-statement in there, but for some reason,
Gorf> I like it this way

Lazy, but not threadsafe. You're much better off with Chris'
suggestion which is both.

--

- Keith James <> Microarray Facility, Team 65 -
- The Wellcome Trust Sanger Institute, Hinxton, Cambridge, UK -
 
Reply With Quote
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      06-30-2004
Lance Lamboy wrote:

> I would recommend hiding accessing to the singleton through a public
> method. That way you preserve flexibility. You could update the
> previously mentioned MySingletonClass to the below code without anyone
> else having to change their code.


Ok. Thanks.

- Dario
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-30-2004
Gorf wrote:
> I do it a slightly different way, lazy instantiation...
>


Here's probably more than you want to know...

The reason is rather subtle, but both of our pieces of code do lazy
instantiation. If I write:

public class MySingletonClass
{
private static MySingletonClass instance = new MySingletonClass();
...
}

That new object is created at class initialization time. In Java, class
initialization happens directly prior to the first access to the class,
and *not* at the beginning of the application.

Java is primarily designed that way because there's no way for the JVM
to know all the classes that will be used when the application first
begins -- as opposed to many other languages where an explicit link step
provides this information. In this case, though, we can take advantage
of this fact (which is specified, and therefore guaranteed) to delay
initialization of a singleton instance.

The primary advantage of the code as I wrote it above is that it is
implicitly thread-safe to retrieve the singleton instance. You could
provide this thread-safety explicitly in your version by synchronizing
the getInstance method, but there are several reasons it's at least
marginally better to go with class initialization:

1. The thread synchronization for class initialization is going to
happen anyway (to set your instance field to null, for example),
so you'd just be introducing redundant synchronization.

2. Synchronization can be done better by the JVM. It knows that
class initialization can only happen once, so it can (and almost
certainly does) actually mutate the code so that there is zero
cost for the synchronization after class initialization is
complete.

You may not be working in a multithreaded environment (although that's
hard to imagine in Java), or you might not be concerned about
performance, but when the better and faster technique is also easier to
read and write, it makes a good default to use that technique.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
GaryM
Guest
Posts: n/a
 
      06-30-2004
Keith James <> wrote in
news: .uk:

>
> Lazy, but not threadsafe. You're much better off with Chris'
> suggestion which is both.
>


Not to go off on a tangent, but I was reading about double check
locks recently. Does this make his example thread safe:

if (instance == null) {
synchronized(this) {
if(instance == null)
instance = new MySingletonClass();

}
}
return instance;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments