Browse Source

工程架构

main
ZhangYuhan 3 weeks ago
parent
commit
c004705337
  1. 5
      w9/AI架构审计
  2. 29
      w9/Article.java
  3. 8
      w9/Command.java
  4. 50
      w9/HistoryCommand.java
  5. 42
      w9/Main2.java

5
w9/AI架构审计

@ -0,0 +1,5 @@
审计结果:当前项目采用 CLI + MVC + Command 模式,分层清晰:
• Model 层(Article):仅负责数据存储,无业务逻辑
• Command 层(Command接口及实现类):封装业务操作,不直接操作视图
• View 层:统一处理输出,不包含业务逻辑
各层职责明确,不存在跨层越权调用,符合 MVC 设计规范。

29
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; }
}

8
w9/Command.java

@ -0,0 +1,8 @@
import java.util.List;
public interface Command {
// 获取命令名称
String getName();
// 执行命令:参数为命令参数、文章集合
void execute(String[] args, List<Article> articles);
}

50
w9/HistoryCommand.java

@ -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("命令历史已清空!");
}
}

42
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<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…
Cancel
Save