This question already has an answer here:
- How do I compare strings in Java? 23 answers
I have a use case like this -
Based on the parameter passed - i have to create an object corresponding to it but the underlying functionality remains same.
public void selectType ()
{
String type = "ABC";
publishType(type);
}
public void publishType(String type)
{
if (type =="ABC")
ABCtype publishObject = new ABCtype();
if (type =="XYZ")
XYZtype publishObject = new XYZtype();
publishObject.setfunctionality();
}
What is a better way to approach this? Which design pattern does it fall in?
Another doubt I have is - how to initialize publishObject? It gives an error like this.
Aucun commentaire:
Enregistrer un commentaire