I'm having an interface "Parent", and multiple classes (say Abc.java, Def.java, and Xyz.java) implementing the interface. Now I want to do something like this:
Parent factoryMethod(String condition){
Parent p = null;
if(condition.equals("Abc"))
p = new Abc();
else if(condition.equals("Def"))
p = new Def();
else if(condition.equals("Xyz"))
p = new Xyz();
return p;
}
Basically I'm passing the name of the class to be instantiated as the parameter to the method. What is the best way to be doing this? Should I use reflection for this? Its not just 3 classes, there might be a lot more. So I don't want to write if/else.
Thanks.