Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

singleton class

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where we use singleton class in realtime i mean i need an example of real usage ?
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Singleton pattern and its usage is usually discussed over in the "OO, Patterns, UML and Refactoring" forum. You can get loads of information by searching for "singleton" in that forum.
However, the bottom line is that you should really think very hard before using a singleton. There are a lot of significant and often unexpected problems which can occur in systems which use singletons. Most situations in which I have seen a singleton used, could have been done more effectively with other approaches.
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think JDBC pool is a good example of using singleton. Since you don't want to have 100 pools that open 100 connection each. That's 100,000 connections!!!
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you don't want 100 pools, but why can't you just pass the pool in to your method as a parameter, or store it in an application context, or fetch it from JNDI, or whetever else takes your fancy? Just because you only want one of something right now, does not automatically imply that it should be a Singleton.
Consider what might happen if you need to connect to two databases, but your database access code assumes a Singleton? Consider what might happen if you need to test your software on a machine that cannot access your database - can you easily and safely provide a "test database" implementation to your code?
Please go and read all the Singleton discussion over in the OO and patterns forum. There's a lot of deep consideration of such points, and more.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to Frank's argument, what if you are in a clustered environment or your application/web server uses a JVM pool? Your singleton is now no longer a singleton, you will have one instance for each JVM. You could overcome this by using an EJB (or CORBA object) to manage the singleton, but then performance will suffer. The bottom line is you can accomplish the same thing without risking the assumption that the object has the same identity across the application.
 
eswar kumar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please consider my question....
 
Marshal
Posts: 75648
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

eswar kumar wrote:Please consider my question....

Have you really waited 5 years for an answer?
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

eswar kumar wrote:Please consider my question....


We did. The answer is, don't use it. Or at least, start with the assumption that a Singleton is a poor design choice. Look at all the alternatives, and pick one.

Just because it was in the Gang of Four patterns book does not mean you should actually use it.
 
money grubbing section goes here:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic