• 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

Singleton Pattern

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I want to know is it good to implement Singleton Pattern?. If Yes how a single instance can be used for several requests? Is there any flaw in implementing Sigleton pattern? I found somewhere we can have more than one instace while implementing Sigleton pattern...
Thanks in advance
Srinivas Ivaturi
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to know is it good to implement Singleton Pattern?

The idea with design patterns is that they're proven solutions for certain kind of problems. Whether the Singleton is good for you right now is impossible to tell unless you describe in a bit more detail what you intend to do.

If Yes how a single instance can be used for several requests?

By using the same object reference from several threads?

Is there any flaw in implementing Sigleton pattern?

???

I found somewhere we can have more than one instace while implementing Sigleton pattern...

Yes, against a common belief, the GoF book defines Singleton as *any* fixed number of instances, not just one.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all i really remember is it avoids the sychronisation complexity of threads. i used it for my servlets. i think perhaps it is less scalable? not sure.
[ January 19, 2004: Message edited by: Randall Twede ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Randall Twede:
[QB]all i really remember is it avoids the sychronisation complexity of threads.


Does it? In which way???
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One instance in which there can be several instances of a Singleton where such is NOT what we want (or often anticipate) is when having distributed applications (mainly EJB but Servlets can also suffer).
In these cases a single instance of the singleton will be created inside each application server (or more correctly virtual machine, maybe per web application).
Unless the programmers understoof and anticipated that this may happen it can lead to very nasty problems (an often-seen use of singletons is to provide unique IDs for use as database key values, when there's more than one instance of the singleton in operation this is no longer guaranteed).
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A gotcha of singletons is that if multiple threads can simultaneously access the singleton it must be thread safe.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's always important to remember that there is a big difference between the need for a Singleton (which enforces the limitation on the number of instances), and the need for just one instance.
Lots of situations only need one of a particular type of object. A company has one CEO, a dog has one tail. This does not nean that there is a need for a Singleton. It's perfectly reasonable to just create a single instance of a class, and use it as a regular object.
Use of the Singleton pattern is fraught with unexpected problems. It's hard to test, it's hard to maintain, etc. etc. Please search for "Singleton" in this forum and read some of the many threads on this issue. And always think at least twice before using one.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, it was a long time ago, and i was new to java, and i knew more then than i know now, but it seemed at the time that if i made my servlets implement the singelton model, i didnt have to worry about making them thread safe. obviously i need to reinvestigate this topic this summer after i get done relearning Java
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it seemed at the time that if i made my servlets implement the singelton model, i didnt have to worry about making them thread safe
Ah. Just a slight memory glitch. What you are probably thinking of is SingleThreadModel rather than Singleton.
 
reply
    Bookmark Topic Watch Topic
  • New Topic