AgentSkillsCN

design-patterns

协助智能体检索、筛选并应用软件设计模式(GoF及更多)。 当用户出现以下需求时,可运用此技能: - 用户描述问题并寻求模式推荐(例如:“如何实现解耦……”、“需要灵活的对象创建”) - 用户询问特定模式(例如:“请解释工厂模式”、“何时该使用策略模式”) - 用户希望获得特定语言的代码示例(Python、TypeScript、Java、Go等) - 用户需要帮助识别现有代码中的模式 - 用户正在进行重构,并考虑应用相关模式 - 问题涉及:对象创建、结构化组合、行为通信、SOLID原则

SKILL.md
--- frontmatter
name: design-patterns
description: |
  Help agents retrieve, filter, and apply software design patterns (GoF and beyond).
  Use this skill when:
  - User describes a problem and needs pattern recommendations (e.g., "how to decouple...", "need flexible object creation")
  - User asks about specific patterns (e.g., "explain Factory pattern", "when to use Strategy")
  - User wants code examples in specific languages (Python, TypeScript, Java, Go, etc.)
  - User needs help identifying patterns in existing code
  - User is refactoring and considering pattern application
  - Questions involve: object creation, structural composition, behavioral communication, SOLID principles

Design Patterns Skill

Quick reference for applying Gang of Four (GoF) and modern design patterns.

Pattern Categories

CategoryPurposeCommon Patterns
CreationalObject creation mechanismsFactory, Builder, Singleton, Prototype
StructuralObject compositionAdapter, Decorator, Facade, Proxy, Composite
BehavioralObject communicationStrategy, Observer, Command, State, Chain of Responsibility

Quick Pattern Selection

"I need flexible object creation"

-> Factory Method (single product) or Abstract Factory (product families)

"I need to build complex objects step by step"

-> Builder

"I need to add behavior without modifying classes"

-> Decorator (wrapping) or Strategy (injection)

"I need to decouple components"

-> Observer (events), Mediator (central hub), or Command (requests as objects)

"I need to adapt incompatible interfaces"

-> Adapter

"I need to simplify a complex subsystem"

-> Facade

"I need state-dependent behavior"

-> State

"I need to process requests through multiple handlers"

-> Chain of Responsibility

Detailed References

For implementation details and code examples, read the appropriate reference file:

Identifying Patterns in Code

Look for these signatures:

Code SignatureLikely Pattern
create_xxx() returning interface typesFactory
.set_xxx().set_yyy().build() chainsBuilder
Class wrapping another of same interfaceDecorator
execute() / undo() methods with command objectsCommand
notify() / update() / subscribe()Observer
handle() passing to next_handlerChain of Responsibility
State classes with handle() methodsState
algorithm injected/swapped at runtimeStrategy

Anti-Pattern Warnings

  • Singleton overuse: Often hides dependencies; prefer DI
  • God Factory: Factory creating too many unrelated types
  • Pattern fever: Applying patterns where simple code suffices
  • Premature abstraction: Adding patterns before actual need

Usage Examples

Recommend a pattern:

"I have multiple payment methods (credit card, PayPal, crypto) and need to process payments differently for each" -> Recommend Strategy pattern, read references/behavioral.md#strategy

Explain implementation:

"Show me how to implement Observer in Python" -> Read references/behavioral.md#observer for code examples

Identify in code:

"What pattern is this code using?" [code with handlers chain] -> Analyze against signatures table, likely Chain of Responsibility