Chain of Command
public interface Command<T>{
boolean execute(T context);
}
public class FirstCommand implements Command<Map<String, Object>>{
public boolean execute(Map<String, Object> context){
//doing something in here
}
}
public class SecondCommand implements Command<Map<String, Object>>{
public boolean execute(Map<String, Object> context){
//doing something in here
}
}
public class Chain {
public List<Command> commands;
public Chain(Command... commands){
this.commands = Arrays.asList(commands);
}
public void start(Object context){
for(Command command : commands){
boolean shouldStop = command.execute(context);
if(shouldStop){
return;
}
}
}
}
Chain chain = new Chain(new FirstCommand(), new SecondCommand());
Map<String, Object> context = new HashMap<>();
context.put("some parameter", "some value");
chain.start(context);
boolean execute(T context);
}
public class FirstCommand implements Command<Map<String, Object>>{
public boolean execute(Map<String, Object> context){
//doing something in here
}
}
public class SecondCommand implements Command<Map<String, Object>>{
public boolean execute(Map<String, Object> context){
//doing something in here
}
}
public class Chain {
public List<Command> commands;
public Chain(Command... commands){
this.commands = Arrays.asList(commands);
}
public void start(Object context){
for(Command command : commands){
boolean shouldStop = command.execute(context);
if(shouldStop){
return;
}
}
}
}
Chain chain = new Chain(new FirstCommand(), new SecondCommand());
Map<String, Object> context = new HashMap<>();
context.put("some parameter", "some value");
chain.start(context);
Comments
Post a Comment