Skip to content

Commit 708cd4c

Browse files
authored
Merge pull request #10 from ahucoder/dev
Optimize project structure and add README
2 parents 57b8c16 + e66e9a0 commit 708cd4c

File tree

86 files changed

+429
-207
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+429
-207
lines changed

README-EN.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Java Design Patterns
2+
3+
A comprehensive collection of Java design pattern implementations, including the 23 GoF patterns as well as additional patterns commonly used in modern development. Each pattern includes detailed explanations, UML diagrams, and real-world example code.
4+
5+
## Why Learn Design Patterns?
6+
7+
Design patterns offer reusable solutions to common software development problems. They provide:
8+
9+
* **Proven Solutions**: Time-tested approaches to specific challenges
10+
* **Code Reusability**: Reduce duplicated code
11+
* **Maintainability**: Make code easier to understand and modify
12+
* **Team Collaboration**: Provide a shared design vocabulary
13+
14+
## Pattern Categories
15+
16+
### Creational Patterns
17+
18+
| Pattern | Description | Example |
19+
| ----------------------------------------------- | ------------------------------------------------- | ------------------------------------ |
20+
| [Singleton](src/main/java/creational/singleton) | Ensure a class has only one instance | Database connection pool |
21+
| [Factory Method]() | Create objects without specifying the exact class | Cross-platform UI component creation |
22+
| [Abstract Factory]() | Create families of related objects | Cross-platform UI widget suites |
23+
| [Builder]() | Step-by-step construction of complex objects | SQL query builder |
24+
| [Prototype]() | Create objects by cloning | Game character duplication |
25+
26+
### Structural Patterns
27+
28+
| Pattern | Description | Example |
29+
| ----------------------------------------- | ---------------------------------------- | ------------------------------ |
30+
| [Adapter]() | Convert one interface into another | Logging system integration |
31+
| [Bridge]() | Separate abstraction from implementation | Graphics rendering engine |
32+
| [Composite]() | Handle tree-structured data | File system representation |
33+
| [Decorator]() | Dynamically add responsibilities | Java I/O streams |
34+
| [Facade](src/main/java/structural/facade) | Simplify complex system interfaces | Smart home control center |
35+
| [Flyweight]() | Efficiently share small objects | Text editor character handling |
36+
| [Proxy](src/main/java/structural/proxy) | Control access to objects | Lazy image loading |
37+
38+
### Behavioral Patterns
39+
40+
| Pattern | Description | Example |
41+
| --------------------------------------------------------- | ----------------------------------- | ---------------------------------- |
42+
| [Chain of Responsibility](src/main/java/behavioral/chain) | Request handling chain | Order approval workflow |
43+
| [Command](src/main/java/behavioral/command) | Encapsulate operations as objects | Transaction system, macro commands |
44+
| [Iterator]() | Traverse elements of a collection | Custom collection iteration |
45+
| [Mediator]() | Reduce dependencies between objects | Chat room system |
46+
| [Memento]() | Capture and restore object state | Document history |
47+
| [Observer](src/main/java/behavioral/observer) | Notify on state changes | Event-driven systems |
48+
| [State]() | Encapsulate state-specific behavior | Order state machine |
49+
| [Strategy](src/main/java/behavioral/strategy) | Encapsulate a family of algorithms | Payment method selection |
50+
| [Template Method]() | Define the skeleton of an algorithm | Build pipelines |
51+
| [Visitor]() | Separate algorithms from objects | Document export system |
52+
| [Interpreter]() | Define a language’s grammar | SQL parser |
53+
54+
### Concurrency Patterns
55+
56+
| Pattern | Description | Example |
57+
| ---------------------------------------------------------------------------- | ------------------------------------------------------------- | -------------------------------------------------- |
58+
| [Producer-Consumer](src/main/java/concurrency/producerconsumer) | Coordinate tasks between threads | Message queue system |
59+
| [Thread Pool]() | Reuse threads for better performance | Web server request handling |
60+
| [Read-Write Lock]() | Optimize concurrent access | Cache system implementation |
61+
| [Two-Phase Stop](src/main/java/concurrency/twophasestop) | Gracefully stop threads | Thread synchronization |
62+
| [Guarded Suspension](src/main/java/concurrency/guardedsuspension) | Wait for results between threads | Thread synchronization |
63+
| [Fixed Operating Sequence](src/main/java/concurrency/fixedoperatingsequence) | Control the execution order of threads (e.g., run t1 then t2) | Thread synchronization (common interview question) |
64+
| [Thread-Alternate Running](src/main/java/concurrency/threadalternaterunning) | Control round-robin thread execution (t1, t2, t3, t1…) | Thread synchronization (common interview question) |
65+
66+
## Project Structure
67+
68+
```
69+
src/main/java/
70+
├── behavioral/ # Behavioral patterns
71+
├── concurrency/ # Concurrency patterns
72+
├── creational/ # Creational patterns
73+
├── structural/ # Structural patterns
74+
└── utils/ # Common utility classes
75+
76+
src/test/java/ # Unit tests
77+
```
78+
79+
## Quick Start
80+
81+
### Prerequisites
82+
83+
* Java 21
84+
* Maven 3.6+
85+
86+
### Running Examples
87+
88+
1. Clone the repository:
89+
90+
```bash
91+
git clone git@github.com:ahucoder/JavaDesignPatterns.git
92+
cd JavaDesignPatterns
93+
```
94+
2. Build and run a particular example:
95+
96+
```bash
97+
mvn compile
98+
mvn exec:java -Dexec.mainClass="com.example.behavioral.command.Client"
99+
```
100+
101+
## Design Principles
102+
103+
This project strives to follow the SOLID design principles:
104+
105+
1. **Single Responsibility Principle (SRP)**: Each class has only one responsibility
106+
2. **Open/Closed Principle (OCP)**: Open for extension, closed for modification
107+
3. **Liskov Substitution Principle (LSP)**: Subtypes should be substitutable for their base types
108+
4. **Interface Segregation Principle (ISP)**: Clients should not be forced to depend on methods they do not use
109+
5. **Dependency Inversion Principle (DIP)**: Depend upon abstractions, not concretions
110+
111+
## Related Resources
112+
113+
* [Refactoring.Guru Design Patterns](https://refactoring.guru/design-patterns)
114+
* [Java Design Patterns](https://java-design-patterns.com/)
115+
* [Expert Bilibili Video Course](https://space.bilibili.com/7968519/upload/video)
116+
117+
---
118+
119+
**Happy Coding!** 🚀

README.md

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,105 @@
1-
# JavaDesignPatterns
2-
Design patterns in Java version
1+
# Java Design Patterns
2+
3+
一个全面的 Java 设计模式实现集合,包含 23 种 GoF 设计模式以及现代开发中常用的其他模式。每个模式都配有详细说明、UML 图和真实场景的示例代码。
4+
5+
## 为什么学习设计模式?
6+
7+
设计模式是软件开发中常见问题的可重用解决方案。它们提供了:
8+
- **经过验证的解决方案**:解决特定问题的成熟方法
9+
- **代码可重用性**:减少重复代码
10+
- **可维护性**:使代码更易于理解和修改
11+
- **团队协作**:提供共享的设计词汇表
12+
13+
## 模式分类
14+
15+
### 创建型模式
16+
| 模式 | 描述 | 示例 |
17+
|------------------------------------------------------|------|------|
18+
| [单例 (Singleton)](src/main/java/creational/singleton) | 确保一个类只有一个实例 | 数据库连接池 |
19+
| [工厂方法 (Factory Method)]() | 创建对象而不指定具体类 | UI 跨平台组件创建 |
20+
| [抽象工厂 (Abstract Factory)]() | 创建相关对象族 | 跨平台 UI 组件套件 |
21+
| [建造者 (Builder)]() | 分步构建复杂对象 | SQL 查询构建器 |
22+
| [原型 (Prototype)]() | 通过克隆创建对象 | 游戏角色复制 |
23+
24+
### 结构型模式
25+
| 模式 | 描述 | 示例 |
26+
|------|------|------|
27+
| [适配器 (Adapter)]() | 转换接口兼容性 | 日志系统集成 |
28+
| [桥接 (Bridge)]() | 分离抽象与实现 | 图形渲染引擎 |
29+
| [组合 (Composite)]() | 树形结构处理 | 文件系统表示 |
30+
| [装饰器 (Decorator)]() | 动态添加职责 | Java I/O 流 |
31+
| [外观 (Facade)](src/main/java/structural/facade) | 简化复杂系统接口 | 智能家居控制中心 |
32+
| [享元 (Flyweight)]() | 高效共享小对象 | 文本编辑器字符处理 |
33+
| [代理 (Proxy)](src/main/java/structural/proxy) | 控制对象访问 | 图片懒加载 |
34+
35+
### 行为型模式
36+
| 模式 | 描述 | 示例 |
37+
|------|------|------|
38+
| [责任链 (Chain of Responsibility)](src/main/java/behavioral/chain) | 请求处理链 | 订单审批流程 |
39+
| [命令 (Command)](src/main/java/behavioral/command) | 封装操作为对象 | 事务系统、宏命令 |
40+
| [迭代器 (Iterator)]() | 遍历集合元素 | 自定义集合遍历 |
41+
| [中介者 (Mediator)]() | 减少对象间依赖 | 聊天室系统 |
42+
| [备忘录 (Memento)]() | 捕获并恢复状态 | 文档历史记录 |
43+
| [观察者 (Observer)](src/main/java/behavioral/observer) | 对象状态变化通知 | 事件驱动系统 |
44+
| [状态 (State)]() | 封装状态行为 | 订单状态机 |
45+
| [策略 (Strategy)](src/main/java/behavioral/strategy) | 封装算法族 | 支付方式选择 |
46+
| [模板方法 (Template Method)]() | 定义算法骨架 | 构建流水线 |
47+
| [访问者 (Visitor)]() | 分离算法与对象 | 文档导出系统 |
48+
| [解释器 (Interpreter)]() | 定义语言语法 | SQL 解析器 |
49+
50+
### 并发模式
51+
| 模式 | 描述 | 示例 |
52+
|-----------------------------------------------------------------------------------------|----------------------------------------|-------------|
53+
| [生产者-消费者 (Producer-Consumer)](src/main/java/concurrency/producerconsumer) | 线程间任务协调 | 消息队列系统 |
54+
| [线程池 (Thread Pool)]() | 重用线程提高性能 | Web 服务器请求处理 |
55+
| [读写锁 (Read-Write Lock)]() | 优化并发访问 | 缓存系统实现 |
56+
| [两阶段终止 (Two-Phase-Stop)](src/main/java/concurrency/twophasestop) | 优雅的终止线程 | 线程间同步 |
57+
| [保护性暂停 (Guarded-Suspension)](src/main/java/concurrency/guardedsuspension) | 在一个线程中等待另一个线程的执行结果 | 线程间同步 |
58+
| [固定线程的运行顺序 (Fixed-Operating-Sequence)](src/main/java/concurrency/fixedoperatingsequence) | 用于控制线程的执行顺序(如先运行t1再运行t2),面试常问 | 线程间同步 |
59+
| [线程交替运行 (Thread-Alternate-Running)](src/main/java/concurrency/threadalternaterunning) | 用于控制线程的执行顺序(t1 t2 t3 t1 t2 t3...),面试常问 | 线程间同步 |
60+
61+
## 项目结构
62+
63+
```bash
64+
src/main/java/
65+
├── behavioral/ # 行为型模式
66+
├── concurrency/ # 并发模式
67+
├── creational/ # 创建型模式
68+
├── structural/ # 结构型模式
69+
└── utils/ # 公共工具类
70+
71+
src/test/java/ # 单元测试
72+
```
73+
74+
## 快速开始
75+
76+
### 前提条件
77+
- Java 21
78+
- Maven 3.6+
79+
80+
### 运行示例
81+
1. 克隆仓库:
82+
```bash
83+
git clone git@github.com:ahucoder/JavaDesignPatterns.git
84+
cd JavaDesignPatterns
85+
```
86+
87+
## 设计原则
88+
89+
本项目尽可能遵循 SOLID 设计原则:
90+
1. **单一职责原则 (SRP)**:每个类只有一个职责
91+
2. **开闭原则 (OCP)**:对扩展开放,对修改关闭
92+
3. **里氏替换原则 (LSP)**:子类可替换父类
93+
4. **接口隔离原则 (ISP)**:特定客户端专用接口
94+
5. **依赖倒置原则 (DIP)**:依赖抽象而非实现
95+
96+
## 相关资源
97+
98+
- [Refactoring.Guru 设计模式](https://refactoring.guru/design-patterns)
99+
- [Java 设计模式实战](https://java-design-patterns.com/)
100+
- [大佬的BiliBili视频课程](https://space.bilibili.com/7968519/upload/video)
101+
102+
---
103+
104+
**Happy Coding!** 🚀
105+
通过掌握设计模式,编写更优雅、可维护的 Java 代码。
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package chain;
1+
package behavioral.chain;
22

3-
import chain.dto.ApprovalRequestDto;
4-
import chain.enums.RequestType;
5-
import chain.handler.ApprovalHandler;
6-
import chain.processor.CEO;
7-
import chain.processor.DepartmentManager;
8-
import chain.processor.HRManager;
3+
import behavioral.chain.dto.ApprovalRequestDto;
4+
import behavioral.chain.enums.RequestType;
5+
import behavioral.chain.handler.ApprovalHandler;
6+
import behavioral.chain.processor.CEO;
7+
import behavioral.chain.processor.DepartmentManager;
8+
import behavioral.chain.processor.HRManager;
99

1010
public class App {
1111
public static void main(String[] args) {

src/main/java/chain/dto/ApprovalRequestDto.java renamed to src/main/java/behavioral/chain/dto/ApprovalRequestDto.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package chain.dto;
1+
package behavioral.chain.dto;
22

3-
import chain.enums.RequestType;
3+
import behavioral.chain.enums.RequestType;
44
import lombok.AllArgsConstructor;
55
import lombok.Getter;
66

src/main/java/chain/enums/RequestType.java renamed to src/main/java/behavioral/chain/enums/RequestType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chain.enums;
1+
package behavioral.chain.enums;
22

33
public enum RequestType {
44
LEAVE("Leave Application"),

src/main/java/chain/handler/ApprovalHandler.java renamed to src/main/java/behavioral/chain/handler/ApprovalHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package chain.handler;
1+
package behavioral.chain.handler;
22

3-
import chain.dto.ApprovalRequestDto;
4-
import chain.service.ApprovalRuleService;
3+
import behavioral.chain.dto.ApprovalRequestDto;
4+
import behavioral.chain.service.ApprovalRuleService;
55

66
import java.util.List;
77

src/main/java/chain/processor/CEO.java renamed to src/main/java/behavioral/chain/processor/CEO.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package chain.processor;
1+
package behavioral.chain.processor;
22

3-
import chain.handler.ApprovalHandler;
4-
import chain.service.impl.DefaultRuleService;
3+
import behavioral.chain.handler.ApprovalHandler;
4+
import behavioral.chain.service.impl.DefaultRuleService;
55

66
import java.util.List;
77

src/main/java/chain/processor/DepartmentManager.java renamed to src/main/java/behavioral/chain/processor/DepartmentManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package chain.processor;
1+
package behavioral.chain.processor;
22

3-
import chain.handler.ApprovalHandler;
4-
import chain.service.impl.LeaveDaysRuleService;
3+
import behavioral.chain.handler.ApprovalHandler;
4+
import behavioral.chain.service.impl.LeaveDaysRuleService;
55

66
import java.util.List;
77

src/main/java/chain/processor/HRManager.java renamed to src/main/java/behavioral/chain/processor/HRManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package chain.processor;
1+
package behavioral.chain.processor;
22

3-
import chain.handler.ApprovalHandler;
4-
import chain.service.impl.ExpenseAmountRuleService;
5-
import chain.service.impl.LeaveDaysRuleService;
3+
import behavioral.chain.handler.ApprovalHandler;
4+
import behavioral.chain.service.impl.ExpenseAmountRuleService;
5+
import behavioral.chain.service.impl.LeaveDaysRuleService;
66

77
import java.util.Arrays;
88

src/main/java/chain/service/ApprovalRuleService.java renamed to src/main/java/behavioral/chain/service/ApprovalRuleService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package chain.service;
1+
package behavioral.chain.service;
22

3-
import chain.dto.ApprovalRequestDto;
3+
import behavioral.chain.dto.ApprovalRequestDto;
44

55
public interface ApprovalRuleService {
66
boolean evaluate(ApprovalRequestDto request);

0 commit comments

Comments
 (0)