• 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

In which real life senario for Java/j2ee application we are using singleton class

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Singleton class used by an application to ensure that at any time there is only one instance of a class created. You can achieve this by having the private constructor in the class and having a getter method which returns an object of the class and creates one for the first time if its null.



Please suggest me in which real life senario(With Small example) for Java/j2ee application we are using singleton class.


Thanks in advance.

Thanks and Regards,
Sumanta Panda
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sumanta,

the idea of a singleton class is to make sure that there's exactly one single instance of this class at any time when you run the application.

One common scenario for example is to represent a global resource handle like database connections via a singleton object which is shared between all classes or objects which need this resource.

Unfortunately the singleton pattern has become very infamous today because it has many drawbacks and it's easier to misuse the singleton pattern then to use it correctly. Often a singleton like the said database connection is just created to hide the fact that it is used like a global variable because the singleton can be accessed via a static method call which somewhat hides the fact that you have created an implicit dependency. Other things where singletons are (mostly) bad are testability or maintainability.

Things got even worse in a multithreaded environment like web or enterprise applications. It's almost impossible to get a singleton implementation right for concurrent access with multiple threads without a deep understanding of concurrency effects in the JVM.

In my experience singletons are misused most of the time. Besides there are other, much better ways to achieve the real goal of the singleton pattern, i.e. to create a single instance of a class. Dependency injection frameworks can do this for you for example. You shouldn't make yourself too familiar with the singleton pattern

Marco
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Side rant. Feel free to ignore. I am not a fan of these "real life scenario" types of questions... which seems to imply that the programming techniques that you learn in school will be different than what is used, when you graduate. Why should this be so?

Henry
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Marco Ehrentreich.
The example you have mentioned which is good one for understanding the concept.

Thanks and Regards,
Sumanta Panda
 
reply
    Bookmark Topic Watch Topic
  • New Topic