ReviseAlgo Logo

Design Patterns

Strategy Pattern

Master the Strategy Pattern in JavaScript. Learn to encapsulate algorithms dynamically, swap execution behaviors, and avoid complex conditional statements.

Last Updated: July 15, 2026 10 min read

1. Introduction

The Strategy Pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable at runtime.

2. Why It Matters

Using complex conditional blocks (like if-else or switch statements) to select algorithms dynamically makes code difficult to maintain and violates the Open-Closed Principle. The Strategy pattern solves this by encapsulating each algorithm in a separate class or function.

3. Real-World Analogy

Think of a Travel Navigation System:

  • Monolithic switch block: A single system with a massive instruction list: "If travel mode is car, do A. If travel mode is walking, do B. If travel mode is train, do C." Modifying a route type requires changing the entire navigation script.
  • Strategy Pattern (Modular route plugins): The navigator accepts interchangeable route calculators: a "WalkingStrategy", a "DrivingStrategy", or a "TransitStrategy". You select the strategy on the screen, and the navigator calls its standardized buildRoute() method, swapping behaviors dynamically.

4. Implementing Strategy

You can implement the Strategy pattern by passing different strategy instances to a context class:

5. Functional Strategy (JavaScript Pattern)

Because JavaScript supports first-class functions, you can implement the Strategy pattern easily by passing functions directly, avoiding classes entirely:

6. Practical Example

This script demonstrates using the Strategy pattern to select different data validation strategies dynamically:

7. Common Mistakes

  • Using Strategy pattern for simple conditional checks: If a class has a single conditional branch that rarely changes, introducing the Strategy pattern adds unnecessary complexity. Use standard conditional logic instead.

8. Quick Quiz

Q1: Which SOLID design principle is directly promoted by the Strategy Pattern?

A) Single Responsibility Principle

B) Open-Closed Principle (open for extension, closed for modification)

Answer: B — The Strategy pattern allows you to add new algorithms (strategies) without modifying the context class, promoting the Open-Closed Principle.

9. Scenario-Based Challenge

The Multi-Client Compression Tool:

An application packages file directories. You want to support multiple compression formats: ZIP and GZIP. Design a compression context class that accepts interchangeable compression strategies dynamically.

10. Debugging Exercise

Explain why this strategy context crashes, and how to fix it:

class ShippingStrategy {
  calculate(weight) { return weight * 1.5; }
}

class ShippingCart { constructor(strategy) { this.strategy = strategy; }

getCost(w) { // Bug: strategy is a class, not an instance! return this.strategy.calculate(w); // throws TypeError: Cannot read property 'calculate' of undefined! Why? } } const cart = new ShippingCart(ShippingStrategy); // passing constructor reference! cart.getCost(10);

View Solution

Diagnosis: The code passes the class constructor reference ShippingStrategy instead of an instantiated object, which causes the method call on the instance to fail.

Fix: Instantiate the strategy object using the new keyword before passing it to the context, or rewrite strategies as simple functions:

const cart = new ShippingCart(new ShippingStrategy()); // Pass instance
console.log(cart.getCost(10)); // 15

11. Interview Questions

🟢 Q1: Explain how JavaScript's support for first-class functions simplifies the implementation of the Strategy Pattern.

Answer: In languages like Java or C++, implementing the Strategy pattern requires declaring an interface, implementing multiple concrete classes, and instantiating strategy objects.
Because JavaScript supports first-class functions, you can define strategies as simple functions and pass them as callbacks directly. The context class calls the callback function directly, removing the need to write interfaces and concrete strategy classes.

12. Production Considerations

  • Interface Consistency: Ensure that all concrete strategies share the exact same method signature (inputs and output types) to prevent runtime errors when swapping strategies.