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.
factory/README.md

463 lines
7.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 本项目包含以下核心内容:
# \- \*\*简单工厂模式\*\*的Java实现代码
# \- \*\*工厂方法模式\*\*的UML类图设计
# \- PlantUML使用指南和教程
# \- 完整的代码示例和测试用例
#
# \## 项目结构
#
# ```
# ├── .trae/ # TRAE AI配置目录
# │ ├── java\_templates.json # Java代码模板配置
# │ ├── rules/ # 规则配置
# │ │ └── project\_rules.md
# │ └── trae\_config.json # 主配置文件
# ├── src/ # 源代码目录
# │ └── main/
# │ └── java/ # Java实现代码
# │ ├── Product.java # 产品接口
# │ ├── ConcreteProductA.java # 具体产品A
# │ ├── ConcreteProductB.java # 具体产品B
# │ ├── Factory.java # 简单工厂类
# │ └── Main.java # 测试主类
# ├── factory\_method\_uml.puml # 工厂方法模式UML类图
# ├── plantuml\_guide.md # PlantUML使用指南
# └── README.md # 项目说明文档
# ```
#
# \## 1. 简单工厂模式实现
#
# \### 核心组件
#
# \#### 1.1 产品接口 (Product.java)
# ```java
# /\*\*
#  \* 产品接口
#  \* 定义所有产品共有的操作方法
#  \*/
# public interface Product {
#   /\*\*
#   \* 产品的操作方法
#   \*/
#   void operation();
# }
# ```
#
# \#### 1.2 具体产品类 (ConcreteProductA.java, ConcreteProductB.java)
# ```java
# /\*\*
#  \* 具体产品A类
#  \* 实现产品接口的具体产品
#  \*/
# public class ConcreteProductA implements Product {
#   @Override
#   public void operation() {
#   System.out.println("ConcreteProductA operation executed");
#   }
# }
# ```
#
# ```java
# /\*\*
#  \* 具体产品B类
#  \* 实现产品接口的具体产品
#  \*/
# public class ConcreteProductB implements Product {
#   @Override
#   public void operation() {
#   System.out.println("ConcreteProductB operation executed");
#   }
# }
# ```
#
# \#### 1.3 简单工厂类 (Factory.java)
# ```java
# /\*\*
#  \* 简单工厂类
#  \* 根据参数创建不同的产品实例
#  \*/
# public class Factory {
#   /\*\*
#   \* 创建产品的静态方法
#   \* @param type 产品类型
#   \* @return 产品实例
#   \* @throws IllegalArgumentException 当类型不支持或为空时抛出异常
#   \*/
#   public static Product createProduct(String type) {
#   if (type == null || type.isEmpty()) {
#   throw new IllegalArgumentException("Product type cannot be null or empty");
#   }
#  
#   if (type.equalsIgnoreCase("A")) {
#   return new ConcreteProductA();
#   } else if (type.equalsIgnoreCase("B")) {
#   return new ConcreteProductB();
#   } else {
#   throw new IllegalArgumentException("Unsupported product type: " + type);
#   }
#   }
# }
# ```
#
# \#### 1.4 测试主类 (Main.java)
# ```java
# /\*\*
#  \* 测试主类
#  \* 演示简单工厂模式的使用
#  \*/
# public class Main {
#   /\*\*
#   \* 主方法
#   \* @param args 命令行参数
#   \*/
#   public static void main(String\[] args) {
#   try {
#   // 测试创建产品A
#   Product productA = Factory.createProduct("A");
#   productA.operation();
#  
#   // 测试创建产品B
#   Product productB = Factory.createProduct("B");
#   productB.operation();
#  
#   // 测试不支持的产品类型
#   System.out.println("\\n测试不支持的产品类型:");
#   try {
#   Product productC = Factory.createProduct("C");
#   } catch (IllegalArgumentException e) {
#   System.out.println("Expected exception: " + e.getMessage());
#   }
#  
#   // 测试空参数
#   System.out.println("\\n测试空参数:");
#   try {
#   Product productNull = Factory.createProduct("");
#   } catch (IllegalArgumentException e) {
#   System.out.println("Expected exception: " + e.getMessage());
#   }
#  
#   } catch (Exception e) {
#   System.err.println("Unexpected error: " + e.getMessage());
#   e.printStackTrace();
#   }
#   }
# }
# ```
#
# \## 2. 运行代码
#
# \### 编译和运行步骤
#
# 1\. 确保已安装Java开发环境JDK
# 2\. 打开命令行工具,导航到项目根目录
# 3\. 编译Java文件
#   ```bash
#   javac src\\main\\java\\\*.java
#   ```
# 4\. 运行Main类
#   ```bash
#   java -cp src\\main\\java Main
#   ```
#
# \### 预期输出
#
# ```
# ConcreteProductA operation executed
# ConcreteProductB operation executed
#
# 测试不支持的产品类型:
# Expected exception: Unsupported product type: C
#
# 测试空参数:
# Expected exception: Product type cannot be null or empty
# ```
#
# \## 3. 工厂方法模式UML类图
#
# 项目中包含了完整的工厂方法模式UML类图定义文件`factory\_method\_uml.puml`
#
# \### 类图核心特点
#
# \- \*\*抽象产品接口\*\* (Product):定义产品的通用操作
# \- \*\*具体产品类\*\* (ConcreteProductA, ConcreteProductB):实现抽象产品接口
# \- \*\*抽象创建者\*\* (Creator):定义工厂方法接口
# \- \*\*具体创建者\*\* (ConcreteCreatorA, ConcreteCreatorB):实现工厂方法,创建具体产品
# \- \*\*客户端\*\* (Main):只依赖抽象接口,不依赖具体实现类
#
# \### 运行UML类图
#
# 1\. 打开 \[PlantText](https://www.planttext.com/) 在线工具
# 2\. 复制 `factory\_method\_uml.puml` 文件的内容
# 3\. 粘贴到左侧编辑区
# 4\. 点击 "Refresh" 按钮查看生成的类图
#
# \## 4. PlantUML使用指南
#
# 项目中包含详细的PlantUML使用指南`plantuml\_guide.md`
#
# \### 主要内容
#
# \- 在线工具访问方法
# \- 基础语法教程
# \- 类图关系表示方法
# \- 样式设置技巧
# \- IDE集成方法
#
# \## 5. 设计模式说明
#
# \### 5.1 简单工厂模式
#
# \*\*核心思想\*\*:由一个工厂类根据参数决定创建哪一种产品类的实例。
#
# \*\*优点\*\*
# \- 实现了对象创建和使用的分离
# \- 客户端不需要知道如何创建和组装产品
#
# \*\*缺点\*\*
# \- 工厂类职责过重
# \- 新增产品需要修改工厂类,违背开闭原则
#
# \### 5.2 工厂方法模式
#
# \*\*核心思想\*\*:定义一个创建产品对象的接口,但由子类决定实例化的类。工厂方法让类的实例化推迟到子类中进行。
#
# \*\*优点\*\*
# \- 符合开闭原则,新增产品只需添加相应的创建者和产品类
# \- 客户端只依赖抽象接口,不依赖具体实现
# \- 更灵活的扩展性
#
# \*\*适用场景\*\*
# \- 客户端不需要知道它所需要的对象的类
# \- 系统需要在不修改现有代码的情况下扩展