• 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

Singaton pattern and factory pattern

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir,

Kindly explain How to use Singaton pattern and factory pattern using Java,JSP,Servlet,artechure?

Thanking You

Bhavesh
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A singleton patteren says "Only one instance per class" i.e. you will always get the same instance of the class, no other instance will be created. One way of doing this is:



here we simply made the constroctor as private so that no one can dirctly initalize our class, now we will always call the getInstance() method to create the instance of our class, where we will check if the class is intantiated earlier or not and return the instance of the class.

I hop this will be helpfull.


 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use google
 
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 bhaveshs shah:

Kindly explain How to use Singaton pattern and factory pattern using Java,JSP,Servlet,artechure?



Can you please clarify your doubts? What do you know about those patterns, and what do you want to use them for?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To nitpick a bit ... Sachin's example is not thread safe - the JVM could switch threads between the if clause and the new Singleton clause. Since you mentioned JSP and Servlet which are multi-threaded environments, that could be important.

If so, there are a couple very easy ways to fix it up. One is to make the getInstance() method synchronized. Then only one thread can be in there at a time. The other is to create the instance with the declaration

That reduces the getInstance() method to a single line "return".

To the original question ... The getInstance() method is a creational method ... a way client objects can get an instance of something other than the new keyword. It's not quite any of the factory related patterns in the Gang of Four book, but it's on the way there.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic