You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

48 lines
1.2 KiB

package command;
import model.CrawlResult;
import exception.CrawlerException;
import view.CrawlerView;
import java.util.ArrayList;
import java.util.List;
public class CommandInvoker {
private final List<Command> commands;
private final CrawlerView view;
public CommandInvoker(CrawlerView view) {
this.commands = new ArrayList<>();
this.view = view;
}
public void addCommand(Command command) {
commands.add(command);
}
public List<CrawlResult> executeAll() throws CrawlerException {
List<CrawlResult> allResults = new ArrayList<>();
for (Command command : commands) {
view.showHeader("执行命令: " + command.getName());
try {
List<CrawlResult> results = command.execute();
allResults.addAll(results);
} catch (CrawlerException e) {
view.showError("命令 " + command.getName() + " 执行失败: " + e.getMessage());
throw e;
}
view.showLine();
}
return allResults;
}
public void clearCommands() {
commands.clear();
}
public int getCommandCount() {
return commands.size();
}
}