conditional statement patterns

Are there any patterns for use of 'if statements' or 'switch statements' ?

I want to know if there is a better way of writing this for example.....

privatevoid actionOptions(String cmd){

if (cmd.equals("Pause")){

simulationStatusHolder = SIMULATION_PAUSE;

stopGo.setLabel("Go");

notify();

}elseif (cmd.equals("Go")){

simulationStatusHolder = SIMULATION_GO;

stopGo.setLabel("Pause");

notify();

}elseif (cmd.equals("Start From Scratch")){

simulationStatusHolder = SIMULATION_RESTART;

stopGo.setLabel("Pause");

notify();

}elseif (cmd.equals("End the World")){

owner.close();

}

}

[1438 byte] By [Jenskia] at [2007-10-2 7:32:17]
# 1

> Are there any patterns for use of 'if statements' or

> 'switch statements' ?

Yes, the command pattern.

public class DynamicSwitch {

public abstract class AbstractCase { public void execute() { System.err.println( this.getClass().getName() ) ; } } ;

public class CaseAA extends AbstractCase { } ;

public class CaseBB extends AbstractCase { } ;

public class CaseCC extends AbstractCase { } ;

java.util.HashMap cases = new java.util.HashMap() ;

/** Creates a new instance of DynamicSwitch */

public DynamicSwitch() {

// load these names from a properties file

// and use a class factory to construct them.

cases.put( "AA" ,new CaseAA() ) ;

cases.put( "BB" ,new CaseBB() ) ;

cases.put( "CC" ,new CaseCC() ) ;

}

public void execute(String key) {

AbstractCase abstractCase = (AbstractCase) cases.get( key ) ;

abstractCase.execute() ;

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

DynamicSwitch instance = new DynamicSwitch() ;

instance.execute( "AA" ) ;

instance.execute( "BB" ) ;

instance.execute( "CC" ) ;

}

}

MartinS.a at 2007-7-16 21:12:05 > top of Java-index,Other Topics,Patterns & OO Design...