From c0047053375bf7319e71145681e5eb91bc5278b3 Mon Sep 17 00:00:00 2001 From: ZhangYuhan <2179360179@qq.com> Date: Fri, 29 May 2026 23:09:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E7=A8=8B=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w9/AI架构审计 | 5 +++++ w9/Article.java | 29 ++++++++++++++++++++++++ w9/Command.java | 8 +++++++ w9/HistoryCommand.java | 50 ++++++++++++++++++++++++++++++++++++++++++ w9/Main2.java | 42 +++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 w9/AI架构审计 create mode 100644 w9/Article.java create mode 100644 w9/Command.java create mode 100644 w9/HistoryCommand.java create mode 100644 w9/Main2.java diff --git a/w9/AI架构审计 b/w9/AI架构审计 new file mode 100644 index 0000000..4bffa88 --- /dev/null +++ b/w9/AI架构审计 @@ -0,0 +1,5 @@ +审计结果:当前项目采用 CLI + MVC + Command 模式,分层清晰: +• Model 层(Article):仅负责数据存储,无业务逻辑 +• Command 层(Command接口及实现类):封装业务操作,不直接操作视图 +• View 层:统一处理输出,不包含业务逻辑 +各层职责明确,不存在跨层越权调用,符合 MVC 设计规范。 diff --git a/w9/Article.java b/w9/Article.java new file mode 100644 index 0000000..c902eb7 --- /dev/null +++ b/w9/Article.java @@ -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; } +} \ No newline at end of file diff --git a/w9/Command.java b/w9/Command.java new file mode 100644 index 0000000..fbbd57f --- /dev/null +++ b/w9/Command.java @@ -0,0 +1,8 @@ +import java.util.List; +public interface Command { + // 获取命令名称 + String getName(); + + // 执行命令:参数为命令参数、文章集合 + void execute(String[] args, List
articles); +} \ No newline at end of file diff --git a/w9/HistoryCommand.java b/w9/HistoryCommand.java new file mode 100644 index 0000000..b148da1 --- /dev/null +++ b/w9/HistoryCommand.java @@ -0,0 +1,50 @@ +import java.util.ArrayList; +import java.util.List; + +public class HistoryCommand implements Command { + // 集合:用来存储所有历史命令 + private final List commandHistory = new ArrayList<>(); + + /** + * 实现接口方法:返回命令标识 + */ + @Override + public String getName() { + return "history"; + } + + /** + * 执行查看历史命令功能 + */ + @Override + public void execute(String[] args, List
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("命令历史已清空!"); + } +} \ No newline at end of file diff --git a/w9/Main2.java b/w9/Main2.java new file mode 100644 index 0000000..718a133 --- /dev/null +++ b/w9/Main2.java @@ -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
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(); + } +} \ No newline at end of file