重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
npx skills add https://github.com/ratacat/claude-skills --skill design-patterns设计模式是针对反复出现的软件设计问题的成熟解决方案。它们为讨论设计提供了共享的词汇表,并汇集了通过数十年实际使用提炼出的集体智慧。
核心理念: 模式是你可以根据自身情境进行调整的模板,而非需要复制的蓝图。只有当模式能真正简化你的设计时才使用它——而不是为了炫技或过度设计。
这些原则是所有良好设计的基础:
| 原则 | 含义 | 违反的症状 |
|---|---|---|
| 封装变化的部分 | 将变化的部分与稳定的部分隔离 | 变更在代码库中产生连锁反应 |
| 面向接口编程 | 依赖抽象,而非具体实现 | 无法交换实现 |
| 组合优于继承 | 通过组合对象来构建行为 | 深层僵化的类层次结构 |
| 松耦合 | 最小化对象间的相互依赖 | 无法在不破坏其他部分的情况下更改某一部分 |
| 开闭原则 | 对扩展开放,对修改关闭 | 必须修改现有代码以添加新功能 |
| 单一职责 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 每个类只有一个变更理由 |
| 类承担了过多职责 |
| 依赖倒置 | 高层模块不依赖于低层模块 | 业务逻辑与基础设施耦合 |
创建对象
├── 复杂/有条件的创建 ──────────→ 工厂方法
├── 相关对象族 ───────────→ 抽象工厂
├── 分步构建 ─────────────→ 建造者
├── 克隆现有对象 ────────────────→ 原型
└── 需要单一实例 ────────────────→ 单例(谨慎使用!)
组织/组合对象
├── 接口不兼容 ────────────────→ 适配器
├── 简化复杂子系统 ────────────→ 外观
├── 树形/层次结构 ──────────────→ 组合
├── 动态添加行为 ──────────────→ 装饰器
└── 控制对象访问 ──────────────→ 代理
管理通信/行为
├── 一对多通知 ──────────────→ 观察者
├── 将请求封装为对象 ───────→ 命令
├── 行为随内部状态变化 ─────→ 状态
├── 运行时交换算法 ────────────→ 策略
├── 带有钩子的算法骨架 ─────────→ 模板方法
├── 减少 N 对 N 通信 ───────────→ 中介者
└── 顺序处理器 ───────────────────→ 责任链
管理数据访问
├── 抽象数据源 ──────────────────→ 仓储
├── 跟踪变更以实现原子提交 ───────→ 工作单元
├── 确保对象标识 ────────────────→ 标识映射
├── 延迟昂贵加载 ────────────────→ 延迟加载
├── 将对象映射到数据库 ───────────→ 数据映射器
└── 为传输塑造数据 ───────────────→ 数据传输对象
| 症状 | 考虑 |
|---|---|
| 基于类型的巨型 switch/if-else | 策略、状态或多态 |
| 跨类重复代码 | 模板方法、策略 |
| 需要通知多个对象变更 | 观察者 |
| 复杂的对象创建逻辑 | 工厂、建造者 |
| 添加功能导致类膨胀 | 装饰器 |
| 第三方 API 不适合你的代码 | 适配器 |
| 组件间依赖过多 | 中介者、外观 |
| 没有数据库/网络就无法测试 | 仓储、依赖注入 |
| 需要撤销/重做 | 命令 |
| 对象行为依赖于状态 | 状态 |
| 请求需要由多个处理器处理 | 责任链 |
| 因素 | 事务脚本 | 领域模型 |
|---|---|---|
| 逻辑复杂度 | 简单(< 500 行) | 复杂,规则众多 |
| 业务规则 | 少,直接 | 多,相互关联 |
| 操作 | 以 CRUD 为主 | 行为丰富 |
| 团队/时间线 | 小团队,快速交付 | 长期维护 |
| 测试 | 集成测试 | 针对领域的单元测试 |
经验法则: 从事务脚本开始。当过程式代码变得难以维护时,再重构为领域模型。
| 模式 | 一句话描述 | 何时使用 | 参考 |
|---|---|---|---|
| 策略 | 封装可互换的算法 | 有多种方式做某事,需要在运行时切换 | strategy.md |
| 观察者 | 通知依赖者状态变化 | 事件系统,响应式更新 | observer.md |
| 工厂 | 封装对象创建 | 复杂/有条件的实例化 | factory.md |
| 装饰器 | 动态添加行为 | 无需继承即可扩展 | decorator.md |
| 命令 | 将请求封装为对象 | 撤销/重做、队列、日志记录 | command.md |
| 模式 | 一句话描述 | 何时使用 | 参考 |
|---|---|---|---|
| 适配器 | 转换接口 | 集成不兼容的代码 | adapter.md |
| 外观 | 简化复杂子系统 | 通过简单 API 隐藏复杂性 | facade.md |
| 组合 | 统一的树形结构 | 部分-整体层次结构 | composite.md |
| 代理 | 控制对象访问 | 延迟加载、访问控制、缓存 | proxy.md |
| 模式 | 一句话描述 | 何时使用 | 参考 |
|---|---|---|---|
| 仓储 | 类似集合的数据访问 | 解耦领域层与数据层 | repository.md |
| 工作单元 | 协调原子变更 | 事务管理 | unit-of-work.md |
| 服务层 | 编排业务操作 | 定义应用边界 | service-layer.md |
| DTO | 为传输塑造数据 | API 契约,防止过度暴露 | dto.md |
| 模式 | 一句话描述 | 何时使用 | 参考 |
|---|---|---|---|
| 建造者 | 分步构建对象 | 复杂对象,流畅 API | builder.md |
| 状态 | 行为随状态变化 | 状态机,工作流 | state.md |
| 模板方法 | 带有钩子的算法骨架 | 框架扩展点 | template-method.md |
| 责任链 | 沿处理器链传递请求 | 中间件,管道 | chain-of-responsibility.md |
| 中介者 | 集中化复杂通信 | 减少组件耦合 | mediator.md |
| 延迟加载 | 延迟昂贵加载 | 性能,大型对象图 | lazy-load.md |
| 标识映射 | 确保对象标识 | ORM,防止重复 | identity-map.md |
| 错误 | 症状 | 修复方法 |
|---|---|---|
| 模式滥用 | 简单操作需要导航许多类 | 仅在解决实际问题时使用 |
| 选错模式 | 代码感觉生硬、别扭 | 重新审视实际问题 |
| 继承滥用 | 深层层次结构,脆弱的基类 | 优先使用组合(策略、装饰器) |
| 单例滥用 | 全局状态,隐藏依赖,难以测试 | 改用依赖注入 |
| 过早抽象 | 接口只有一个实现 | 等待真正的变化需求 |
| 现代模式 | 基于 | 描述 |
|---|---|---|
| 依赖注入 | 策略 + 工厂 | 容器创建并注入依赖 |
| 中间件 | 装饰器 + 责任链 | 请求/响应管道 |
| 事件溯源 | 命令 | 将状态变更存储为事件 |
| CQRS | 命令/查询分离 | 分离读写模型 |
| 钩子(React/Vue) | 观察者 + 策略 | 函数式生命周期订阅 |
在实现模式之前:
每周安装量
48
代码仓库
GitHub 星标数
28
首次出现
2026年1月20日
安全审计
安装于
opencode40
gemini-cli39
codex38
github-copilot35
cursor33
amp33
Design patterns are proven solutions to recurring software design problems. They provide a shared vocabulary for discussing design and capture collective wisdom refined through decades of real-world use.
Core Philosophy: Patterns are templates you adapt to your context, not blueprints to copy. Use the right pattern when it genuinely simplifies your design—not to impress or over-engineer.
These principles underpin all good design:
| Principle | Meaning | Violation Symptom |
|---|---|---|
| Encapsulate What Varies | Isolate changing parts from stable parts | Changes ripple through codebase |
| Program to Interfaces | Depend on abstractions, not concretions | Can't swap implementations |
| Composition Over Inheritance | Build behavior by composing objects | Deep rigid class hierarchies |
| Loose Coupling | Minimize interdependency between objects | Can't change one thing without breaking another |
| Open-Closed | Open for extension, closed for modification | Must edit existing code for new features |
| Single Responsibility | One reason to change per class | Classes doing too many things |
| Dependency Inversion | High-level modules don't depend on low-level | Business logic coupled to infrastructure |
CREATING OBJECTS
├── Complex/conditional creation ──────────→ Factory Method
├── Families of related objects ───────────→ Abstract Factory
├── Step-by-step construction ─────────────→ Builder
├── Clone existing objects ────────────────→ Prototype
└── Single instance needed ────────────────→ Singleton (use sparingly!)
STRUCTURING/COMPOSING OBJECTS
├── Incompatible interface ────────────────→ Adapter
├── Simplify complex subsystem ────────────→ Facade
├── Tree/hierarchy structure ──────────────→ Composite
├── Add behavior dynamically ──────────────→ Decorator
└── Control access to object ──────────────→ Proxy
MANAGING COMMUNICATION/BEHAVIOR
├── One-to-many notification ──────────────→ Observer
├── Encapsulate requests as objects ───────→ Command
├── Behavior varies by internal state ─────→ State
├── Swap algorithms at runtime ────────────→ Strategy
├── Algorithm skeleton with hooks ─────────→ Template Method
├── Reduce N-to-N communication ───────────→ Mediator
└── Sequential handlers ───────────────────→ Chain of Responsibility
MANAGING DATA ACCESS
├── Abstract data source ──────────────────→ Repository
├── Track changes for atomic commit ───────→ Unit of Work
├── Ensure object identity ────────────────→ Identity Map
├── Defer expensive loading ───────────────→ Lazy Load
├── Map objects to database ───────────────→ Data Mapper
└── Shape data for transfer ───────────────→ DTO
| Symptom | Consider |
|---|---|
| Giant switch/if-else on type | Strategy, State, or polymorphism |
| Duplicate code across classes | Template Method, Strategy |
| Need to notify many objects of changes | Observer |
| Complex object creation logic | Factory, Builder |
| Adding features bloats class | Decorator |
| Third-party API doesn't fit your code | Adapter |
| Too many dependencies between components | Mediator, Facade |
| Can't test without database/network | Repository, Dependency Injection |
| Need undo/redo | Command |
| Object behavior depends on state | State |
| Request needs processing by multiple handlers | Chain of Responsibility |
| Factor | Transaction Script | Domain Model |
|---|---|---|
| Logic complexity | Simple (< 500 lines) | Complex, many rules |
| Business rules | Few, straightforward | Many, interacting |
| Operations | CRUD-heavy | Rich behavior |
| Team/timeline | Small team, quick delivery | Long-term maintenance |
| Testing | Integration tests | Unit tests on domain |
Rule of thumb: Start with Transaction Script. Refactor to Domain Model when procedural code becomes hard to maintain.
| Pattern | One-Line | When to Use | Reference |
|---|---|---|---|
| Strategy | Encapsulate interchangeable algorithms | Multiple ways to do something, swap at runtime | strategy.md |
| Observer | Notify dependents of state changes | Event systems, reactive updates | observer.md |
| Factory | Encapsulate object creation | Complex/conditional instantiation | factory.md |
| Decorator | Add behavior dynamically | Extend without inheritance | decorator.md |
| Pattern | One-Line | When to Use | Reference |
|---|---|---|---|
| Adapter | Convert interfaces | Integrate incompatible code | adapter.md |
| Facade | Simplify complex subsystems | Hide complexity behind simple API | facade.md |
| Composite | Uniform tree structures | Part-whole hierarchies | composite.md |
| Proxy | Control access to objects | Lazy load, access control, caching | proxy.md |
| Pattern | One-Line | When to Use | Reference |
|---|---|---|---|
| Repository | Collection-like data access | Decouple domain from data layer | repository.md |
| Unit of Work | Coordinate atomic changes | Transaction management | unit-of-work.md |
| Service Layer | Orchestrate business operations | Define application boundary | service-layer.md |
| DTO | Shape data for transfer | API contracts, prevent over-exposure | dto.md |
| Pattern | One-Line | When to Use | Reference |
|---|---|---|---|
| Builder | Step-by-step object construction | Complex objects, fluent APIs | builder.md |
| State | Behavior changes with state | State machines, workflow | state.md |
| Template Method | Algorithm skeleton with hooks | Framework extension points | template-method.md |
| Chain of Responsibility | Pass request along handlers | Middleware, pipelines | chain-of-responsibility.md |
| Mistake | Symptom | Fix |
|---|---|---|
| Pattern Overuse | Simple operations require navigating many classes | Only use when solving real problem |
| Wrong Pattern | Code feels forced, awkward | Re-examine actual problem |
| Inheritance Abuse | Deep hierarchies, fragile base class | Favor composition (Strategy, Decorator) |
| Singleton Abuse | Global state, hidden dependencies, hard to test | Use dependency injection instead |
| Premature Abstraction | Interfaces with single implementation | Wait for real need to vary |
| Modern Pattern | Based On | Description |
|---|---|---|
| Dependency Injection | Strategy + Factory | Container creates and injects dependencies |
| Middleware | Decorator + Chain of Responsibility | Request/response pipeline |
| Event Sourcing | Command | Store state changes as events |
| CQRS | Command/Query separation | Separate read/write models |
| Hooks (React/Vue) | Observer + Strategy | Functional lifecycle subscriptions |
Before implementing a pattern:
Weekly Installs
48
Repository
GitHub Stars
28
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode40
gemini-cli39
codex38
github-copilot35
cursor33
amp33
Flutter/Dart代码审查最佳实践:提升应用性能与质量的完整检查清单
1,200 周安装
| Command | Encapsulate requests as objects | Undo/redo, queuing, logging | command.md |
| Mediator | Centralize complex communication | Reduce component coupling | mediator.md |
| Lazy Load | Defer expensive loading | Performance, large object graphs | lazy-load.md |
| Identity Map | Ensure object identity | ORM, prevent duplicates | identity-map.md |