AgentSkillsCN

design-patterns

从 seedr 注册表中移除 toolr 来源的项目。 触发指令:“/remove-toolr <slug>”、“移除 toolr 项目”、“删除 toolr 技能/钩子/代理/插件”。 在 registry/manifest.json 中根据 slug 查找该项目(sourceType: “toolr”),与用户确认后, 删除 registry/<type>s/<slug>/ 中的本地文件,并移除 manifest 条目。

SKILL.md
--- frontmatter
name: design-patterns
description: >
  Gang of Four (GoF) design patterns reference covering all 23 classic patterns.
  Use when implementing, refactoring, or discussing software design patterns.
  Triggers on phrases like "use X pattern", "implement factory", "refactor to strategy",
  "which pattern for...", "design pattern help", or when code structure suggests a
  pattern would help (complex conditionals suggesting State/Strategy, object creation
  issues suggesting Factory/Builder, many similar objects suggesting Flyweight).

Design Patterns

Quick reference for the 23 Gang of Four design patterns organized by category.

Pattern Selection Guide

ProblemConsider
Complex object creationFactory Method, Abstract Factory, Builder
Clone existing objectsPrototype
Single shared instanceSingleton
Incompatible interfacesAdapter
Separate abstraction from implementationBridge
Tree structuresComposite
Add behavior dynamicallyDecorator
Simplify complex subsystemsFacade
Many similar objects (memory)Flyweight
Control access/lazy loadingProxy
Sequential handlersChain of Responsibility
Encapsulate requests as objectsCommand
Traverse collectionsIterator
Reduce coupling between componentsMediator
Save/restore state (undo)Memento
Event notificationObserver
Object behavior varies by stateState
Interchangeable algorithmsStrategy
Algorithm skeleton with variable stepsTemplate Method
Operations on object structuresVisitor

Pattern Categories

Creational Patterns

Object creation mechanisms. See references/creational.md.

PatternIntent
Factory MethodDefer instantiation to subclasses
Abstract FactoryCreate families of related objects
BuilderConstruct complex objects step-by-step
PrototypeClone existing objects
SingletonEnsure single instance with global access

Structural Patterns

Object composition and relationships. See references/structural.md.

PatternIntent
AdapterConvert interface to another expected interface
BridgeSeparate abstraction from implementation
CompositeTreat individual objects and compositions uniformly
DecoratorAdd responsibilities dynamically
FacadeSimplified interface to complex subsystem
FlyweightShare state to support many fine-grained objects
ProxyPlaceholder controlling access to another object

Behavioral Patterns

Object interaction and responsibility distribution. See references/behavioral.md.

PatternIntent
Chain of ResponsibilityPass request along handler chain
CommandEncapsulate request as object
IteratorSequential access without exposing representation
MediatorCentralize complex communications
MementoCapture and restore object state
ObserverNotify dependents of state changes
StateAlter behavior when state changes
StrategyInterchangeable algorithm family
Template MethodDefine algorithm skeleton, defer steps to subclasses
VisitorAdd operations without modifying classes

Quick Implementation Examples

Factory Method (TypeScript)

typescript
interface Product { operation(): string }

abstract class Creator {
  abstract createProduct(): Product
  doSomething(): string {
    const product = this.createProduct()
    return product.operation()
  }
}

class ConcreteCreator extends Creator {
  createProduct(): Product { return new ConcreteProduct() }
}

Strategy (TypeScript)

typescript
interface Strategy { execute(data: string[]): string[] }

class Context {
  constructor(private strategy: Strategy) {}
  setStrategy(strategy: Strategy) { this.strategy = strategy }
  doWork(data: string[]): string[] { return this.strategy.execute(data) }
}

// Usage: context.setStrategy(new SortAscending())

Observer (TypeScript)

typescript
interface Observer { update(state: any): void }

class Subject {
  private observers: Observer[] = []
  attach(o: Observer) { this.observers.push(o) }
  detach(o: Observer) { this.observers = this.observers.filter(x => x !== o) }
  notify(state: any) { this.observers.forEach(o => o.update(state)) }
}

Decorator (TypeScript)

typescript
interface Component { operation(): string }

class ConcreteComponent implements Component {
  operation() { return 'base' }
}

class Decorator implements Component {
  constructor(protected component: Component) {}
  operation() { return this.component.operation() }
}

class ConcreteDecorator extends Decorator {
  operation() { return `decorated(${super.operation()})` }
}

Resources

For detailed explanations, participants, and when-to-use guidance: