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.
43 lines
1.3 KiB
43 lines
1.3 KiB
package com.example.datacollect.strategy;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class StrategyFactory {
|
|
private static final Logger logger = LoggerFactory.getLogger(StrategyFactory.class);
|
|
private final List<CrawlStrategy> strategies = new ArrayList<>();
|
|
|
|
public StrategyFactory() {
|
|
// 注册所有策略
|
|
strategies.add(new BlogStrategy());
|
|
strategies.add(new NewsStrategy());
|
|
logger.info("StrategyFactory initialized with {} strategies", strategies.size());
|
|
}
|
|
|
|
public CrawlStrategy getStrategy(String url) {
|
|
if (url == null || url.isBlank()) {
|
|
logger.warn("Null or blank URL provided to getStrategy");
|
|
return null;
|
|
}
|
|
|
|
for (CrawlStrategy strategy : strategies) {
|
|
if (strategy.supports(url)) {
|
|
logger.debug("Found strategy {} for URL: {}", strategy.getClass().getSimpleName(), url);
|
|
return strategy;
|
|
}
|
|
}
|
|
|
|
logger.warn("No strategy found for URL: {}", url);
|
|
return null;
|
|
}
|
|
|
|
public void registerStrategy(CrawlStrategy strategy) {
|
|
if (strategy != null) {
|
|
strategies.add(strategy);
|
|
logger.info("Registered new strategy: {}", strategy.getClass().getSimpleName());
|
|
}
|
|
}
|
|
}
|