This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes where can use single object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "where can use single object" Watch "where can use single object" New topic
Author

where can use single object

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
I know the sigleton design patter but i dont know where should i use it.
what are the applications of singleton design pattern?
as ia m developing online examination using jsp, servlets can i use this singleton deg pattern in my application???




A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Generally I'm trying to avoid using singletons in building applications. Unfortunately it isn't always easy, for example configuration is something required for most of applications and fits in singleton pattern perfectly.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Singleton is most useful when you need to control access to a particular resource. For example, in my test engine one class controls all loading of XML test script documents and tracks the usage of these DOMs. Obviously, having more than one instance of this class would be a big waste of memory and make debugging harder. Therefore the constructor is private and a static "factory" method handles initial creation of the single instance.
Bill


Java Resources at www.wbrogden.com
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
ok thanx but can i sue this concept for user login
so that the only one user can login with his PIN and no other can login by using the same PIN?
can i do this ?
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Originally posted by saikrishna cinux:
ok thanx but can i sue this concept for user login
so that the only one user can login with his PIN and no other can login by using the same PIN?
can i do this ?


No. Singletone is not for that purpose.


My blood is tested +ve for Java.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Chetan is correct.

You might have a UserManager class using the Singleton pattern since you only need one UserManager for the entire application. UserManager could track all logins and be able to tell if a particular PIN was in use.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where can use single object
 
Similar Threads
Singleton Pattern
singleton class
When would it be imperative to use Singleton
Just One Instance
Static Methods in EJB