1
0
Fork 0

添加中文 README 文件,包含 23 种设计模式的实现及相关说明,增强文档可读性

This commit is contained in:
IvisTang 2026-01-07 20:33:05 +08:00
parent a319494128
commit b6f0ca5aef
2 changed files with 482 additions and 0 deletions

240
README.md
View File

@ -0,0 +1,240 @@
# Design Patterns Implementation
[中文版 README](README_CN.md) | This repository contains implementations of the 23 classic Gang of Four (GoF) design patterns in three different programming languages: Go, TypeScript, and Python. Each pattern is implemented with clear examples and explanations to help developers understand and apply these fundamental design principles.
## Table of Contents
- [Introduction](#introduction)
- [Design Patterns Overview](#design-patterns-overview)
- [Project Structure](#project-structure)
- [Getting Started](#getting-started)
- [Pattern Categories](#pattern-categories)
- [Contributing](#contributing)
- [License](#license)
## Introduction
Design patterns are typical solutions to commonly occurring problems in software design. They represent best practices that seasoned object-oriented software developers have used in their work for decades. This repository provides practical implementations of these patterns in three popular programming languages to demonstrate their usage and benefits.
## Design Patterns Overview
This repository includes implementations of all 23 Gang of Four design patterns:
### Creational Patterns
- **Abstract Factory**: Provides an interface for creating families of related objects
- **Builder**: Separates the construction of a complex object from its representation
- **Factory Method**: Defines an interface for creating an object, but lets subclasses alter the type of objects that will be created
- **Prototype**: Specifies the kinds of objects to create using a prototypical instance
- **Singleton**: Ensures a class has only one instance and provides a global point of access to it
### Structural Patterns
- **Adapter**: Converts the interface of a class into another interface clients expect
- **Bridge**: Decouples an abstraction from its implementation so that the two can vary independently
- **Composite**: Composes objects into tree structures to represent part-whole hierarchies
- **Decorator**: Attaches additional responsibilities to an object dynamically
- **Facade**: Provides a unified interface to a set of interfaces in a subsystem
- **Flyweight**: Uses sharing to support large numbers of fine-grained objects efficiently
- **Proxy**: Provides a surrogate or placeholder for another object to control access to it
### Behavioral Patterns
- **Chain of Responsibility**: Passes a request along a chain of handlers
- **Command**: Encapsulates a request as an object
- **Interpreter**: Defines a grammatical representation for a language
- **Iterator**: Provides a way to access the elements of an aggregate object sequentially
- **Mediator**: Defines how a set of objects interact with each other
- **Memento**: Captures and externalizes an object's internal state without violating encapsulation
- **Observer**: Defines a one-to-many dependency between objects so that when one object changes state, all dependents are notified automatically
- **State**: Allows an object to alter its behavior when its internal state changes
- **Strategy**: Defines a family of algorithms, encapsulates each one, and makes them interchangeable
- **Template Method**: Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses
- **Visitor**: Represents an operation to be performed on the elements of an object structure
## Project Structure
```
design-patterns/
├── go/ # Go implementations
│ ├── abstract_factory/
│ ├── adapter/
│ ├── bridge/
│ ├── builder/
│ ├── chain_of_responsibility/
│ ├── command/
│ ├── composite/
│ ├── decorator/
│ ├── facade/
│ ├── factory_method/
│ ├── flyweight/
│ ├── interpreter/
│ ├── iterator/
│ ├── mediator/
│ ├── memento/
│ ├── observer/
│ ├── prototype/
│ ├── proxy/
│ ├── singleton/
│ ├── state/
│ ├── strategy/
│ ├── template_method/
│ ├── visitor/
│ ├── go.mod
│ ├── Makefile
│ └── README.md
├── python/ # Python implementations
│ ├── abstract_factory/
│ ├── adapter/
│ ├── bridge/
│ ├── builder/
│ ├── chain_of_responsibility/
│ ├── command/
│ ├── composite/
│ ├── decorator/
│ ├── facade/
│ ├── factory_method/
│ ├── flyweight/
│ ├── interpreter/
│ ├── iterator/
│ ├── mediator/
│ ├── memento/
│ ├── observer/
│ ├── prototype/
│ ├── proxy/
│ ├── singleton/
│ ├── state/
│ ├── strategy/
│ ├── template_method/
│ ├── visitor/
│ ├── Makefile
│ ├── pyproject.toml
│ └── README.md
├── ts/ # TypeScript implementations
│ ├── src/
│ │ ├── abstract_factory/
│ │ ├── adapter/
│ │ ├── bridge/
│ │ ├── builder/
│ │ ├── chain_of_responsibility/
│ │ ├── command/
│ │ ├── composite/
│ │ ├── decorator/
│ │ ├── facade/
│ │ ├── factory_method/
│ │ ├── flyweight/
│ │ ├── interpreter/
│ │ ├── iterator/
│ │ ├── mediator/
│ │ ├── memento/
│ │ ├── observer/
│ │ ├── prototype/
│ │ ├── proxy/
│ │ ├── singleton/
│ │ ├── state/
│ │ ├── strategy/
│ │ ├── template_method/
│ │ ├── visitor/
│ ├── package.json
│ ├── tsconfig.json
│ ├── pnpm-lock.yaml
│ ├── Makefile
│ └── README.md
├── .gitignore
├── README.md # This file
└── README_CN.md # Chinese version
```
## Getting Started
### Go Implementation
1. Navigate to the Go directory:
```bash
cd go
```
2. Each pattern directory contains:
- Implementation files
- Example usage
- Tests (if applicable)
3. Run examples using:
```bash
go run <pattern-name>/main.go
```
### Python Implementation
1. Navigate to the Python directory:
```bash
cd python
```
2. Each pattern directory contains:
- Implementation modules
- Example usage
- Tests (if applicable)
3. Run examples using:
```bash
python -m <pattern-name>.main
```
### TypeScript Implementation
1. Navigate to the TypeScript directory:
```bash
cd ts
```
2. Install dependencies:
```bash
pnpm install
```
3. Each pattern directory contains:
- Implementation files
- Example usage
- Tests (if applicable)
4. Run examples using:
```bash
pnpm run <pattern-name>
```
## Pattern Categories
### Creational Patterns
Creational patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented.
### Structural Patterns
Structural patterns deal with the composition of classes or objects. They help ensure that when one part of a system changes, the entire system doesn't need to change.
### Behavioral Patterns
Behavioral patterns characterize the ways in which classes or objects interact and distribute responsibility. They focus on communication between objects.
## Contributing
Contributions are welcome! Here are some ways you can contribute:
1. Report bugs and issues
2. Suggest new features or improvements
3. Add implementations in other languages
4. Improve documentation
5. Add more comprehensive examples
To contribute:
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License.
## Acknowledgments
- The Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) for their seminal work on design patterns
- The open-source community for providing inspiration and examples
- All contributors who help maintain and improve this repository

242
README_CN.md Normal file
View File

@ -0,0 +1,242 @@
# 设计模式实现
本仓库包含了 23 种经典 GoFGang of Four设计模式的三种编程语言实现Go、TypeScript 和 Python。每种模式都提供了清晰的示例和解释帮助开发者理解和应用这些基本的设计原则。
## 目录
- [介绍](#介绍)
- [设计模式概览](#设计模式概览)
- [项目结构](#项目结构)
- [快速开始](#快速开始)
- [模式分类](#模式分类)
- [贡献](#贡献)
- [许可证](#许可证)
## 介绍
设计模式是软件设计中常见问题的典型解决方案。它们代表了经验丰富的面向对象软件开发人员在工作中使用的最佳实践。本仓库提供了这三种流行编程语言中的实用实现,以演示它们的用法和优势。
## 设计模式概览
本仓库包含所有 23 种 Gang of Four 设计模式的实现:
### 创建型模式
- **抽象工厂模式Abstract Factory**: 为创建相关对象族提供接口
- **建造者模式Builder**: 将复杂对象的构建与其表示分离
- **工厂方法模式Factory Method**: 定义创建对象的接口,但让子类决定将要创建的对象类型
- **原型模式Prototype**: 使用原型实例指定要创建的对象类型
- **单例模式Singleton**: 确保一个类只有一个实例,并提供全局访问点
### 结构型模式
- **适配器模式Adapter**: 将一个类的接口转换为客户期望的另一个接口
- **桥接模式Bridge**: 将抽象与其实现分离,使两者可以独立变化
- **组合模式Composite**: 将对象组合成树形结构以表示部分-整体层次结构
- **装饰器模式Decorator**: 动态地为对象附加额外职责
- **外观模式Facade**: 为子系统中的一组接口提供统一接口
- **享元模式Flyweight**: 使用共享来高效支持大量细粒度对象
- **代理模式Proxy**: 为另一个对象提供代理或占位符以控制对它的访问
### 行为型模式
- **责任链模式Chain of Responsibility**: 将请求沿处理程序链传递
- **命令模式Command**: 将请求封装为对象
- **解释器模式Interpreter**: 为语言定义语法表示
- **迭代器模式Iterator**: 提供一种方法来顺序访问聚合对象的元素
- **中介者模式Mediator**: 定义一组对象如何相互交互
- **备忘录模式Memento**: 在不违反封装的情况下捕获并外部化对象的内部状态
- **观察者模式Observer**: 定义对象间的一对多依赖关系,当一个对象状态改变时,所有依赖者都会自动收到通知
- **状态模式State**: 允许对象在其内部状态改变时改变其行为
- **策略模式Strategy**: 定义算法族,封装每个算法,并使它们可互换
- **模板方法模式Template Method**: 在操作中定义算法骨架,将一些步骤推迟到子类
- **访问者模式Visitor**: 表示对对象结构中元素执行的操作
## 项目结构
``
design-patterns/
├── go/ # Go 实现
│ ├── abstract_factory/
│ ├── adapter/
│ ├── bridge/
│ ├── builder/
│ ├── chain_of_responsibility/
│ ├── command/
│ ├── composite/
│ ├── decorator/
│ ├── facade/
│ ├── factory_method/
│ ├── flyweight/
│ ├── interpreter/
│ ├── iterator/
│ ├── mediator/
│ ├── memento/
│ ├── observer/
│ ├── prototype/
│ ├── proxy/
│ ├── singleton/
│ ├── state/
│ ├── strategy/
│ ├── template_method/
│ ├── visitor/
│ ├── go.mod
│ ├── Makefile
│ └── README.md
├── python/ # Python 实现
│ ├── abstract_factory/
│ ├── adapter/
│ ├── bridge/
│ ├── builder/
│ ├── chain_of_responsibility/
│ ├── command/
│ ├── composite/
│ ├── decorator/
│ ├── facade/
│ ├── factory_method/
│ ├── flyweight/
│ ├── interpreter/
│ ├── iterator/
│ ├── mediator/
│ ├── memento/
│ ├── observer/
│ ├── prototype/
│ ├── proxy/
│ ├── singleton/
│ ├── state/
│ ├── strategy/
│ ├── template_method/
│ ├── visitor/
│ ├── Makefile
│ ├── pyproject.toml
│ └── README.md
├── ts/ # TypeScript 实现
│ ├── src/
│ │ ├── abstract_factory/
│ │ ├── adapter/
│ │ ├── bridge/
│ │ ├── builder/
│ │ ├── chain_of_responsibility/
│ │ ├── command/
│ │ ├── composite/
│ │ ├── decorator/
│ │ ├── facade/
│ │ ├── factory_method/
│ │ ├── flyweight/
│ │ ├── interpreter/
│ │ ├── iterator/
│ │ ├── mediator/
│ │ ├── memento/
│ │ ├── observer/
│ │ ├── prototype/
│ │ ├── proxy/
│ │ ├── singleton/
│ │ ├── state/
│ │ ├── strategy/
│ │ ├── template_method/
│ │ ├── visitor/
│ ├── package.json
│ ├── tsconfig.json
│ ├── pnpm-lock.yaml
│ ├── Makefile
│ └── README.md
├── .gitignore
├── README.md # 英文版 README
└── README_CN.md # 中文版 README
``
## 快速开始
### Go 实现
1. 进入 Go 目录:
```bash
cd go
```
2. 每个模式目录包含:
- 实现文件
- 示例用法
- 测试(如适用)
3. 运行示例:
```bash
go run <pattern-name>/main.go
```
### Python 实现
1. 进入 Python 目录:
```bash
cd python
```
2. 每个模式目录包含:
- 实现模块
- 示例用法
- 测试(如适用)
3. 运行示例:
```bash
python -m <pattern-name>.main
```
### TypeScript 实现
1. 进入 TypeScript 目录:
```bash
cd ts
```
2. 安装依赖:
```bash
pnpm install
```
3. 每个模式目录包含:
- 实现文件
- 示例用法
- 测试(如适用)
4. 运行示例:
```bash
pnpm run <pattern-name>
```
## 模式分类
### 创建型模式
创建型模式抽象了实例化过程。它们帮助系统独立于如何创建、组合和表示其对象。
### 结构型模式
结构型模式处理类或对象的组合。它们确保当系统的一部分发生变化时,整个系统不需要改变。
### 行为型模式
行为型模式描述类或对象如何交互和分配职责。它们专注于对象之间的通信。
## 贡献
欢迎贡献!您可以通过以下方式参与:
1. 报告错误和问题
2. 建议新功能或改进
3. 添加其他语言的实现
4. 改进文档
5. 添加更全面的示例
贡献步骤:
1. Fork 仓库
2. 创建功能分支 (`git checkout -b feature/amazing-feature`)
3. 提交更改 (`git commit -m 'Add some amazing feature
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>'`)
4. 推送到分支 (`git push origin feature/amazing-feature`)
5. 开启 Pull Request
## 许可证
本项目采用 MIT 许可证。
## 致谢
- Gang of FourErich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides对设计模式的开创性工作
- 开源社区提供的灵感和示例
- 所有帮助维护和改进此仓库的贡献者