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.
35 lines
959 B
35 lines
959 B
package com.example.datacollect.strategy;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class StrategyFactory {
|
|
private final List<CrawlStrategy> strategies = new ArrayList<>();
|
|
|
|
public StrategyFactory() {
|
|
strategies.add(new HnuNewsStrategy());
|
|
strategies.add(new BlogStrategy());
|
|
strategies.add(new NewsStrategy());
|
|
}
|
|
|
|
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) {
|
|
return bestStrategy;
|
|
}
|
|
return new DefaultStrategy();
|
|
}
|
|
|
|
public void register(CrawlStrategy strategy) {
|
|
strategies.add(strategy);
|
|
}
|
|
}
|
|
|