I have a database table that contains a column named type. For every row in my database column I have to create an object depending on the type. At the moment I use if else statements for that:
if (type.equals("object1")){
Object1 object1 = new Object1();
}
else if (type.equals("object2")){
Object2 object2 = new Object2();
}
Somewhat nicer would be to use an enum, as the number of types is limited but is there a possibility to let the creation of an object depend on the value of the String?
I’m open to suggestions that might solve my problem in another way than I am trying to.
You can use
If you have an int and two Strings as arguments you need.
Another possibility is to use factory methods
but the most flexible approach may be to use a switch
What would be more elegant is using closures in Java 8.
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html