1 00:00:07,230 --> 00:00:07,700 Hello. 2 00:00:08,280 --> 00:00:13,590 In this video, we're going to talk about the first design pattern on our list. 3 00:00:13,890 --> 00:00:18,480 The question 38, what is the strategy design pattern? 4 00:00:19,260 --> 00:00:26,670 The strategy design pattern is a pattern that allows us to define a family of algorithms that perform 5 00:00:26,670 --> 00:00:27,540 some tasks. 6 00:00:27,990 --> 00:00:31,380 The concrete strategy can be chosen at one time. 7 00:00:32,160 --> 00:00:36,150 Let's imagine we implement a platform selling video games. 8 00:00:37,140 --> 00:00:44,610 Here is the game type having title price rating release date and is available flag. 9 00:00:45,570 --> 00:00:51,960 And here are the games we currently have in our database at the first version of the platform. 10 00:00:52,170 --> 00:00:55,590 The user can only search for games by title. 11 00:00:56,130 --> 00:01:00,510 By definition, we don't want to show games that are not available. 12 00:01:01,030 --> 00:01:05,430 The code that implements this behavior could look like this. 13 00:01:22,860 --> 00:01:30,990 Right after some time, our platform evolves and we are asked to add some predefined filters to the 14 00:01:30,990 --> 00:01:31,950 search options. 15 00:01:32,370 --> 00:01:39,120 The first one is best games with two terms games with a rating of 95 or more. 16 00:01:47,120 --> 00:01:47,860 All right. 17 00:01:48,350 --> 00:01:54,950 This method is quite similar to the one that we had before, but let's not jump to refactoring just 18 00:01:54,950 --> 00:01:55,440 yet. 19 00:01:55,700 --> 00:02:04,040 We perhaps have better things to do, but soon after we are asked to add other predefined filters games 20 00:02:04,040 --> 00:02:12,050 of this year, showing games released in the current year and best deals finding games with price below 21 00:02:12,080 --> 00:02:13,340 $25. 22 00:02:27,940 --> 00:02:31,330 Well, this starts to look unmanageable. 23 00:02:32,140 --> 00:02:34,840 All those methods are almost identical. 24 00:02:35,050 --> 00:02:39,250 And the code is duplicated before we start refactoring. 25 00:02:39,460 --> 00:02:42,070 Let's see how this code could be used. 26 00:03:12,260 --> 00:03:12,990 All right. 27 00:03:13,460 --> 00:03:20,150 It's high time to introduce the strategy designed button, according to this pattern, we should be 28 00:03:20,150 --> 00:03:27,020 able to define a family of algorithms that can be injected into some other code at runtime. 29 00:03:27,590 --> 00:03:35,480 In our case, the family of algorithms will contain all predicate methods that decide whether a game 30 00:03:35,510 --> 00:03:38,870 should be included in filtered results or not. 31 00:04:24,630 --> 00:04:25,440 All right. 32 00:04:26,130 --> 00:04:30,840 Each strategy is an algorithm and caused in executable code. 33 00:04:31,200 --> 00:04:38,820 In this case, I return it as a funk, but returning it as an object, implementing an interface would 34 00:04:38,820 --> 00:04:42,570 also be valid and in line with this design pattern. 35 00:04:43,050 --> 00:04:48,270 Remember that after all, a funk is like an interface with a single method. 36 00:04:48,870 --> 00:04:57,180 We can now remove the old methods and create a single find by method that will accept a predicate returned 37 00:04:57,270 --> 00:04:59,130 by the Select Strategy method. 38 00:05:11,620 --> 00:05:19,270 Let's understand how it will be used, let's say, for example, that the selected filtering type is 39 00:05:19,270 --> 00:05:27,310 best games, this method will return this predicate a function taking again and returning through. 40 00:05:27,340 --> 00:05:35,620 If the game's rating is above 95, this predicate will be passed to this method, which takes a collection 41 00:05:35,620 --> 00:05:42,280 of games and returns those that are available and for whom the predicate who turns true. 42 00:05:43,000 --> 00:05:46,000 Let's see how it all could be used together. 43 00:06:09,420 --> 00:06:16,950 The selected filtering type is best games, so games with rating above 95 shouldn't be selected. 44 00:06:17,490 --> 00:06:19,770 Let's see if it works as expected. 45 00:06:22,220 --> 00:06:29,630 Seems like it's working because Stardew Valley and God of War both have ratings above 95. 46 00:06:31,340 --> 00:06:38,000 Now, if business requirements turned and will be asked to add another way of filtering, it would be 47 00:06:38,000 --> 00:06:38,840 very simple. 48 00:06:39,690 --> 00:06:45,740 We'll just add another case here and define another strategy of filtering gains. 49 00:06:46,490 --> 00:06:49,340 As you can see, this pattern is quite simple. 50 00:06:49,640 --> 00:06:53,540 You probably used it before, even if you didn't know it. 51 00:06:53,840 --> 00:07:01,610 If you are where you pass some interchangeable code as a parameter, especially a func or various objects 52 00:07:01,610 --> 00:07:06,950 representing a single interface, you are using the strategy design pattern. 53 00:07:07,520 --> 00:07:11,540 Using this pattern allowed us to remove code duplication. 54 00:07:12,380 --> 00:07:17,090 Now we have one clear place where the algorithms are defined. 55 00:07:17,510 --> 00:07:19,130 The generic algorithm. 56 00:07:19,580 --> 00:07:27,760 So the one defined here would also tax if the game is available is now separated from the specific algorithms. 57 00:07:28,280 --> 00:07:29,420 What are we defined here? 58 00:07:29,960 --> 00:07:34,640 This makes the code simpler, and again, it removes duplications. 59 00:07:35,150 --> 00:07:42,530 Let's summarize the strategy design pattern allows us to define a family of algorithms that perform 60 00:07:42,530 --> 00:07:43,460 some tasks. 61 00:07:43,820 --> 00:07:50,510 The concrete strategy can be chosen at runtime if this topic pops up during the interview. 62 00:07:50,720 --> 00:07:56,540 There is a big chance you will be asked What are the benefits of using this tool to redesign pattern? 63 00:07:57,320 --> 00:08:02,510 This pattern helps to reduce code duplications and makes the code cleaner. 64 00:08:02,930 --> 00:08:10,010 It separates the code that needs to be changed often, so the particular strategy from the code that 65 00:08:10,010 --> 00:08:11,840 doesn't change that much. 66 00:08:12,260 --> 00:08:14,930 So the code that uses this strategy? 67 00:08:15,620 --> 00:08:16,370 All right. 68 00:08:16,820 --> 00:08:18,050 Let's eat in this video. 69 00:08:18,560 --> 00:08:21,920 Thank you for your attention and I'll see you in the next one.