• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bill Pugh Singleton Pattern Sample code?  RSS feed

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I can't understand the logic in bill pugh's code:



In this code, which class is singleton: the Something or the Lazyholder class?
And also, how can I use the said class? Sorry noob here and cant understand it

It's because I have to use an API Class method. Its specification is singleton. Right now I am using Spring Web MVC.
Can someone please help me... TIA
 
Saloon Keeper
Posts: 3331
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:Hi guys, I can't understand the logic in bill pugh's code:


Unless I'm missing something (pun), I don't see how it would be any better than
 
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't any better, unless Something has another static thing that can be accessed:

In the above, if another class accessed SOME_CONSTANT before someone used getInstance then the instance constant would be instantiated. In the LazyLoad case in the OP it wouldn't be.

I have never seen a situation where the LazyLoad version is needed, it has to be said.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Winston Liek wrote:Hi guys, I can't understand the logic in bill pugh's code:


Unless I'm missing something (pun), I don't see how it would be any better than

The difference is that this doesn't do a "lazy load" of the object referenced by INSTANCE. Pugh's version only instantiates the singleton when the getInstance() method is first invoked. I'll post some code in a bit.

Edit: Although I think Dave Tolls has a good point in that without some other static member that can be accessed, which my sample code below does have, there's really not much difference between the lazy loading version and the eager loading version.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code to demonstrate the sequence in which things get executed:

And this is the output:

Lazy Load
LazyLoaded class is loaded.
LazyLoaded.foo() invoked
Holder is loaded...
private LazyLoaded() invoked...LazyLoaded.INSTANCE created and assigned.
LazyLoaded.INSTANCE.bar() invoked

Eager load
EagerLoaded class is loaded
private EagerLoaded() invoked...EagerLoaded.INSTANCE created and assigned
EagerLoaded.foo() invoked
EagerLoaded.INSTANCE.bar() invoked


With LazyLoaded, the constructor is not invoked even though the class itself is loaded. It only gets invoked when the getInstance() method is invoked.

With EagerLoaded, the constructor is invoked when the class in loaded.

Lazy loading the singleton instance is handy when it's relatively expensive to instantiate the object.

Winston Liek wrote:In this code, which class is singleton: the Something or the Lazyholder class?

the singleton class is Something; LazyHolder is just a "holder" for the lazy-loaded singleton instance.

Google for Double-Checked Locking is broken
 
Dave Tolls
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Lazy loading the singleton instance is handy when it's relatively expensive to instantiate the object.


But only if there's something else on the singleton class that makes lazy loading meaningful.

I remember having a discussion on this a couple of years ago on the OTN forums, and the general conclusion (apart from "avoid singletons") was that it was largely pointless. I don't think anyone could remember an example which would have benefited from it.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
But only if there's something else on the singleton class that makes lazy loading meaningful.

You're quite right. The demo code I gave can be modified to eliminate the static foo() method in both classes to show that execution flow will then be pretty much the same. Thanks for pointing it out. I can vaguely remember only one time where we used Pugh's lazy holder technique to lazy load configuration settings from a database. 99% of the time, we just go with eager loading with a private static initialization. That's my story and I'm sticking to it

Honestly, I don't understand the obsession many programmers seem to have with gut-based optimization. The eager loading code, as seen in this thread, is easier to grok and you don't get as many programmers scratching their heads wondering what's with all these extra gyrations. At the very least, you'd have to add some implementation notes around the lazy holder class to make sure the intent and basis for the design decision is understood by newcomers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!