Open In App

Strategy Method | JavaScript Design Pattern

Strategy Method or Strategy Pattern in JavaScript helps solve the problem of needing to use different methods or behaviors in your code and easily switch between them. Strategy Method is a behavioral design pattern in JavaScript that defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows the client to choose an algorithm from a family of algorithms at runtime, without altering the code that uses these algorithms.

Example of Strategy Method in JavaScript Design Patterns

The problem statement is essentially to create a flexible and extensible discount system for a shopping cart, allowing for different discount strategies to be easily added and applied to different shopping carts. This pattern is useful for scenarios where you want to decouple the discount logic from the shopping cart and make it easy to change or extend the discount strategies.

Key Component of Strategy Method:

Step-by-step Implementation of the above Problem:

Strategy Interface: DiscountStrategy is an abstract class with a method calculateDiscount(orderTotal) that needs to be implemented by concrete discount strategies.






// Strategy Interface
class DiscountStrategy {
  calculateDiscount(orderTotal) {
    // To be implemented by concrete strategies
  }
}

Concrete Strategies: There are three concrete discount strategies implemented in the code:




// Concrete Strategies
class NoDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return 0;
  }
}
class TenPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.1;
  }
}
 
class TwentyPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.2;
  }
}
 
  

Context: The ShoppingCart class represents the shopping cart and takes a discount strategy as a parameter during initialization. It allows items to be added to the cart and calculates the total order price, taking into account the selected discount strategy.




class ShoppingCart {
  constructor(discountStrategy) {
    this.discountStrategy = discountStrategy;
    this.items = [];
  }
 
  addItem(item) {
    this.items.push(item);
  }
 
  calculateTotal() {
    const orderTotal = this.items.reduce((total, item) => total + item.price, 0);
    return orderTotal - this.discountStrategy.calculateDiscount(orderTotal);
  }
}

Example Usage: Three different shopping carts (cart1, cart2, and cart3) are created with different discount strategies and items. The code calculates and prints the total price for each cart after applying the selected discount strategy.




// Example usage
const noDiscount = new NoDiscount();
const tenPercentDiscount = new TenPercentDiscount();
const twentyPercentDiscount = new TwentyPercentDiscount();
 
const cart1 = new ShoppingCart(noDiscount);
cart1.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 1 Total:', cart1.calculateTotal());
 
const cart2 = new ShoppingCart(tenPercentDiscount);
cart2.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 2 Total:', cart2.calculateTotal());
 
const cart3 = new ShoppingCart(twentyPercentDiscount);
cart3.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 3 Total:', cart3.calculateTotal());

Code Implementation of the above Problem:




// Strategy Interface
class DiscountStrategy {
  calculateDiscount(orderTotal) {
    // To be implemented by concrete strategies
  }
}
 
// Concrete Strategies
class NoDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return 0;
  }
}
 
class TenPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.1;
  }
}
 
class TwentyPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.2;
  }
}
 
// Context
class ShoppingCart {
  constructor(discountStrategy) {
    this.discountStrategy = discountStrategy;
    this.items = [];
  }
 
  addItem(item) {
    this.items.push(item);
  }
 
  calculateTotal() {
    const orderTotal = this.items.reduce((total, item) => total + item.price, 0);
    return orderTotal - this.discountStrategy.calculateDiscount(orderTotal);
  }
}
 
// Example usage
const noDiscount = new NoDiscount();
const tenPercentDiscount = new TenPercentDiscount();
const twentyPercentDiscount = new TwentyPercentDiscount();
 
const cart1 = new ShoppingCart(noDiscount);
cart1.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 1 Total:', cart1.calculateTotal());
 
const cart2 = new ShoppingCart(tenPercentDiscount);
cart2.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 2 Total:', cart2.calculateTotal());
 
const cart3 = new ShoppingCart(twentyPercentDiscount);
cart3.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 3 Total:', cart3.calculateTotal());

Output
Cart 1 Total: 50
Cart 2 Total: 45
Cart 3 Total: 40




Diagrammatic explanation of the Above Example

Lets understand the Diagram

  1. Context: This represents the class that contains a reference to the Strategy interface. It is responsible for configuring and using the strategies. The “Context” class has a private instance variable called “strategy” that refers to the Strategy interface. It also contains a method “execute()” which delegates the work to the strategy object.
  2. Strategy: This is an interface or an abstract class that defines the method “algorithm()”. It represents the common interface for all supported algorithms. Concrete strategy classes implement this interface.
  3. ConcreteStrategy1, ConcreteStrategy2, …: These are the concrete classes that implement the Strategy interface. Each of these concrete classes encapsulates a specific algorithm. For instance, “ConcreteStrategy1” and “ConcreteStrategy2” represent two different algorithms. They both implement the “algorithm()” method defined in the Strategy interface.

The arrows in the diagram illustrate the relationships between the classes.

Overall, this structure allows the “Context” class to utilize different algorithms without being dependent on their specific implementations. It enables the client to switch between different algorithms easily at runtime, promoting flexibility and maintainability.

Use Cases of Strategy Method in JavaScript Design Patterns

Advantages of the Strategy Method in JavaScript Design Patterns

Disadvantages of the Strategy Method in JavaScript Design Patterns

Conclusion

The Strategy Method is a powerful design pattern that facilitates dynamic algorithm selection at runtime, ensuring code flexibility and reusability. By separating algorithms from clients, it encourages clean, maintainable code that aligns with the Open/Closed Principle. Despite its benefits, it’s important to manage the potential complexity and increased class count, which may impact system architecture. Overall, the Strategy Method is valuable for encapsulating various behaviors and enabling algorithm interchangeability without client code modification, making it an effective approach for robust software development.


Article Tags :