Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
Add Comment
Please, Sign In to add comment