diff --git a/README.md b/README.md index e69de29..e4e1ec7 100644 --- a/README.md +++ b/README.md @@ -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 /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 .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 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 \ No newline at end of file diff --git a/README_CN.md b/README_CN.md new file mode 100644 index 0000000..a51ffa0 --- /dev/null +++ b/README_CN.md @@ -0,0 +1,242 @@ +# 设计模式实现 + +本仓库包含了 23 种经典 GoF(Gang 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 /main.go + ``` + +### Python 实现 + +1. 进入 Python 目录: + ```bash + cd python + ``` + +2. 每个模式目录包含: + - 实现模块 + - 示例用法 + - 测试(如适用) + +3. 运行示例: + ```bash + python -m .main + ``` + +### TypeScript 实现 + +1. 进入 TypeScript 目录: + ```bash + cd ts + ``` + +2. 安装依赖: + ```bash + pnpm install + ``` + +3. 每个模式目录包含: + - 实现文件 + - 示例用法 + - 测试(如适用) + +4. 运行示例: + ```bash + pnpm run + ``` + +## 模式分类 + +### 创建型模式 +创建型模式抽象了实例化过程。它们帮助系统独立于如何创建、组合和表示其对象。 + +### 结构型模式 +结构型模式处理类或对象的组合。它们确保当系统的一部分发生变化时,整个系统不需要改变。 + +### 行为型模式 +行为型模式描述类或对象如何交互和分配职责。它们专注于对象之间的通信。 + +## 贡献 + +欢迎贡献!您可以通过以下方式参与: + +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 '`) +4. 推送到分支 (`git push origin feature/amazing-feature`) +5. 开启 Pull Request + +## 许可证 + +本项目采用 MIT 许可证。 + +## 致谢 + +- Gang of Four(Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides)对设计模式的开创性工作 +- 开源社区提供的灵感和示例 +- 所有帮助维护和改进此仓库的贡献者