Quote:
Originally Posted by notmattlythgoe
Code:
public class DecisionCommand extends Command {
public DecisionCommand (IProvidesDecision decision, Command trueCommand, Command falseCommand) {}
public void initialize() {
if (decision.getValue()) {
trueCommand.initialize();
} else {
falseCommand.initialize());
}
}
public void execute() {
if (decision.getValue()) {
trueCommand.execute();
} else {
falseCommand.execute());
}
etc...
}
|
That looks like it would work. I was pretty sure it would be possible to do something but I thought it might get really ugly really fast. That's a pretty slick way to do it.
Here's something you could do if you wanted to be avoid duplicating the end of command conditions in the subsequent if:
Code:
class Continuable extends Command{
//I don't remember whether I've set this class up is valid Java...
//this would return self unless you wnated to run a different command.
abstract public Continuable next();
public bool isFinished(){
return next()==null;
}
}
class Continues extends Command{
Continuable current;
Continues(Continuable cmd){
current=cmd;
}
void initialize(){
if(current!=null) current.initialize();
}
void execute(){
if(current!=null){
current.execute();
}
}
bool isFinished(){
if(current==null) return 1;
current=current.next();
return current==null;
}
}