Js strategy pattern

From Intense-Visions/harness-engineering · View interactive version

Js strategy pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Define a family of algorithms and make them interchangeable without altering the client

How to use this skill

Skill file

JS Strategy Pattern

Define a family of algorithms and make them interchangeable without altering the client

When to Use

Instructions

  1. Define a strategy interface — each strategy is a function or object with a common method signature.
  2. The context accepts a strategy and delegates the varying behavior to it.
  3. Swap strategies at runtime by passing a different function/object.
  4. Prefer plain functions as strategies in JavaScript — no need for class hierarchies.
// Strategies as plain functions
const strategies = {
  add: (a, b) => a + b,
  subtract: (a, b) => a - b,
  multiply: (a, b) => a * b,
};

function calculate(strategy, a, b) {
  if (!strategies[strategy]) throw new Error(`Unknown strategy: ${strategy}`);
  return strategies[strategy](a, b);
}

calculate('add', 5, 3); // 8
calculate('multiply', 5, 3); // 15
  1. Store strategies in a Map or object for O(1) lookup.

Details

The Strategy pattern separates the "what" from the "how." The context knows what operation to perform but delegates the implementation to an interchangeable strategy. In JavaScript, first-class functions make this pattern lightweight — a strategy is just a function.

Trade-offs:

When NOT to use:

Source

https://patterns.dev/javascript/strategy-pattern

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.