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.

113 lines
3.6 KiB

package com.example.datacollect;
/**
* =============================================================================
* MVC 三层架构审计报告
* =============================================================================
*
* 一、审计目的
* ----------
* 检查当前项目的 MVC(Model/View/Controller)三层架构划分是否规范,
* 是否存在跨层直接调用等越权行为。
*
*
* 二、当前项目结构分析
* -------------------
*
* Model 层:
* - com.example.datacollect.model.Article
*
* View 层:
* - com.example.datacollect.view.ConsoleView
*
* Controller 层:
* - com.example.datacollect.controller.CrawlerController
*
* Command 层(可视为 Controller 的扩展):
* - HelpCommand, ListCommand, CrawlCommand, ExitCommand, HistoryCommand
*
*
* 三、发现的问题
* -------------
*
* 【问题1】Controller 直接持有 Model 列表引用 ⚠️
* 位置:CrawlerController.java 第17行
* 代码:private final List<Article> articles;
*
* 风险:
* - Controller 直接操作 Model 数据,违反单一职责
* - 多个 Command 都可以直接修改 articles 列表
* - 数据修改入口分散,难以追踪
*
* 修改建议:
* - 引入 Service 层(如 ArticleService)专门管理 Article 数据
* - Controller 只持有 Service 引用,Command 通过 Controller 间接访问
*
*
* 【问题2】Command 直接持有 View 引用 ⚠️
* 位置:所有 Command 实现类(如 CrawlCommand.java 第10行)
* 代码:private final ConsoleView view;
*
* 风险:
* - Command 越过 Controller 直接与 View 交互
* - Command 承担了部分 Controller 职责
*
* 修改建议:
* - Command 只负责解析命令和调用 Controller
* - View 引用统一由 Controller 管理
*
*
* 【问题3】List<Article> 共享引用风险 ⚠️
* 位置:CrawlerController.java 第17行
* 代码:private final List<Article> articles;
*
* 风险:
* - articles 作为可变共享状态被多个 Command 操作
* - 多线程环境下可能导致数据不一致
*
* 修改建议:
* - 使用不可变列表或返回副本
* - 添加线程安全保护(如 CopyOnWriteArrayList)
*
*
* 【问题4】View 直接遍历 Model 数据
* 位置:ConsoleView.java display 方法
*
* 风险:
* - View 层直接访问 Model 数据结构
*
* 修改建议:
* - Controller 将需要显示的数据封装为 DTO
*
*
* 四、架构修改建议
* ---------------
*
* 【推荐架构】
* View -> Controller -> Service -> DAO -> Model
*
* 【具体修改】
* 1. 新增 ArticleService.java 统一管理 Article 数据
* 2. 修改 CrawlerController 持有 ArticleService 而非 List<Article>
* 3. Command 只调用 Controller 方法,不直接操作列表
*
*
* 五、审计清单
* -----------
* □ Controller 是否直接持有 Model 数据? -> 是,需要引入 Service
* □ Command 是否直接操作 View? -> 是,需要通过 Controller
* □ 共享 List 是否线程安全? -> 否,需要保护
* □ View 是否直接访问 Model? -> 部分存在,需要 DTO
*
*
* 六、总结
* -------
* 当前项目存在以下越权行为:
* 1. Controller 直接持有 Model 数据
* 2. Command 直接操作 View 和 Model
* 3. 共享数据缺乏线程安全保护
*
* 建议逐步引入 Service 层,解耦数据管理和视图渲染。
*
* =============================================================================
*/