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.

33 lines
993 B

package command;
import strategy.CrawlStrategy;
import model.Article;
import controller.CrawlerController;
public class CrawlCommand implements Command {
private CrawlStrategy strategy;
private CrawlerController controller;
public CrawlCommand(CrawlStrategy strategy, CrawlerController controller) {
this.strategy = strategy;
this.controller = controller;
}
@Override
public void execute() {
try {
controller.getView().showMessage("正在爬取: " + strategy.getName());
Article article = strategy.crawl();
controller.addArticle(article);
controller.getView().showArticle(article);
controller.getView().showMessage("爬取成功!");
} catch (Exception e) {
controller.getView().showError("爬取失败: " + e.getMessage());
}
}
@Override
public String getDescription() {
return "爬取 " + strategy.getName();
}
}