Pattern to replace switch statement..
I have a class which processes XML data . The class hierarchy is given below
public abstract class Processor{
public abstract void buildDataFromXml();
public void publishResponse(){
StringBuffer response = new StringBuffer();
response.append("Some Common Info -- ");
response.append(buildResponse()); // Sub Classes implement this method
}
public abstract void buildResponse();
}
public class MyProcessor extends Processor{
String xmlData;
String name;
String age
String companyId;
int caseType;
public void buildDataFromXml(){
// Set the name, age and companyId,caseType from the xmlData
}
public String buildResponse(){
switch(caseType ){
case 1: // Do something and break
case 2: // Do something else and break
}
return response;
}
}
I have XML Data coming with every request, which gets processed by the appropriate processor based on the request case.
One or more requests use the same processor, since the incoming data for these requests is common but the response sent out varies in each case.
Right now, I'm using a switch statement to determine the response, but i would like to change this and not depend on switch statement. So that i can add more cases(Case 3, Case 4..) without having to go back and add more cases to switch statement everytime.
I'm unable to figure out how to do this.. Please pitch in suggestions..

