gof-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GoF 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)

创建型模式(对象创建)

PatternIntentUse When
Abstract FactoryCreate families of related objectsNeed platform/theme independence
BuilderConstruct complex objects step-by-stepObject has many optional parts
Factory MethodLet subclasses decide which class to instantiateDon't know concrete types ahead of time
PrototypeClone existing objectsObject creation is expensive
SingletonEnsure single instanceNeed exactly one shared instance
模式意图使用场景
Abstract Factory创建相关对象族需要平台/主题独立性
Builder逐步构建复杂对象对象包含多个可选部分
Factory Method让子类决定实例化哪个类无法提前知晓具体类型
Prototype克隆现有对象对象创建成本高昂
Singleton确保单一实例需要恰好一个共享实例

Structural Patterns (Composition)

结构型模式(组合)

PatternIntentUse When
AdapterConvert interface to expected interfaceIntegrating incompatible interfaces
BridgeSeparate abstraction from implementationNeed to vary both independently
CompositeTreat individual and groups uniformlyHave tree structures
DecoratorAdd responsibilities dynamicallyNeed flexible extension
FacadeSimplified interface to subsystemComplex subsystem needs simple API
FlyweightShare common state efficientlyMany similar objects needed
ProxyControl access to objectNeed lazy loading, access control, logging
模式意图使用场景
Adapter将接口转换为预期接口集成不兼容的接口
Bridge将抽象与实现分离需要独立变化抽象和实现
Composite统一处理单个对象和对象组存在树形结构
Decorator动态添加职责需要灵活扩展功能
Facade为子系统提供简化接口复杂子系统需要简单API
Flyweight高效共享公共状态需要大量相似对象
Proxy控制对象访问需要懒加载、访问控制或日志记录

Behavioral Patterns (Communication)

行为型模式(通信)

PatternIntentUse When
Chain of ResponsibilityPass request along handler chainMultiple handlers, unknown which handles
CommandEncapsulate request as objectNeed undo, queue, or log operations
InterpreterDefine grammar and interpret sentencesHave a simple language to parse
IteratorSequential access without exposing internalsNeed to traverse collections
MediatorCentralize complex communicationsMany objects communicate in complex ways
MementoCapture and restore object stateNeed undo/snapshot functionality
ObserverNotify dependents of state changesOne-to-many dependency
StateAlter behavior when state changesObject behavior depends on state
StrategyEncapsulate interchangeable algorithmsNeed to swap algorithms at runtime
Template MethodDefine skeleton, let subclasses fill inAlgorithm structure fixed, steps vary
VisitorAdd operations without changing classesNeed 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

如何使用此参考资料

  1. Choosing a pattern: Start with the decision guide or tables above
  2. Learning a pattern: Read the full documentation with examples
  3. Quick reminder: Use the tables above for at-a-glance reference
  4. Implementation: Follow the pseudocode, adapt to your language
  1. 选择模式:从决策指南或上方表格开始
  2. 学习模式:阅读完整文档及示例
  3. 快速查阅:使用上方表格进行概览式参考
  4. 实现:遵循伪代码,适配你的编程语言

Language Translation Notes

语言转换说明

The pseudocode in this reference uses these conventions:
  • class
    for type definitions
  • function
    for methods/functions
  • ->
    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
    ,
    function
    /arrow,
    .
    ,
    //
    , TS types
  • Python:
    class
    ,
    def
    ,
    .
    ,
    #
    , type hints
  • Java/C#: Direct mapping with
    new
    , generics

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年出版的《设计模式:可复用面向对象软件的基础》一书中的概念。