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.
69 lines
1.7 KiB
69 lines
1.7 KiB
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");
|
|
}
|
|
}
|
|
|