Hello.
In this video, we're going to talk about the first design pattern on our list.
The question 38, what is the strategy design pattern?
The strategy design pattern is a pattern that allows us to define a family of algorithms that perform
some tasks.
The concrete strategy can be chosen at one time.
Let's imagine we implement a platform selling video games.
Here is the game type having title price rating release date and is available flag.
And here are the games we currently have in our database at the first version of the platform.
The user can only search for games by title.
By definition, we don't want to show games that are not available.
The code that implements this behavior could look like this.
Right after some time, our platform evolves and we are asked to add some predefined filters to the
search options.
The first one is best games with two terms games with a rating of 95 or more.
All right.
This method is quite similar to the one that we had before, but let's not jump to refactoring just
yet.
We perhaps have better things to do, but soon after we are asked to add other predefined filters games
of this year, showing games released in the current year and best deals finding games with price below
$25.
Well, this starts to look unmanageable.
All those methods are almost identical.
And the code is duplicated before we start refactoring.
Let's see how this code could be used.
All right.
It's high time to introduce the strategy designed button, according to this pattern, we should be
able to define a family of algorithms that can be injected into some other code at runtime.
In our case, the family of algorithms will contain all predicate methods that decide whether a game
should be included in filtered results or not.
All right.
Each strategy is an algorithm and caused in executable code.
In this case, I return it as a funk, but returning it as an object, implementing an interface would
also be valid and in line with this design pattern.
Remember that after all, a funk is like an interface with a single method.
We can now remove the old methods and create a single find by method that will accept a predicate returned
by the Select Strategy method.
Let's understand how it will be used, let's say, for example, that the selected filtering type is
best games, this method will return this predicate a function taking again and returning through.
If the game's rating is above 95, this predicate will be passed to this method, which takes a collection
of games and returns those that are available and for whom the predicate who turns true.
Let's see how it all could be used together.
The selected filtering type is best games, so games with rating above 95 shouldn't be selected.
Let's see if it works as expected.
Seems like it's working because Stardew Valley and God of War both have ratings above 95.
Now, if business requirements turned and will be asked to add another way of filtering, it would be
very simple.
We'll just add another case here and define another strategy of filtering gains.
As you can see, this pattern is quite simple.
You probably used it before, even if you didn't know it.
If you are where you pass some interchangeable code as a parameter, especially a func or various objects
representing a single interface, you are using the strategy design pattern.
Using this pattern allowed us to remove code duplication.
Now we have one clear place where the algorithms are defined.
The generic algorithm.
So the one defined here would also tax if the game is available is now separated from the specific algorithms.
What are we defined here?
This makes the code simpler, and again, it removes duplications.
Let's summarize the strategy design pattern allows us to define a family of algorithms that perform
some tasks.
The concrete strategy can be chosen at runtime if this topic pops up during the interview.
There is a big chance you will be asked What are the benefits of using this tool to redesign pattern?
This pattern helps to reduce code duplications and makes the code cleaner.
It separates the code that needs to be changed often, so the particular strategy from the code that
doesn't change that much.
So the code that uses this strategy?
All right.
Let's eat in this video.
Thank you for your attention and I'll see you in the next one.