From the course: C++ Design Patterns: Behavioral

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Creating strategies

Creating strategies

- [Narrator] All right, so now that we've learned the basics of the strategy pattern, let's take a look at an example of what it might look like in code. Now previously, we discussed the example of using the strategy pattern to choose between different sorting algorithms and that's absolutely possible to implement. But for our example here, we're going to do something just a little bit simpler. We're going to use the strategy pattern to allow us to specify different greetings for people in our program. So let me show you what I mean by that. If you look at the starter code for this video, which we have here, and you can find it in the exercise files, you'll see the situation that the strategy pattern is meant to help us with. So specifically we have this person interface with a single greet method. And then what we've had to do is define a new subclass of this interface for each different person that greets…

Contents