gof-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoF Design Patterns Reference
GoF设计模式参考资料
A comprehensive reference for the Gang of Four design patterns. This skill provides language-agnostic guidance with pseudocode examples that can be translated to any programming language.
这是一份关于四人组(Gang of Four)设计模式的全面参考资料。此技能提供与语言无关的指导,并附带可转换为任何编程语言的伪代码示例。
When This Skill Activates
此技能的激活场景
This skill automatically activates when you:
- Ask about or need to implement a design pattern
- Need to choose between patterns for a design problem
- Are refactoring code and considering structural improvements
- Discuss architecture, decoupling, or extensibility
- Mention specific patterns: factory, singleton, observer, decorator, etc.
当你进行以下操作时,此技能会自动激活:
- 询问或需要实现某种设计模式
- 针对某个设计问题需要在多种模式间做选择
- 正在重构代码并考虑结构优化
- 讨论架构、解耦或可扩展性相关内容
- 提及特定模式:factory、singleton、observer、decorator等
Quick Pattern Reference
快速模式参考
Creational Patterns (Object Creation)
创建型模式(对象创建)
| Pattern | Intent | Use When |
|---|---|---|
| Abstract Factory | Create families of related objects | Need platform/theme independence |
| Builder | Construct complex objects step-by-step | Object has many optional parts |
| Factory Method | Let subclasses decide which class to instantiate | Don't know concrete types ahead of time |
| Prototype | Clone existing objects | Object creation is expensive |
| Singleton | Ensure single instance | Need exactly one shared instance |
| 模式 | 意图 | 使用场景 |
|---|---|---|
| Abstract Factory | 创建相关对象族 | 需要平台/主题独立性 |
| Builder | 逐步构建复杂对象 | 对象包含多个可选部分 |
| Factory Method | 让子类决定实例化哪个类 | 无法提前知晓具体类型 |
| Prototype | 克隆现有对象 | 对象创建成本高昂 |
| Singleton | 确保单一实例 | 需要恰好一个共享实例 |
Structural Patterns (Composition)
结构型模式(组合)
| Pattern | Intent | Use When |
|---|---|---|
| Adapter | Convert interface to expected interface | Integrating incompatible interfaces |
| Bridge | Separate abstraction from implementation | Need to vary both independently |
| Composite | Treat individual and groups uniformly | Have tree structures |
| Decorator | Add responsibilities dynamically | Need flexible extension |
| Facade | Simplified interface to subsystem | Complex subsystem needs simple API |
| Flyweight | Share common state efficiently | Many similar objects needed |
| Proxy | Control access to object | Need lazy loading, access control, logging |
| 模式 | 意图 | 使用场景 |
|---|---|---|
| Adapter | 将接口转换为预期接口 | 集成不兼容的接口 |
| Bridge | 将抽象与实现分离 | 需要独立变化抽象和实现 |
| Composite | 统一处理单个对象和对象组 | 存在树形结构 |
| Decorator | 动态添加职责 | 需要灵活扩展功能 |
| Facade | 为子系统提供简化接口 | 复杂子系统需要简单API |
| Flyweight | 高效共享公共状态 | 需要大量相似对象 |
| Proxy | 控制对象访问 | 需要懒加载、访问控制或日志记录 |
Behavioral Patterns (Communication)
行为型模式(通信)
| Pattern | Intent | Use When |
|---|---|---|
| Chain of Responsibility | Pass request along handler chain | Multiple handlers, unknown which handles |
| Command | Encapsulate request as object | Need undo, queue, or log operations |
| Interpreter | Define grammar and interpret sentences | Have a simple language to parse |
| Iterator | Sequential access without exposing internals | Need to traverse collections |
| Mediator | Centralize complex communications | Many objects communicate in complex ways |
| Memento | Capture and restore object state | Need undo/snapshot functionality |
| Observer | Notify dependents of state changes | One-to-many dependency |
| State | Alter behavior when state changes | Object behavior depends on state |
| Strategy | Encapsulate interchangeable algorithms | Need to swap algorithms at runtime |
| Template Method | Define skeleton, let subclasses fill in | Algorithm structure fixed, steps vary |
| Visitor | Add operations without changing classes | Need to add many operations to stable structure |
| 模式 | 意图 | 使用场景 |
|---|---|---|
| Chain of Responsibility | 沿处理链传递请求 | 存在多个处理者,不确定哪个会处理请求 |
| Command | 将请求封装为对象 | 需要撤销、排队或记录操作 |
| Interpreter | 定义语法并解释语句 | 需要解析简单语言 |
| Iterator | 顺序访问集合而不暴露内部结构 | 需要遍历集合 |
| Mediator | 集中处理复杂通信 | 多个对象间通信方式复杂 |
| Memento | 捕获并恢复对象状态 | 需要撤销/快照功能 |
| Observer | 状态变化时通知依赖对象 | 存在一对多依赖关系 |
| State | 状态变化时改变行为 | 对象行为依赖其状态 |
| Strategy | 封装可互换的算法 | 需要在运行时切换算法 |
| Template Method | 定义算法骨架,让子类填充步骤 | 算法结构固定,但步骤可变 |
| Visitor | 在不修改类的情况下添加操作 | 需要为稳定结构添加多种操作 |
Decision Guide
决策指南
See Pattern Selection Guide for help choosing the right pattern.
如需帮助选择合适的模式,请查看模式选择指南。
How to Use This Reference
如何使用此参考资料
- Choosing a pattern: Start with the decision guide or tables above
- Learning a pattern: Read the full documentation with examples
- Quick reminder: Use the tables above for at-a-glance reference
- Implementation: Follow the pseudocode, adapt to your language
- 选择模式:从决策指南或上方表格开始
- 学习模式:阅读完整文档及示例
- 快速查阅:使用上方表格进行概览式参考
- 实现:遵循伪代码,适配你的编程语言
Language Translation Notes
语言转换说明
The pseudocode in this reference uses these conventions:
- for type definitions
class - for methods/functions
function - for method calls on objects
-> - for comments
// - Type hints shown as
name: Type
Translate to your language:
- PHP: ,
class,function,->, type hints in docblocks or PHP 8+// - JavaScript/TypeScript: ,
class/arrow,function,., TS types// - Python: ,
class,def,., type hints# - Java/C#: Direct mapping with , generics
new
Based on concepts from "Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson, and Vlissides (Gang of Four), 1994.
本参考资料中的伪代码遵循以下约定:
- 用于类型定义
class - 用于方法/函数
function - 用于对象方法调用
-> - 用于注释
// - 类型提示格式为
name: Type
转换为你的编程语言时:
- PHP:使用 、
class、function、->,类型提示可放在文档块或使用PHP 8+语法// - JavaScript/TypeScript:使用 、
class/箭头函数、function、.,TypeScript类型// - Python:使用 、
class、def、.,类型提示# - Java/C#:直接映射,使用 、泛型
new
基于Gamma、Helm、Johnson和Vlissides(四人组)于1994年出版的《设计模式:可复用面向对象软件的基础》一书中的概念。