• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bill Pugh Singleton approach

 
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill Pugh Singleton approach says that instance is created only once even if multiple threads call the getInstance(). I don't understand because I think that every time a thread calling an getInstance () creates a new Instance as it is not checking null check for instance creation. Please help me understand in this regard.
1.JPG
[Thumbnail for 1.JPG]
 
Ranch Hand
Posts: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as you see it creates INSTANCE as a static final variable which means a variable is defined before class instantiated so any time only one instance is
available. In method it is returning that variable only not instantiating again.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a form of lazy initialization.

While using singletons is questionable, lazily instantiating them is a sure sign of bad application design.

It's good to understand the pattern, but never use this in real life, no matter how pervasive this anti-pattern is in online articles.
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one of the programmer here suggested to use like this, so can i go ahead with his approach
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can do this, as long as you're aware that it doesn't do the same thing.

Why do you think you need a singleton, Rajeev?
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a far better way to get a singleton:-You can add fields, methods, etc., as required.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
It's good to understand the pattern, but never use this in real life, no matter how pervasive this anti-pattern is in online articles.



Not sure I'd call it an anti-pattern, as it doesn't make anything worse than a regular singleton would.
It's just pointless, and shows a lack of understanding of how Java loads classes.
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to reuse the same object(DBService) instead of creating every time on every thread call.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Not sure I'd call it an anti-pattern, as it doesn't make anything worse than a regular singleton would.


At least regular singletons solve a common problem (although in a bad way).

When you feel the need to load them lazily, you definitely need to go back to the drawing board.

Rajeev Pedada wrote:I just want to reuse the same object(DBService) instead of creating every time on every thread call.


So why not just pass the DBService in a constructor, and save it in a private instance field?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And passing it in a constructor at least allows you to inject a mock version in for unit testing without much trouble.
Using a singleton is a pain to test.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Stephan van Hulst wrote:
It's good to understand the pattern, but never use this in real life, no matter how pervasive this anti-pattern is in online articles.



Not sure I'd call it an anti-pattern, as it doesn't make anything worse than a regular singleton would.
It's just pointless, and shows a lack of understanding of how Java loads classes.


I doubt that Bill Pugh had anything but a very good understanding of how Java loads classes. His work influenced changes to Java Memory model after all.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can also see this in wiki https://en.wikipedia.org/wiki/Singleton_pattern#Initialization_On_Demand_Holder_Idiom
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
I doubt that Bill Pugh had anything but a very good understanding of how Java loads classes. His work influenced changes to Java Memory model after all.



Well, if someone is using pre-1.5(is it?) Java then possibly. It's not been relevant for about a decade, unless you force load a class before you actually use it.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean Dave. The code in the very first post is the best way to instantiate globally accessible objects only on demand. It only works reliably under the Java 5+ memory model.

Regarding the general discussion, I'm not attacking the validity of the code. I'm just saying it's useless.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I'm not sure what you mean Dave. The code in the very first post is the best way to instantiate globally accessible objects only on demand. It only works reliably under the Java 5+ memory model.



What I'm saying is, since at least 1.5 the use of lazy-loading is unnecessary for a singleton, and I'm not entirely clear what it was about the class loading pre-1.5 that needed it...it has been 10 years after all.
Unless your singleton has more public static things than the getInstance() method then it's a pointless exercise. And I haven't seen one of those. Then again, I haven't written a regular singleton for the best part of a decade anyway.
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic