EDIT:
Based on Ilgorbek Kuchkarovs answer (original from StackOverflow) (similar problem here: Dispatching SOAP function calls) I have implemented the solution as this:
Interface ServiceMethod.java:
interface ServiceMethod {
public void execute();
}
WorkingClass.java:Should
public class WorkingClass {
static OrderObject orderObject;
public static Map<String, ServiceMethod> serviceMethodMap = new HashMap<>();
public static Map<String, String> allCarAttributes = new HashMap<>();
public static void main(String[] args) {
orderObject = new OrderObject();
allCarAttributes.put("CAR_DOORS", "4");
allCarAttributes.put("CAR_WHEELS", "10");
serviceMethodMap.put("CAR_DOORS", new ServiceMethod() {
@Override
public void execute(String input) {
orderObject.setCarDoors(input);
}
});
serviceMethodMap.put("CAR_WHEELS", new ServiceMethod() {
@Override
public void execute(String input) {
orderObject.setCarWheels(input);
}
});
for (Map.Entry<String, String> attribute : allCarAttributes.entrySet()) {
serviceMethodMap.get(attribute.getKey()).execute(attribute.getValue());
}
System.out.println(orderObject.toString());
}
}
This works as intended, however I dont see the advantageget rid of using the interface-approach to usingit at all? The switch-case.
I still have to write 80 times:
serviceMethodMap.put("CAR_DOORS / WHEELS / COLORS / ...", new ServiceMethod() {
@Override
public void execute(String input) {
orderObject.setCarDoors / -Wheels / -Colors / ...(input);
}
});
This does not seem any shorter works fine as it is, it is just very long and even complexer?
Could anyone explain the advantages of using this approach?ugly.