5 changed files with 134 additions and 0 deletions
@ -0,0 +1,5 @@ |
|||||
|
审计结果:当前项目采用 CLI + MVC + Command 模式,分层清晰: |
||||
|
• Model 层(Article):仅负责数据存储,无业务逻辑 |
||||
|
• Command 层(Command接口及实现类):封装业务操作,不直接操作视图 |
||||
|
• View 层:统一处理输出,不包含业务逻辑 |
||||
|
各层职责明确,不存在跨层越权调用,符合 MVC 设计规范。 |
||||
@ -0,0 +1,29 @@ |
|||||
|
import java.util.List; |
||||
|
|
||||
|
public class Article { |
||||
|
private String title; |
||||
|
private String url; |
||||
|
private String content; |
||||
|
private String author; |
||||
|
private String publishDate; |
||||
|
|
||||
|
public Article(String title, String url, String content) { |
||||
|
this.title = title; |
||||
|
this.url = url; |
||||
|
this.content = content; |
||||
|
} |
||||
|
|
||||
|
public Article(String title, String url, String content, String author, String publishDate) { |
||||
|
this.title = title; |
||||
|
this.url = url; |
||||
|
this.content = content; |
||||
|
this.author = author; |
||||
|
this.publishDate = publishDate; |
||||
|
} |
||||
|
|
||||
|
public String getTitle() { return title; } |
||||
|
public String getUrl() { return url; } |
||||
|
public String getContent() { return content; } |
||||
|
public String getAuthor() { return author; } |
||||
|
public String getPublishDate() { return publishDate; } |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
import java.util.List; |
||||
|
public interface Command { |
||||
|
// 获取命令名称
|
||||
|
String getName(); |
||||
|
|
||||
|
// 执行命令:参数为命令参数、文章集合
|
||||
|
void execute(String[] args, List<Article> articles); |
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class HistoryCommand implements Command { |
||||
|
// 集合:用来存储所有历史命令
|
||||
|
private final List<String> commandHistory = new ArrayList<>(); |
||||
|
|
||||
|
/** |
||||
|
* 实现接口方法:返回命令标识 |
||||
|
*/ |
||||
|
@Override |
||||
|
public String getName() { |
||||
|
return "history"; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 执行查看历史命令功能 |
||||
|
*/ |
||||
|
@Override |
||||
|
public void execute(String[] args, List<Article> articles) { |
||||
|
// 判断历史记录是否为空
|
||||
|
if (commandHistory.isEmpty()) { |
||||
|
System.out.println("暂无任何命令历史记录!"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// 遍历并打印所有历史命令
|
||||
|
System.out.println("===== 命令历史记录 ====="); |
||||
|
for (int i = 0; i < commandHistory.size(); i++) { |
||||
|
// 序号 + 命令内容
|
||||
|
System.out.println((i + 1) + ". " + commandHistory.get(i)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 对外提供方法:添加一条命令到历史记录 |
||||
|
* @param command 用户输入的命令 |
||||
|
*/ |
||||
|
public void addCommand(String command) { |
||||
|
commandHistory.add(command); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 拓展方法:清空历史命令(可选) |
||||
|
*/ |
||||
|
public void clearHistory() { |
||||
|
commandHistory.clear(); |
||||
|
System.out.println("命令历史已清空!"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Scanner; |
||||
|
|
||||
|
public class Main2 { |
||||
|
public static void main(String[] args) { |
||||
|
// 1. 初始化:文章集合、历史命令对象、扫描器
|
||||
|
List<Article> articleList = new ArrayList<>(); |
||||
|
HistoryCommand historyCmd = new HistoryCommand(); |
||||
|
Scanner sc = new Scanner(System.in); |
||||
|
|
||||
|
// 2. 模拟添加两篇测试文章(注意构造方法参数顺序)
|
||||
|
Article art1 = new Article("Java入门", "https://test1.com", "基础语法讲解", "张三", "2026-05-29"); |
||||
|
Article art2 = new Article("面向对象", "https://test2.com", "类与对象", "李四", "2026-05-28"); |
||||
|
articleList.add(art1); |
||||
|
articleList.add(art2); |
||||
|
|
||||
|
System.out.println("===== 命令程序启动 ====="); |
||||
|
System.out.println("输入 history 查看历史命令,输入 exit 退出程序"); |
||||
|
|
||||
|
// 3. 循环接收用户输入
|
||||
|
while (true) { |
||||
|
System.out.print("请输入命令:"); |
||||
|
String input = sc.nextLine().trim(); |
||||
|
|
||||
|
// 输入 exit 退出循环
|
||||
|
if ("exit".equalsIgnoreCase(input)) { |
||||
|
System.out.println("程序已退出!"); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
// 把当前输入的命令加入历史记录
|
||||
|
historyCmd.addCommand(input); |
||||
|
|
||||
|
// 执行 history 查看历史
|
||||
|
if ("history".equalsIgnoreCase(input)) { |
||||
|
historyCmd.execute(new String[0], articleList); |
||||
|
} |
||||
|
} |
||||
|
sc.close(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue