I have written this piece of code that reads the content of an array of textfield.The content of the textfields represent one of several instructions.They are read,broken into two parts and they are looked up in a switch statement and if valid the appropriate action is executed.Also,i want to be able to add more commands.At the moment i have to add to the switch statement.This works but i was wondering if there is a more efficient way to design it.e.g to create a class for each class instruction and let the program determine (at runtime) which of these classes to execute.And to add a new one all i have to do is create a new class. How do i do this?If you can think of any method that is more efficient kindly let me know.
private class do implements ActionListener { public void actionPerformed(ActionEvent event) { for(km=start2;km<Size && Stop==false;km++) { String do1=read.substring(0,2); int do11=(int)(Integer.parseInt(do1)); int rem=read.length()-2+2; String do2=read.substring(2,rem); int do22=(int)(Integer.parseInt(do2)); Command(do11,do22); } } } public void Command(int code1,int code2) { switch(code1) { case 1: String copy11=MemoryContent[operand].getText(); calculator2.setText(" "+copy11); break; case 2: int copy31=(int)(Integer.parseInt(MemoryContent[operand].getText ())); int copy32=(int)(Integer.parseInt(calculator2.getText())); int sum=copy31+copy32; //calculator2 is a label calculator2.setText(sum+""); break; case 3: //do something break; .. case n://n indicates any other integer that may be added in future //do something break; default: error.setText("Invalid Command");
P.S:the commands are in the form of integers e.g. 0000.This is split into two sets of integerand passed to command as in: int do11=00 do22=00 command(do11,do22)