Created
October 25, 2010 20:31
-
-
Save isterin/645694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class ChainDemo { | |
static interface Command { | |
void execute(Map<String, Object> ctx); | |
} | |
static class Chain { | |
private List<Command> commands = new ArrayList<Command>(); | |
public void addCommand(Command cmd) { | |
commands.add(cmd); | |
} | |
public void execute() { | |
Map<String, Object> ctx = new HashMap<String, Object>(); | |
for (Command cmd : commands) { | |
cmd.execute(ctx); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
Chain chain = new Chain(); | |
chain.addCommand(new Command() { | |
public void execute(Map<String, Object> ctx) { | |
// Perform some calc here | |
ctx.put("result1", 1234); | |
} | |
}); | |
chain.addCommand(new Command() { | |
public void execute(Map<String, Object> ctx) { | |
int res = (Integer) ctx.get("result1"); | |
ctx.put("result2", res * 2); | |
} | |
}); | |
chain.addCommand(new Command() { | |
public void execute(Map<String, Object> ctx) { | |
System.err.println("Results: "); | |
for (String key : ctx.keySet()) { | |
System.err.println("\t" + key + ": " + ctx.get(key)); | |
} | |
} | |
}); | |
chain.execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment