1
0
Fork 0
design-patterns/README_CN.md

6.6 KiB
Raw Permalink Blame History

设计模式实现

本仓库包含了 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 目录:

    cd go
    
  2. 运行示例:

    make
    

Python 实现

  1. 进入 Python 目录:

    cd python
    
  2. 运行示例:

    make
    

TypeScript 实现

  1. 进入 TypeScript 目录:

    cd ts
    
  2. 安装依赖:

    pnpm install
    
  3. 运行示例:

    make
    

贡献

欢迎贡献!您可以通过以下方式参与:

  1. 报告错误和问题
  2. 建议新功能或改进
  3. 添加其他语言的实现
  4. 改进文档
  5. 添加更全面的示例

贡献步骤:

  1. Fork 仓库
  2. 创建功能分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m 'Add some amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 开启 Pull Request

许可证

本项目采用 MIT 许可证。

致谢

  • Gang of FourErich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides对设计模式的开创性工作
  • 开源社区提供的灵感和示例
  • 所有帮助维护和改进此仓库的贡献者