• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt abt SingletonPattern

 
Maneshi tuli
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All ,
I have some doubt abt benifits of singletonPattern as it mentioned in Sun Certified guide by Mark Cade.

- Permits a variable number of instances.

What does this mean. As singleton permits only one single instance of the class. Do what does this means.

regds
Manishi
 
Goan Balchao
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but you can / could control the number of instances that should be available by holding a counter / refernces - caching.

Although, it would not strictly be a "Singleton", it would be a Singleton for all practical purposes.
 
Deepak Pant
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneshi tuli:
All ,
I have some doubt abt benifits of singletonPattern as it mentioned in Sun Certified guide by Mark Cade.

- Permits a variable number of instances.

What does this mean. As singleton permits only one single instance of the class. Do what does this means.

regds
Manishi


Maneshi,

Most of the times Singleton is used in such a fashion that there is only one instance across the system.

But Cade's interpretation is correct. The creation of the instance of the Singleton class either lies with the class itself or with a Singleton factory. In either case the number of instances to be created can be controlled. Hence it permits variable number of instances.

regards,
Deepak
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak,

You said:

Most of the times Singleton is used in such a fashion that there is only one instance across the system.


My beloved guide "Sun Certified Enterprise Architect for J2EE, Study Guide" by Paul Allen and Joseph Bambara says the following on page #219:

Singletons really are "per classloader" and in a J2EE application, many developers make the mistake of assuming that a singleton really is a singleton in a cluster of application servers. This is not true!


Regards,
Dan
 
Deepak Pant
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan,

You are absolutely 100% correct. Your guide is correct.

When I said "system" I meant one JVM. In a cluster implementation there would be one instance per JVM running. System is a very vague term, I should have clarified my definition.



regards,
Deepak
 
Maneshi tuli
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everybody .
I was confused with this statement that
"There shd be only one instance of the singletone class per class loader".

Thks to everybody to make my clear.

Manishi
 
Ghulam Rashid
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this article useful -
http://www.javaworld.com/javaworld/jw-01-2001/jw-0112-singleton.html

From the Article
------------------
In the Singleton design pattern you never have more than one instance of a singleton class. Yet sometimes you may find that, unexpectedly, more than one singleton instance exists. Some of these circumstances have to do with the quirks of special class loaders, multithreading, distributed systems, and object serialization, which makes them all the harder to figure out when they do occur. In this article, Joshua Fox explains how to watch out for those pitfalls and how to avoid the oxymoronic multiple singleton. (2,700 words)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic