AgentSkillsCN

design-patterns

选择并应用GoF设计模式:工厂模式、建造者模式、策略模式、观察者模式、适配器模式、装饰器模式。在解决反复出现的设计问题,或对多组件变更进行结构化处理时,可使用此功能。

SKILL.md
--- frontmatter
name: design-patterns
description: Select and apply GoF design patterns: Factory, Builder, Strategy, Observer, Adapter, Decorator. Use when solving recurring design problems or structuring multi-component changes.

Applicability Rubric

ConditionPassFail
Multi-component changeFeature affects multiple componentsSingle component change
Recognizable problemMatches known design problemUnique/novel problem
Flexibility neededRequires extensible solutionFixed requirements
Recurring challengeSolving common design issueOne-off implementation

Apply when: Any condition passes

Core Principles

Pattern Selection Process

code
1. Identify the problem
   ↓
2. Match problem to pattern category
   ↓
3. Evaluate pattern candidates
   ↓
4. Consider trade-offs
   ↓
5. Apply pattern minimally

Pattern Categories

CategoryPurposeCommon Patterns
CreationalObject creationFactory, Builder, Singleton
StructuralObject compositionAdapter, Decorator, Facade
BehavioralObject interactionStrategy, Observer, Command

Common Patterns Quick Reference

Creational Patterns

PatternUse WhenExample
Factory MethodObject creation varies by contextcreateLogger(type)
BuilderComplex object constructionFluent configuration
SingletonSingle instance needed globallyConfiguration manager

Structural Patterns

PatternUse WhenExample
AdapterInterface incompatibilityWrap legacy API
DecoratorAdd behavior dynamicallyLogging wrapper
FacadeSimplify complex subsystemUnified API client
CompositeTree structuresUI components

Behavioral Patterns

PatternUse WhenExample
StrategyAlgorithm varies at runtimePayment methods
ObserverOne-to-many notificationsEvent system
CommandEncapsulate operationsUndo/redo actions
StateBehavior changes with stateOrder status

Pattern Decision Table

NeedSituationPattern
Create objectsVarying types by contextFactory Method
Create objectsMany optional parametersBuilder
Create objectsExactly one instanceSingleton
Structure objectsIncompatible interfaceAdapter
Structure objectsAdd responsibilities dynamicallyDecorator
Structure objectsSimplify complex subsystemFacade
Structure objectsTree hierarchiesComposite
Define behaviorAlgorithm varies at runtimeStrategy
Define behaviorOne-to-many notificationsObserver
Define behaviorOperations that can be undoneCommand
Define behaviorBehavior changes with stateState

Completion Rubric

Before Applying

CriterionPassFail
Problem identificationProblem clearly definedVague problem statement
Real needSolves actual problemHypothetical/speculative
Simpler alternativesConsidered simpler options firstJumped to pattern
Team familiarityTeam understands patternPattern is obscure

During Implementation

CriterionPassFail
Minimal applicationPattern applied minimallyOver-applied
Clear namingNames reflect pattern intentGeneric/unclear names
Intent preservationPattern intent maintainedPattern misused
Appropriate complexityComplexity justifiedOver-engineered

After Implementation

CriterionPassFail
Flexibility gainedCode more flexibleSame or less flexible
DocumentedPattern documented if not obviousUndocumented complexity
TestedTests cover pattern behaviorPattern untested

Anti-Pattern Warning

Pattern Fever: Applying patterns everywhere

Signs:

  • Patterns for trivial problems
  • Multiple patterns where one suffices
  • Code harder to understand than before

Cure: YAGNI - Use patterns only when they solve real problems