Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton pattern

 
vijaya bacina
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic