• 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

best design for singleton

 
Ranch Hand
Posts: 53
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to display name from database using type ahead feature in my web application. I used Dojo filtering select to display names . those names have to be displayed in multiple jsp page. I have a Java class named getClientNames() that gets the list of names from the database . based on a condition list of names will vary. So i will pass a parameter to that java class ,based on the parameter it executes the query.

eg,
public class X{
public List<String> getNames(String param){
if param.equals("x"){
//query 1
}
else{
//query 2
}
return clisntList;
}

i have a Singleton class to call getNames method.

public class ClassicSingleton {
private static ClassicSingleton instance = null;
List<String> clientList;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}

public List<String> getName(){
X obj=new obj();
this.clientList=obj.getNames(param);
return this.clientList;
}
}

i need to pass a parameter to this singleton method getName to get proper result. How can i achieve it..
 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hemamalini,

I'm not sure I see the difficulty why not declare the method with the parameter you need? something like:



Notice I made the clientList a local variable instead of a field in the class. As a field there can be only one such list and I assume this may be called from different places with different parameters.

Best,
Joe

ps. Also notice the use of the code tags makes reading Java code easier.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic