my dog learned polymorphism*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton pattern" Watch "Singleton pattern" New topic
Author

Singleton pattern

vijaya bacina
Ranch Hand

Joined: Aug 23, 2005
Posts: 155
Here is my situation
each user of our application will have a set of credentials(username/password) saved in our database to login to a partner application appB.
the credentials can be added for the first time user tries to login to the appB. thereafter user can update the credentials.
i am planning to make the credential class singleton since i need only one instance per user.
is the singleton pattern creates one instance for all users or one instance of credential per user?? please suggest
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
is the singleton pattern creates one instance for all users or one instance of credential per user?? please suggest


Does the Singleton pattern create one instance for all users or one instance per user?

The purpose of this pattern is to limit the number of instances of a class. A single instance is typically what is implemented. However, the design pattern can be used to limit the number of instances to three or four as well.

i am planning to make the credential class singleton since i need only one instance per user.


This is not a correct way to implement the Singleton design pattern. The purpose of the pattern is to limit the number of instances in an application, not per user. If you implement the credential class as a Singleton, each user will get the same credential as there will only be one instance.

Forget about the Singleton design pattern. It comes from non object-oriented roots and is typically a flaw whenever it is used, in my opinion.
[ September 02, 2008: Message edited by: James Clark ]
Kevin Smither
Ranch Hand

Joined: Oct 22, 2007
Posts: 30
The Credential class itself cannot be Singelton since what it represents is the user's credentials which varies on a per-user basis. The Authenticator class which authenticates credentials can be Singleton, perhaps it can accept a username and password and return a Credential class instance when it passes authentication.
 
 
subject: Singleton pattern