1 changed files with 35 additions and 0 deletions
@ -0,0 +1,35 @@ |
|||
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); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue