• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton pattern  RSS feed

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's difference between "Initialization on-demand holder" and "Early Instantiation" ? are both same approach ? if not which one is better.

thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:What's difference between "Initialization on-demand holder" and "Early Instantiation" ? are both same approach ?

If "Early Instantiation" is the same as "Eager Instantiation", then no; they're basically the opposite of each other.

if not which one is better.

The second.

However, before you start littering your programs with eagerly-instantiated Singletons, you might want to read this.

Winston
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below article says that initialization on demand is better than all.In that object will be initialize on only demand, while eager initialization will initialize object even if object is not in use.

Eager Initialization

In eager initialization, the singleton is instantiated when the programs starts whether or not it gets used. The problem with this approach is that valuable resources are allocated whether or not they ever get used. If the singleton has a large memory footprint, this may not be a good choice.


Initialize-on-demand holder class idiom

There is one final way to get the best of both worlds and that is to use “Initialize-on-demand holder class” idiom which can be found in “Effective Java” by Joshua Bloch. This method relies on the JVM only intializing the class members upon first reference to the class. In this case, we have a inner class that is only referenced within the getInstance() method. This means SingletonHolder will get initialized on the first call to getInstance().

http://www.chiaocheng.com/blog/2009/07/java-singleton/
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:Below article says that initialization on demand is better than all.

Then, IMO, it's wrong.

The IODH idiom is arcane, requires documentation, and is almost certainly slower than eager instantiation. The only time I'd even think of using it was if the Singleton I was defining - if I actually wanted one at all - took so long to create or took up so much space that it affected performance visibly. And if that's the case, I'd probably look at my design very hard before I started dropping in IODH all over the place.

Winston
 
Sresh Rangi
Ranch Hand
Posts: 54
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In eager initialization, the singleton is instantiated when the programs starts whether or not it gets used.


The singleton is not initialized when the program starts. It's initialized when the class is loaded, which is normally right before Singleton.getInstance() is called for the first time. Class loading is already lazy so lazy initialisation is not buying much even if it was expensive.
 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of those articles which wrangled over how to squeeze the last nanosecond out of initializing a singleton object while still remaining thread-safe are old articles.

Since Java 6 the simplest (and IMO best) way to define a singleton is to define it as an enum with one element. As Sresh Rangi points out, class-loading is lazy anyway, so none of the schemes in those old articles are really useful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!