AgentSkillsCN

design-patterns-core

为架构师提供模式选择指导。可借助MCP服务器获取语言特定的示例。

SKILL.md
--- frontmatter
name: design-patterns-core
description: Pattern selection guidance for architects. Use MCP servers for language-specific examples.

Design Patterns Selection Guide

When to Use Patterns

Creational Patterns

Use when object creation logic becomes complex or needs flexibility.

PatternUse When
Factory MethodCreating objects without specifying exact class; subclasses decide
Abstract FactoryCreating families of related objects without concrete classes
BuilderConstructing complex objects step-by-step; many optional parameters
PrototypeCloning existing objects; creation is expensive
SingletonExactly one instance needed globally (use sparingly)

Structural Patterns

Use when composing objects into larger structures.

PatternUse When
AdapterMaking incompatible interfaces work together
BridgeSeparating abstraction from implementation; both vary independently
CompositeTree structures; treating individual objects and compositions uniformly
DecoratorAdding behavior dynamically without subclassing
FacadeSimplifying complex subsystem with unified interface
FlyweightMany similar objects; sharing common state to save memory
ProxyControlling access; lazy loading, caching, access control

Behavioral Patterns

Use when defining communication between objects.

PatternUse When
Chain of ResponsibilityMultiple handlers; request passed until handled
CommandEncapsulating requests as objects; undo/redo, queuing
IteratorSequential access without exposing underlying structure
MediatorReducing chaotic dependencies; centralized communication
MementoSaving/restoring object state; undo functionality
ObserverOne-to-many notifications; event systems
StateBehavior changes based on internal state
StrategyInterchangeable algorithms; runtime selection
Template MethodAlgorithm skeleton; subclasses override specific steps
VisitorAdding operations to objects without modifying them

Pattern Selection by Problem

ProblemRecommended Pattern(s)
Object creation is complexBuilder, Factory Method
Need runtime algorithm selectionStrategy
Need to notify multiple objectsObserver
Incompatible interfacesAdapter
Complex subsystem accessFacade
Adding features without subclassingDecorator
Request handling chainChain of Responsibility
Undo/redo functionalityCommand, Memento
State-dependent behaviorState
Tree/hierarchical structuresComposite

Design Doc Integration

When specifying patterns in design docs:

markdown
## Design Patterns
- **[Pattern Name]** - [Component/area] - [Brief rationale]

Example:

markdown
## Design Patterns
- **Strategy** - PaymentProcessor - Runtime selection of payment providers
- **Observer** - EventBus - Decoupled notification of order status changes
- **Factory Method** - ServiceFactory - Creating service instances with dependencies

MCP Servers for Examples

Query language-specific MCP servers for implementation details:

  • go-design-patterns - Go examples
  • ts-design-patterns - TypeScript examples
  • php-design-patterns - PHP examples