Skip to content

Instantly share code, notes, and snippets.

@isterin
Created October 25, 2010 20:31
Show Gist options
  • Save isterin/645694 to your computer and use it in GitHub Desktop.
Save isterin/645694 to your computer and use it in GitHub Desktop.
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