JavaScript Design Patterns – Behavioral – Strategy

The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.

In this example, We have a set of discounts that can be applied to a shopping cart. We can pass the function that we will apply to the construc…


This content originally appeared on DEV Community and was authored by Nhan Nguyen

The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.

In this example, We have a set of discounts that can be applied to a shopping cart. We can pass the function that we will apply to the constructor and, in that way, change the amount discounted.

class ShoppingCart {
  constructor(discount) {
    this.discount = discount;
    this.amount = 0;
  }

  checkout() {
    return this.discount(this.amount);
  }

  setAmount(amount) {
    this.amount = amount;
  }
}

function guest(amount) {
  return amount;
}

function regular(amount) {
  return amount * 0.9;
}

function premium(amount) {
  return amount * 0.8;
}

export { ShoppingCart, guest, regular, premium };

A complete example is here 👉 https://stackblitz.com/edit/vitejs-vite-tygwh3?file=strategy.js

Conclusion

Use this pattern when you have a lot of similar classes that only differ in how they execute some behavior.

I hope you found it helpful. Thanks for reading. 🙏

Let's get connected! You can find me on:


This content originally appeared on DEV Community and was authored by Nhan Nguyen


Print Share Comment Cite Upload Translate Updates
APA

Nhan Nguyen | Sciencx (2024-08-16T09:23:40+00:00) JavaScript Design Patterns – Behavioral – Strategy. Retrieved from https://www.scien.cx/2024/08/16/javascript-design-patterns-behavioral-strategy/

MLA
" » JavaScript Design Patterns – Behavioral – Strategy." Nhan Nguyen | Sciencx - Friday August 16, 2024, https://www.scien.cx/2024/08/16/javascript-design-patterns-behavioral-strategy/
HARVARD
Nhan Nguyen | Sciencx Friday August 16, 2024 » JavaScript Design Patterns – Behavioral – Strategy., viewed ,<https://www.scien.cx/2024/08/16/javascript-design-patterns-behavioral-strategy/>
VANCOUVER
Nhan Nguyen | Sciencx - » JavaScript Design Patterns – Behavioral – Strategy. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/16/javascript-design-patterns-behavioral-strategy/
CHICAGO
" » JavaScript Design Patterns – Behavioral – Strategy." Nhan Nguyen | Sciencx - Accessed . https://www.scien.cx/2024/08/16/javascript-design-patterns-behavioral-strategy/
IEEE
" » JavaScript Design Patterns – Behavioral – Strategy." Nhan Nguyen | Sciencx [Online]. Available: https://www.scien.cx/2024/08/16/javascript-design-patterns-behavioral-strategy/. [Accessed: ]
rf:citation
» JavaScript Design Patterns – Behavioral – Strategy | Nhan Nguyen | Sciencx | https://www.scien.cx/2024/08/16/javascript-design-patterns-behavioral-strategy/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.