This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Singleton Design Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singleton Design Pattern" Watch "Singleton Design Pattern" New topic
Author

Singleton Design Pattern

Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Hi all, in singleton design pattern. We normally have a method call getInstance() which takes in no argument. I'm wondering is it ok to pass in a parameter, for eg. java.sql.Connection to retrieve record from the database?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
You can do anything you want, but the normal purpose of the getInstance() method in a singleton class is to return an instance of the class. You then use that instance to call other methods of the class to do whatever processing you want.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

However, although a bit unusual for Singletons, you could use it to return a single instance for each different parameter value. So each connection can have only one instance.

Keep in mind though that at some time you want to clean up or your singleton's cache could keep increasing until you get a memory error.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
The purpose for this is to have only a single instance for a particular class but using the connection provided by different calling class.

Alternatively is there any wrong with a class with static methods which takes in a java.sql.Connection as one of the arguments?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Design Pattern
 
Similar Threads
newInstance()
final Objects .....
how to make single instance of a class?
about default constructor
Email and Design pattern