The following flowchart:
may be described by the following java code:
if (A == 1 && B ==1){
actionA();
}
if (B == 3 || (B == 1 && A == 2)){
actionB();
actionC();
}
if (B == 2){
actionC();
}
Is there a better way to translate a flowchart in java code? I am looking for some sort of general pattern to do this. My question arises from the fact that adding a single condition to the flowchart results in very significant changes to the code.
You could encapsulate ActionB and ActionC, while ActionC is being called after ActionB in ActionBC and make a new method for each cell in your flow chart. In general you should get something like:
void B1(){
if(B==1)
A1();
if (B==2)
actionC();
...
}
void A1(){ if(A1==2) actionBC(); }
private void actionBC(){...}
And so on... In that way, expanding your flowchart won't explode you code.