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 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 executeAll() throws CrawlerException { List allResults = new ArrayList<>(); for (Command command : commands) { view.showHeader("执行命令: " + command.getName()); try { List 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(); } }