From the course: JavaScript: Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Strategy pattern

Strategy pattern - JavaScript Tutorial

From the course: JavaScript: Patterns

Start my 1-month free trial

Strategy pattern

- [Instructor] The strategy pattern is basically a way to encapsulate different algorithms or functions and then at runtime practically use the same code to run different scenarios. And we've used this approach in some of the code we've written thus far. So let me show you. So basically if you go into the Exercise Files, and either you use 02_03 or the actual files for this video, so 04_06, it's the same thing. So let's go ahead and use that. So I'm going to drag and drop this inside of Visual Studio Code. And you probably recognize this pattern that we've done in video 02_03. So we created a class car with a constructor with doors, engine, and color as arguments, which we prepped with the constructor here. And then we've extended that with the SUV. And then, again, we added the doors, engine, color and then add this wheels of the number of wheels for a SUV. But the strategy pattern is happening right here. So…

Contents