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.

61 lines
1.9 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实现
这是一个符合SOLID原则的简单工厂模式Java应用。
## 项目结构
```
src/
├── main/
│ └── java/
│ ├── Product.java # 抽象产品类
│ ├── ConcreteProductA.java # 具体产品类A
│ ├── ConcreteProductB.java # 具体产品类B
│ ├── Factory.java # 工厂类
│ └── Main.java # 主类,用于演示
└── test/
└── java/
├── ProductTest.java # 抽象产品测试类
├── ConcreteProductATest.java # 具体产品A测试类
├── ConcreteProductBTest.java # 具体产品B测试类
└── FactoryTest.java # 工厂类测试类
```
## 功能说明
1. **抽象产品类 (Product)**:定义产品的公共接口和抽象方法
2. **具体产品类 (ConcreteProductA, ConcreteProductB)**:实现产品的具体功能
3. **工厂类 (Factory)**:负责创建不同类型的产品实例
4. **主类 (Main)**:演示工厂模式的使用方法
## 运行方法
### 运行主程序
```bash
cd src
javac -encoding UTF-8 main/java/*.java
java main.java.Main
```
### 运行测试
使用JUnit框架运行测试
```bash
# 需要将JUnit库添加到classpath
java -cp .:junit.jar:hamcrest-core.jar org.junit.runner.JUnitCore main.java.FactoryTest main.java.ProductTest main.java.ConcreteProductATest main.java.ConcreteProductBTest
```
## SOLID原则应用
- **单一职责原则 (SRP)**:每个类都有明确的职责
- **开闭原则 (OCP)**:系统对扩展开放,对修改关闭
- **里氏替换原则 (LSP)**:子类可以替换父类使用
- **依赖倒置原则 (DIP)**:依赖于抽象而非具体实现
- **接口隔离原则 (ISP)**:产品接口简洁,只包含必要的方法
## 注意事项
- 本项目使用Java标准库实现无需额外依赖
- 测试类使用JUnit框架运行测试需要JUnit库