9 changed files with 136 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,69 @@ |
|||||
|
import java.util.List; |
||||
|
|
||||
|
interface ParseStrategy { |
||||
|
boolean supports(String url); |
||||
|
int analyze(String url); |
||||
|
} |
||||
|
|
||||
|
class BlogStrategy implements ParseStrategy { |
||||
|
@Override |
||||
|
public boolean supports(String url) { |
||||
|
return url.contains("blog"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int analyze(String url) { |
||||
|
return 1000; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class NewsStrategy implements ParseStrategy { |
||||
|
@Override |
||||
|
public boolean supports(String url) { |
||||
|
return url.contains("news"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int analyze(String url) { |
||||
|
return 2000; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class AnalyzeCommand { |
||||
|
private final List<ParseStrategy> strategies; |
||||
|
|
||||
|
public AnalyzeCommand(List<ParseStrategy> strategies) { |
||||
|
this.strategies = strategies; |
||||
|
} |
||||
|
|
||||
|
public void execute(String url) { |
||||
|
if (url == null || url.isBlank()) { |
||||
|
System.out.println("错误:URL不能为空"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
ParseStrategy matchedStrategy = null; |
||||
|
for (ParseStrategy strategy : strategies) { |
||||
|
if (strategy.supports(url)) { |
||||
|
matchedStrategy = strategy; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (matchedStrategy == null) { |
||||
|
System.out.println("错误:无支持该URL的解析策略"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
int stats = matchedStrategy.analyze(url); |
||||
|
System.out.println("=== 分析结果 ==="); |
||||
|
System.out.println("URL: " + url); |
||||
|
System.out.println("统计信息: " + stats); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
List<ParseStrategy> strategies = List.of(new BlogStrategy(), new NewsStrategy()); |
||||
|
AnalyzeCommand cmd = new AnalyzeCommand(strategies); |
||||
|
cmd.execute("http://example.com/blog/123"); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
Binary file not shown.
@ -0,0 +1,56 @@ |
|||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
public class ArticleRepository { |
||||
|
private final List<Article> articles = new ArrayList<>(); |
||||
|
|
||||
|
public void add(Article article) { |
||||
|
if (article != null) { |
||||
|
articles.add(article); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void addAll(Collection<Article> newArticles) { |
||||
|
if (newArticles == null || newArticles.isEmpty()) { |
||||
|
return; |
||||
|
} |
||||
|
for (Article article : newArticles) { |
||||
|
add(article); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public List<Article> getAll() { |
||||
|
return new ArrayList<>(articles); |
||||
|
} |
||||
|
|
||||
|
public void clear() { |
||||
|
articles.clear(); |
||||
|
} |
||||
|
|
||||
|
static class Article { |
||||
|
private String title; |
||||
|
public Article(String title) { this.title = title; } |
||||
|
public String getTitle() { return title; } |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
ArticleRepository repo = new ArticleRepository(); |
||||
|
System.out.println("=== 测试 ArticleRepository ==="); |
||||
|
System.out.println("初始大小:" + repo.getAll().size()); |
||||
|
|
||||
|
repo.add(new Article("文章1")); |
||||
|
System.out.println("添加一个后大小:" + repo.getAll().size()); |
||||
|
|
||||
|
ArrayList<Article> list = new ArrayList<>(); |
||||
|
list.add(new Article("文章2")); |
||||
|
list.add(null); |
||||
|
list.add(new Article("文章3")); |
||||
|
repo.addAll(list); |
||||
|
System.out.println("添加多个后大小:" + repo.getAll().size()); |
||||
|
|
||||
|
repo.addAll(null); |
||||
|
System.out.println("过滤null后大小:" + repo.getAll().size()); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,11 @@ |
|||||
|
两个策略都 supports 同一URL时怎么办? |
||||
|
|
||||
|
当多个策略都匹配同一URL时,会出现策略冲突,导致解析逻辑不确定。解决方案如下: |
||||
|
|
||||
|
1. 策略优先级排序:为每个策略设置明确的优先级(如数字权重),匹配时按优先级从高到低查找,优先使用优先级高的策略。 |
||||
|
|
||||
|
2. 匹配规则互斥设计:调整各策略的URL匹配规则,避免出现交叉匹配的情况,例如使用更精确的正则表达式,确保一个URL只被一个策略匹配。 |
||||
|
|
||||
|
3. 冲突处理机制:在代码中检测到多个匹配策略时,抛出异常并提示冲突,或让用户手动选择策略。 |
||||
|
|
||||
|
4. 策略注册顺序约定:若未设置优先级,按策略注册的先后顺序匹配,先注册的策略优先生效,但这种方式可读性较差,不推荐使用。 |
||||
Loading…
Reference in new issue