From the course: Node.js: Design Patterns

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

The Strategy pattern

The Strategy pattern

- [Instructor] One of the most powerful and most dynamic design patterns is the strategy. The strategy pattern let's you create multiple algorithms for a specific task and then dynamically decide upon which algorithm to use at run time. An example of a common place that the strategy pattern is used is with payment processing. We could build a shopping cart and checkout process that only let's users make purchases via a credit card. In this case the checkout process would always use the same algorithm, the credit card payment. We could also modify the checkout process to implement a strategy. This means that we decouple how to handle payments from the process itself. Now we can add different strategies for handling payments. We could add a credit card strategy and a PayPal strategy and then let the user decide which one to use dynamically at run time. The cool thing about strategies is that we can add new strategies as the…

Contents