搜索

PythonReactSEOPDF
GitHub Skill

design-pattern-strategy

Use when multiple interchangeable algorithms are needed and clients should switch behavior without branching on concrete implementations.

zenobi-us☆ 39 仓库 Stars7 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Skill: Strategy

Intent

Define a family of algorithms behind a common strategy contract and select among them at runtime or configuration time. This keeps client flow stable while algorithm choices evolve.

Applicability Signals

  • Signal 1: Conditional branches select algorithm variants in many call sites.
  • Signal 2: New algorithm variants appear regularly and must be plugged in with low disruption.
  • Signal 3: Runtime environment, tenant policy, or feature flags determine algorithm choice.

Contraindications

  • Case 1: Only one algorithm exists and no variation pressure is expected.
  • Case 2: Variants differ by one parameter and can be handled by data/config only.
  • Case 3: Strategy selection criteria are unstable and undocumented, causing hidden behavior drift.

Decision Heuristics

  • If algorithm variation is the main axis of change and caller workflow should stay fixed, prefer Strategy.
  • If behavior evolves through lifecycle states with explicit transitions, State may be more appropriate.
  • Decision anti-bias note: avoid creating many micro-strategies for negligible differences.

Implementation Checklist

  • Define strategy interface with minimal method surface.
  • Implement concrete strategies for each meaningful algorithm variant.
  • Inject strategy into context (constructor/setter/config factory).
  • Keep selection policy separate from strategy implementation details.
  • Add tests proving strategy replacement without client code changes.

Misuse Checks

  • Misuse 1: Context inspects concrete strategy types → Remediation: move variant behavior behind strategy interface.
  • Misuse 2: Strategies rely on shared mutable hidden globals → Remediation: inject explicit dependencies/state.
  • Misuse 3: Selection logic duplicated across multiple callers → Remediation: centralize strategy factory/resolver.

Verification Rubric

  • Correctness:
    • Baseline strategy yields expected output/behavior.
    • Alternate strategy can be swapped with no client workflow edits.
  • Design quality:
    • Strategy contracts are cohesive and stable.
    • Selection policy is explicit and testable.
  • Regression safety:
    • Tests cover at least two concrete strategies plus selection boundary case.

Language-Specific Adaptations (Optional)

  • TypeScript: use generic strategy interfaces for typed input/output contracts.
  • Python: strategies can be classes or callables; keep contract explicit via protocols.
  • Go: use interface-based strategies and constructor injection into services.

Related Patterns (Optional)

  • State: strategy chooses algorithm; state models lifecycle-dependent behavior transitions.
  • Template Method: template fixes workflow skeleton; strategy swaps specific algorithm steps.

Attribution & Sources

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-03-11
索引更新时间
2026-03-11
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!