From 8bce0f0c9d439c728a524e152c64829cfd9ec015 Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Thu, 7 May 2026 19:59:43 +0800 Subject: [PATCH] =?UTF-8?q?w10-=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w10/StrategyFactory.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 w10/StrategyFactory.java diff --git a/w10/StrategyFactory.java b/w10/StrategyFactory.java new file mode 100644 index 0000000..4de96f9 --- /dev/null +++ b/w10/StrategyFactory.java @@ -0,0 +1,35 @@ +package com.example.datacollect.strategy; + +import java.util.ArrayList; +import java.util.List; + +public class StrategyFactory { + private final List 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); + } +}