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.

42 lines
1.4 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 HnuNewsStrategy());
strategies.add(new BlogStrategy());
strategies.add(new NewsStrategy());
logger.info("Initialized with {} strategies", strategies.size());
}
public CrawlStrategy getStrategy(String url) {
CrawlStrategy bestStrategy = null;
int highestPriority = -1;
for (CrawlStrategy s : strategies) {
if (s.supports(url) && s.getPriority() > highestPriority) {
bestStrategy = s;
highestPriority = s.getPriority();
}
}
if (bestStrategy != null) {
logger.debug("Found strategy {} for URL: {}", bestStrategy.getClass().getSimpleName(), url);
return bestStrategy;
}
logger.debug("No specific strategy found for URL: {}, using default", url);
return new DefaultStrategy();
}
public void register(CrawlStrategy strategy) {
strategies.add(strategy);
logger.info("Registered strategy: {}", strategy.getClass().getSimpleName());
}
}