1 00:00:07,230 --> 00:00:07,570 Hello. 2 00:00:08,220 --> 00:00:14,610 In this lecture, we're going to talk about Question 40, what is the template method design pattern? 3 00:00:15,060 --> 00:00:21,240 Template method is a design pattern that defines the skeleton of an algorithm in the base class. 4 00:00:21,600 --> 00:00:26,220 Specific steps of this algorithm are implemented in draft classes. 5 00:00:26,640 --> 00:00:33,870 Let's consider the following example We are developing a platform that allows users to play board games 6 00:00:33,870 --> 00:00:34,530 online. 7 00:00:35,100 --> 00:00:38,760 The first board game we deliver is suckers of cartoon. 8 00:00:39,240 --> 00:00:42,480 He was the slightly simplified implementation. 9 00:00:43,620 --> 00:00:47,260 He was the main method in this class to play this game. 10 00:00:47,280 --> 00:00:49,320 We must first set a bird. 11 00:00:49,590 --> 00:00:52,980 Then we play turns until the game is finished. 12 00:00:53,520 --> 00:00:55,980 Finally, we must select the winner. 13 00:00:56,850 --> 00:00:58,440 So let's see a game. 14 00:01:00,640 --> 00:01:01,330 All right. 15 00:01:01,870 --> 00:01:05,070 In this case, the game was finished in six turns. 16 00:01:07,110 --> 00:01:13,410 Soon after, we are asked to implement another game, this time it's terraforming Mars. 17 00:01:35,560 --> 00:01:35,820 Huh. 18 00:01:36,460 --> 00:01:43,150 It seems this game is quite similar to the code we've had before after implementing a couple more board 19 00:01:43,150 --> 00:01:43,630 games. 20 00:01:43,860 --> 00:01:45,490 What come to our revelation? 21 00:01:45,880 --> 00:01:48,880 All board games follow a similar template. 22 00:01:49,330 --> 00:01:56,140 We first set up the board, then we play turns until the game is finished and finally we select the 23 00:01:56,140 --> 00:02:00,370 winner instead of repeating this logic in each class. 24 00:02:00,550 --> 00:02:07,570 We could define it once in the base class and ask the subclasses to only provide the details of the 25 00:02:07,570 --> 00:02:09,580 implementation of each step. 26 00:02:10,090 --> 00:02:15,970 This way, if the template changes for some reason, we only have one place to fix. 27 00:02:16,420 --> 00:02:19,540 We'll be using the template method cheese and butter. 28 00:02:20,080 --> 00:02:23,020 First, let's define the template itself. 29 00:02:23,530 --> 00:02:26,290 It will be done using an abstract class. 30 00:02:54,770 --> 00:02:59,150 Here we defined the template, but we made all those methods abstract. 31 00:02:59,510 --> 00:03:02,600 It will be up to concrete classes to implement them. 32 00:03:03,260 --> 00:03:07,490 Let's make the games we defined derive from this abstract class. 33 00:03:36,590 --> 00:03:43,670 Now, the feeling that those cases had in common, so the general template of it game is engrossed in 34 00:03:43,670 --> 00:03:49,640 the base type, if this template changes will only need to adjust this base across. 35 00:03:51,620 --> 00:03:58,160 The derived classes only define what makes it boardgame special, and they don't replicate what they 36 00:03:58,160 --> 00:03:59,360 have in common. 37 00:04:00,140 --> 00:04:06,830 The template method design pattern is useful everywhere where some base algorithm is needed, but the 38 00:04:06,830 --> 00:04:14,480 specific parts of it, very a practical example, could be the execution flow of tests in some units 39 00:04:14,480 --> 00:04:15,440 test framework. 40 00:04:15,950 --> 00:04:20,930 Typically, such execution looks like this for each test. 41 00:04:21,110 --> 00:04:28,100 We run the setup method, then we execute the test and then we run the teardown method. 42 00:04:28,730 --> 00:04:32,720 This could easily be achieved with the template method, the design pattern. 43 00:04:33,260 --> 00:04:37,610 First, let's define the base class for all test fixtures. 44 00:05:23,660 --> 00:05:24,300 All right. 45 00:05:24,950 --> 00:05:32,210 So here is the template of the test picture for each test that should be wrong, it first runs the setup 46 00:05:32,210 --> 00:05:37,800 method, then it executes the test and raises the counter of failed tests. 47 00:05:37,820 --> 00:05:39,620 If the test was not successful. 48 00:05:39,890 --> 00:05:42,740 And finally, it runs the cool down method. 49 00:05:43,310 --> 00:05:48,500 The final result of the execution is true only if the count of three tests. 50 00:05:48,710 --> 00:05:49,430 This is zero. 51 00:05:49,970 --> 00:05:56,900 It's up to the draft classes to define what this exactly should be wrong and what happens in the setup 52 00:05:56,900 --> 00:05:58,310 and teardown methods. 53 00:05:58,940 --> 00:06:01,970 So let's define some actual tests now. 54 00:06:02,330 --> 00:06:06,920 First, let's see the class will test it here. 55 00:06:07,310 --> 00:06:09,830 As you can see, it's not very complicated. 56 00:06:10,430 --> 00:06:12,300 So the test fixture. 57 00:06:12,320 --> 00:06:16,910 Testing this class will be derived from the base, the structure across. 58 00:07:09,130 --> 00:07:09,820 All right. 59 00:07:10,360 --> 00:07:16,720 Here we defined two simple tests for the calculators admitted in the setup method. 60 00:07:16,810 --> 00:07:19,840 We simply create the class under test object. 61 00:07:20,350 --> 00:07:23,590 And in the teardown, we just bring something to the council. 62 00:07:24,220 --> 00:07:26,680 Let's see if those tests will pass. 63 00:07:39,010 --> 00:07:41,380 Great for both us. 64 00:07:41,560 --> 00:07:49,390 First, the setup method was executed, then the test itself and then the teardown method thanks to 65 00:07:49,390 --> 00:07:50,530 the template method. 66 00:07:50,620 --> 00:07:55,420 We found this common behavior in the base class and in the derived class. 67 00:07:55,420 --> 00:07:57,490 We could focus on what's important. 68 00:07:57,730 --> 00:07:59,650 So on the tests themselves. 69 00:08:01,530 --> 00:08:07,680 As you can see, the template method design pattern can be quite handy everywhere, where a generic 70 00:08:07,680 --> 00:08:09,870 algorithm shall be defined once. 71 00:08:10,140 --> 00:08:17,900 But the implementations of the particular steps of this algorithm may vary when discussing design patterns 72 00:08:17,910 --> 00:08:19,110 during the interview. 73 00:08:19,200 --> 00:08:25,950 You can be asked What is the difference between the template method design pattern and the strategy 74 00:08:25,950 --> 00:08:26,880 design pattern? 75 00:08:27,480 --> 00:08:34,560 Both patterns allow specifying what concrete algorithm or a piece of the algorithm will be used. 76 00:08:34,950 --> 00:08:41,970 The main difference is that with the template method is selected at compile time as this pattern uses 77 00:08:41,970 --> 00:08:44,700 inheritance with the strategy pattern. 78 00:08:45,060 --> 00:08:49,500 The decision is made at runtime as this pattern uses composition. 79 00:08:50,370 --> 00:08:51,090 All right. 80 00:08:51,330 --> 00:08:54,180 We learned about yet another design pattern. 81 00:08:54,720 --> 00:08:57,510 Thanks for watching and see you in the next lecture.