Skip to content

Latest commit

 

History

History
79 lines (58 loc) · 3.77 KB

File metadata and controls

79 lines (58 loc) · 3.77 KB

Strategy Pattern

The Strategy Pattern is a behavioral design pattern that defines a family of algorithms, encapsulates each one of them, and makes them interchangeable. It lets the algorithm vary independently from the clients that use it.

Example in Node.js:

In this example, the Strategy Pattern is used to implement different sorting algorithms. SortStrategy defines the interface for sorting algorithms, while BubbleSort and QuickSort are concrete implementations of sorting algorithms. The Sorter class acts as the context that uses the selected sorting strategy to sort data. Clients can switch between different sorting strategies dynamically without modifying the client code.

// Strategy interface
class SortStrategy {
  sort(data) {}
}

// Concrete Strategy: Bubble Sort
class BubbleSort extends SortStrategy {
  sort(data) {
    console.log('Sorting using Bubble Sort');
    // Bubble sort implementation
    return data.sort((a, b) => a - b);
  }
}

// Concrete Strategy: Quick Sort
class QuickSort extends SortStrategy {
  sort(data) {
    console.log('Sorting using Quick Sort');
    // Quick sort implementation
    return data.sort((a, b) => a - b);
  }
}

// Context
class Sorter {
  constructor(strategy) {
    this.strategy = strategy;
  }

  setStrategy(strategy) {
    this.strategy = strategy;
  }

  sort(data) {
    return this.strategy.sort(data);
  }
}

// Usage
const bubbleSort = new BubbleSort();
const quickSort = new QuickSort();

const sorter = new Sorter(bubbleSort);
console.log(sorter.sort([3, 1, 2])); // Output: [1, 2, 3] (Sorted using Bubble Sort)

sorter.setStrategy(quickSort);
console.log(sorter.sort([3, 1, 2])); // Output: [1, 2, 3] (Sorted using Quick Sort)

Now for the Pros & Cons

Pros:
  1. Flexibility: The Strategy Pattern allows you to switch between different algorithms or strategies at runtime without altering the client code. This flexibility enables dynamic behavior in your application.

  2. Encapsulation: Each strategy is encapsulated into its own class, making it easier to understand, maintain, and extend the codebase. It also promotes the Single Responsibility Principle.

  3. Promotes Code Reuse: By encapsulating algorithms into separate classes, you can reuse them across different parts of your application or even in different applications.

  4. Enhanced Testability: Since each strategy is isolated from the rest of the code, it's easier to test them individually, leading to better unit testing and overall test coverage.

  5. Decouples Context and Strategy: The Strategy Pattern decouples the context (client) from the concrete implementations of algorithms, reducing dependencies and making the code more modular.

Cons:
  1. Increased Number of Classes: Implementing the Strategy Pattern may lead to an increased number of classes in your codebase, which could potentially make the system more complex and harder to understand.

  2. Potential Performance Overhead: Using the Strategy Pattern may introduce a slight performance overhead due to the additional layers of abstraction involved. However, this overhead is often negligible in most applications.

  3. Complexity in Configuration: If the application requires dynamic configuration of strategies or complex decision-making logic to choose between strategies, managing this configuration complexity can be challenging.

  4. Potential for Overdesign: In simple cases where only a single algorithm is used or where algorithms don't change frequently, applying the Strategy Pattern may be considered overengineering and could introduce unnecessary complexity.

  5. Tight Coupling to Strategies: If the context tightly couples with specific strategy implementations, it may limit the benefits of flexibility and interchangeability provided by the pattern.