Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions README-EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Java Design Patterns

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.

## Why Learn Design Patterns?

Design patterns offer reusable solutions to common software development problems. They provide:

* **Proven Solutions**: Time-tested approaches to specific challenges
* **Code Reusability**: Reduce duplicated code
* **Maintainability**: Make code easier to understand and modify
* **Team Collaboration**: Provide a shared design vocabulary

## Pattern Categories

### Creational Patterns

| Pattern | Description | Example |
| ----------------------------------------------- | ------------------------------------------------- | ------------------------------------ |
| [Singleton](src/main/java/creational/singleton) | Ensure a class has only one instance | Database connection pool |
| [Factory Method]() | Create objects without specifying the exact class | Cross-platform UI component creation |
| [Abstract Factory]() | Create families of related objects | Cross-platform UI widget suites |
| [Builder]() | Step-by-step construction of complex objects | SQL query builder |
| [Prototype]() | Create objects by cloning | Game character duplication |

### Structural Patterns

| Pattern | Description | Example |
| ----------------------------------------- | ---------------------------------------- | ------------------------------ |
| [Adapter]() | Convert one interface into another | Logging system integration |
| [Bridge]() | Separate abstraction from implementation | Graphics rendering engine |
| [Composite]() | Handle tree-structured data | File system representation |
| [Decorator]() | Dynamically add responsibilities | Java I/O streams |
| [Facade](src/main/java/structural/facade) | Simplify complex system interfaces | Smart home control center |
| [Flyweight]() | Efficiently share small objects | Text editor character handling |
| [Proxy](src/main/java/structural/proxy) | Control access to objects | Lazy image loading |

### Behavioral Patterns

| Pattern | Description | Example |
| --------------------------------------------------------- | ----------------------------------- | ---------------------------------- |
| [Chain of Responsibility](src/main/java/behavioral/chain) | Request handling chain | Order approval workflow |
| [Command](src/main/java/behavioral/command) | Encapsulate operations as objects | Transaction system, macro commands |
| [Iterator]() | Traverse elements of a collection | Custom collection iteration |
| [Mediator]() | Reduce dependencies between objects | Chat room system |
| [Memento]() | Capture and restore object state | Document history |
| [Observer](src/main/java/behavioral/observer) | Notify on state changes | Event-driven systems |
| [State]() | Encapsulate state-specific behavior | Order state machine |
| [Strategy](src/main/java/behavioral/strategy) | Encapsulate a family of algorithms | Payment method selection |
| [Template Method]() | Define the skeleton of an algorithm | Build pipelines |
| [Visitor]() | Separate algorithms from objects | Document export system |
| [Interpreter]() | Define a language’s grammar | SQL parser |

### Concurrency Patterns

| Pattern | Description | Example |
| ---------------------------------------------------------------------------- | ------------------------------------------------------------- | -------------------------------------------------- |
| [Producer-Consumer](src/main/java/concurrency/producerconsumer) | Coordinate tasks between threads | Message queue system |
| [Thread Pool]() | Reuse threads for better performance | Web server request handling |
| [Read-Write Lock]() | Optimize concurrent access | Cache system implementation |
| [Two-Phase Stop](src/main/java/concurrency/twophasestop) | Gracefully stop threads | Thread synchronization |
| [Guarded Suspension](src/main/java/concurrency/guardedsuspension) | Wait for results between threads | Thread synchronization |
| [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) |
| [Thread-Alternate Running](src/main/java/concurrency/threadalternaterunning) | Control round-robin thread execution (t1, t2, t3, t1…) | Thread synchronization (common interview question) |

## Project Structure

```
src/main/java/
├── behavioral/ # Behavioral patterns
├── concurrency/ # Concurrency patterns
├── creational/ # Creational patterns
├── structural/ # Structural patterns
└── utils/ # Common utility classes

src/test/java/ # Unit tests
```

## Quick Start

### Prerequisites

* Java 21
* Maven 3.6+

### Running Examples

1. Clone the repository:

```bash
git clone git@github.com:ahucoder/JavaDesignPatterns.git
cd JavaDesignPatterns
```
2. Build and run a particular example:

```bash
mvn compile
mvn exec:java -Dexec.mainClass="com.example.behavioral.command.Client"
```

## Design Principles

This project strives to follow the SOLID design principles:

1. **Single Responsibility Principle (SRP)**: Each class has only one responsibility
2. **Open/Closed Principle (OCP)**: Open for extension, closed for modification
3. **Liskov Substitution Principle (LSP)**: Subtypes should be substitutable for their base types
4. **Interface Segregation Principle (ISP)**: Clients should not be forced to depend on methods they do not use
5. **Dependency Inversion Principle (DIP)**: Depend upon abstractions, not concretions

## Related Resources

* [Refactoring.Guru Design Patterns](https://refactoring.guru/design-patterns)
* [Java Design Patterns](https://java-design-patterns.com/)
* [Expert Bilibili Video Course](https://space.bilibili.com/7968519/upload/video)

---

**Happy Coding!** 🚀
107 changes: 105 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,105 @@
# JavaDesignPatterns
Design patterns in Java version
# Java Design Patterns

一个全面的 Java 设计模式实现集合,包含 23 种 GoF 设计模式以及现代开发中常用的其他模式。每个模式都配有详细说明、UML 图和真实场景的示例代码。

## 为什么学习设计模式?

设计模式是软件开发中常见问题的可重用解决方案。它们提供了:
- **经过验证的解决方案**:解决特定问题的成熟方法
- **代码可重用性**:减少重复代码
- **可维护性**:使代码更易于理解和修改
- **团队协作**:提供共享的设计词汇表

## 模式分类

### 创建型模式
| 模式 | 描述 | 示例 |
|------------------------------------------------------|------|------|
| [单例 (Singleton)](src/main/java/creational/singleton) | 确保一个类只有一个实例 | 数据库连接池 |
| [工厂方法 (Factory Method)]() | 创建对象而不指定具体类 | UI 跨平台组件创建 |
| [抽象工厂 (Abstract Factory)]() | 创建相关对象族 | 跨平台 UI 组件套件 |
| [建造者 (Builder)]() | 分步构建复杂对象 | SQL 查询构建器 |
| [原型 (Prototype)]() | 通过克隆创建对象 | 游戏角色复制 |

### 结构型模式
| 模式 | 描述 | 示例 |
|------|------|------|
| [适配器 (Adapter)]() | 转换接口兼容性 | 日志系统集成 |
| [桥接 (Bridge)]() | 分离抽象与实现 | 图形渲染引擎 |
| [组合 (Composite)]() | 树形结构处理 | 文件系统表示 |
| [装饰器 (Decorator)]() | 动态添加职责 | Java I/O 流 |
| [外观 (Facade)](src/main/java/structural/facade) | 简化复杂系统接口 | 智能家居控制中心 |
| [享元 (Flyweight)]() | 高效共享小对象 | 文本编辑器字符处理 |
| [代理 (Proxy)](src/main/java/structural/proxy) | 控制对象访问 | 图片懒加载 |

### 行为型模式
| 模式 | 描述 | 示例 |
|------|------|------|
| [责任链 (Chain of Responsibility)](src/main/java/behavioral/chain) | 请求处理链 | 订单审批流程 |
| [命令 (Command)](src/main/java/behavioral/command) | 封装操作为对象 | 事务系统、宏命令 |
| [迭代器 (Iterator)]() | 遍历集合元素 | 自定义集合遍历 |
| [中介者 (Mediator)]() | 减少对象间依赖 | 聊天室系统 |
| [备忘录 (Memento)]() | 捕获并恢复状态 | 文档历史记录 |
| [观察者 (Observer)](src/main/java/behavioral/observer) | 对象状态变化通知 | 事件驱动系统 |
| [状态 (State)]() | 封装状态行为 | 订单状态机 |
| [策略 (Strategy)](src/main/java/behavioral/strategy) | 封装算法族 | 支付方式选择 |
| [模板方法 (Template Method)]() | 定义算法骨架 | 构建流水线 |
| [访问者 (Visitor)]() | 分离算法与对象 | 文档导出系统 |
| [解释器 (Interpreter)]() | 定义语言语法 | SQL 解析器 |

### 并发模式
| 模式 | 描述 | 示例 |
|-----------------------------------------------------------------------------------------|----------------------------------------|-------------|
| [生产者-消费者 (Producer-Consumer)](src/main/java/concurrency/producerconsumer) | 线程间任务协调 | 消息队列系统 |
| [线程池 (Thread Pool)]() | 重用线程提高性能 | Web 服务器请求处理 |
| [读写锁 (Read-Write Lock)]() | 优化并发访问 | 缓存系统实现 |
| [两阶段终止 (Two-Phase-Stop)](src/main/java/concurrency/twophasestop) | 优雅的终止线程 | 线程间同步 |
| [保护性暂停 (Guarded-Suspension)](src/main/java/concurrency/guardedsuspension) | 在一个线程中等待另一个线程的执行结果 | 线程间同步 |
| [固定线程的运行顺序 (Fixed-Operating-Sequence)](src/main/java/concurrency/fixedoperatingsequence) | 用于控制线程的执行顺序(如先运行t1再运行t2),面试常问 | 线程间同步 |
| [线程交替运行 (Thread-Alternate-Running)](src/main/java/concurrency/threadalternaterunning) | 用于控制线程的执行顺序(t1 t2 t3 t1 t2 t3...),面试常问 | 线程间同步 |

## 项目结构

```bash
src/main/java/
├── behavioral/ # 行为型模式
├── concurrency/ # 并发模式
├── creational/ # 创建型模式
├── structural/ # 结构型模式
└── utils/ # 公共工具类

src/test/java/ # 单元测试
```

## 快速开始

### 前提条件
- Java 21
- Maven 3.6+

### 运行示例
1. 克隆仓库:
```bash
git clone git@github.com:ahucoder/JavaDesignPatterns.git
cd JavaDesignPatterns
```

## 设计原则

本项目尽可能遵循 SOLID 设计原则:
1. **单一职责原则 (SRP)**:每个类只有一个职责
2. **开闭原则 (OCP)**:对扩展开放,对修改关闭
3. **里氏替换原则 (LSP)**:子类可替换父类
4. **接口隔离原则 (ISP)**:特定客户端专用接口
5. **依赖倒置原则 (DIP)**:依赖抽象而非实现

## 相关资源

- [Refactoring.Guru 设计模式](https://refactoring.guru/design-patterns)
- [Java 设计模式实战](https://java-design-patterns.com/)
- [大佬的BiliBili视频课程](https://space.bilibili.com/7968519/upload/video)

---

**Happy Coding!** 🚀
通过掌握设计模式,编写更优雅、可维护的 Java 代码。
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package chain;
package behavioral.chain;

import chain.dto.ApprovalRequestDto;
import chain.enums.RequestType;
import chain.handler.ApprovalHandler;
import chain.processor.CEO;
import chain.processor.DepartmentManager;
import chain.processor.HRManager;
import behavioral.chain.dto.ApprovalRequestDto;
import behavioral.chain.enums.RequestType;
import behavioral.chain.handler.ApprovalHandler;
import behavioral.chain.processor.CEO;
import behavioral.chain.processor.DepartmentManager;
import behavioral.chain.processor.HRManager;

public class App {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package chain.dto;
package behavioral.chain.dto;

import chain.enums.RequestType;
import behavioral.chain.enums.RequestType;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package chain.enums;
package behavioral.chain.enums;

public enum RequestType {
LEAVE("Leave Application"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chain.handler;
package behavioral.chain.handler;

import chain.dto.ApprovalRequestDto;
import chain.service.ApprovalRuleService;
import behavioral.chain.dto.ApprovalRequestDto;
import behavioral.chain.service.ApprovalRuleService;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chain.processor;
package behavioral.chain.processor;

import chain.handler.ApprovalHandler;
import chain.service.impl.DefaultRuleService;
import behavioral.chain.handler.ApprovalHandler;
import behavioral.chain.service.impl.DefaultRuleService;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chain.processor;
package behavioral.chain.processor;

import chain.handler.ApprovalHandler;
import chain.service.impl.LeaveDaysRuleService;
import behavioral.chain.handler.ApprovalHandler;
import behavioral.chain.service.impl.LeaveDaysRuleService;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package chain.processor;
package behavioral.chain.processor;

import chain.handler.ApprovalHandler;
import chain.service.impl.ExpenseAmountRuleService;
import chain.service.impl.LeaveDaysRuleService;
import behavioral.chain.handler.ApprovalHandler;
import behavioral.chain.service.impl.ExpenseAmountRuleService;
import behavioral.chain.service.impl.LeaveDaysRuleService;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package chain.service;
package behavioral.chain.service;

import chain.dto.ApprovalRequestDto;
import behavioral.chain.dto.ApprovalRequestDto;

public interface ApprovalRuleService {
boolean evaluate(ApprovalRequestDto request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chain.service.impl;
package behavioral.chain.service.impl;

import chain.dto.ApprovalRequestDto;
import chain.service.ApprovalRuleService;
import behavioral.chain.dto.ApprovalRequestDto;
import behavioral.chain.service.ApprovalRuleService;

public class DefaultRuleService implements ApprovalRuleService {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package chain.service.impl;
package behavioral.chain.service.impl;

import chain.dto.ApprovalRequestDto;
import chain.enums.RequestType;
import chain.service.ApprovalRuleService;
import behavioral.chain.dto.ApprovalRequestDto;
import behavioral.chain.enums.RequestType;
import behavioral.chain.service.ApprovalRuleService;

public class ExpenseAmountRuleService implements ApprovalRuleService {
private final double maxAmount;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package chain.service.impl;
package behavioral.chain.service.impl;

import chain.dto.ApprovalRequestDto;
import chain.enums.RequestType;
import chain.service.ApprovalRuleService;
import behavioral.chain.dto.ApprovalRequestDto;
import behavioral.chain.enums.RequestType;
import behavioral.chain.service.ApprovalRuleService;

public class LeaveDaysRuleService implements ApprovalRuleService {
private final int maxDays;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package chain.service.impl;
package behavioral.chain.service.impl;

import chain.dto.ApprovalRequestDto;
import chain.enums.RequestType;
import chain.service.ApprovalRuleService;
import behavioral.chain.dto.ApprovalRequestDto;
import behavioral.chain.enums.RequestType;
import behavioral.chain.service.ApprovalRuleService;

public class PurchaseTypeRuleService implements ApprovalRuleService {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package command;

import command.service.command.Command;
import command.service.command.impl.DelayedCommand;
import command.service.command.impl.LightOffCommand;
import command.service.command.impl.LightOnCommand;
import command.service.command.impl.MacroCommand;
import command.service.device.SmartDeviceController;
import command.service.device.impl.LightDevice;
package behavioral.command;

import behavioral.command.service.command.Command;
import behavioral.command.service.command.impl.DelayedCommand;
import behavioral.command.service.command.impl.LightOffCommand;
import behavioral.command.service.command.impl.LightOnCommand;
import behavioral.command.service.command.impl.MacroCommand;
import behavioral.command.service.device.SmartDeviceController;
import behavioral.command.service.device.impl.LightDevice;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package command.listener;
package behavioral.command.listener;

import command.service.command.Command;
import behavioral.command.service.command.Command;

public interface CommandListener {
void onCommandAction(Command command, boolean executed);
Expand Down
Loading