1 00:00:00,690 --> 00:00:07,200 As usual, we are going to start with the abstraction, so here is our interface, our strategy interface, 2 00:00:07,200 --> 00:00:14,450 which essentially has only one method DU operation, and it takes two parameters of type integer. 3 00:00:14,700 --> 00:00:23,020 So you will see how we can modify at runtime the operation, the algorithm that is behind this operation. 4 00:00:23,340 --> 00:00:29,470 So let's take a look first at the ad operation class that implements this strategy. 5 00:00:29,760 --> 00:00:34,160 So basically this will have the addition operation behind. 6 00:00:34,200 --> 00:00:43,290 So basically it will add the two numbers that are given as parameters to this method and multiply performance 7 00:00:43,320 --> 00:00:44,090 multiplication. 8 00:00:44,100 --> 00:00:47,920 And also we have substraction which performs substraction, of course. 9 00:00:48,330 --> 00:00:57,210 So also, the next object that we are going to look into is this context object, which contains a strategy, 10 00:00:57,420 --> 00:00:59,270 a reference to a strategy object. 11 00:00:59,670 --> 00:01:03,960 And as you can see, we initialize it using this strategy. 12 00:01:03,960 --> 00:01:11,760 We give in this constructor a strategy object and this strategy will be will be assigned this variable 13 00:01:11,760 --> 00:01:19,800 over here, the execute strategy method will basically invoke that the operation method for this specific 14 00:01:19,800 --> 00:01:20,330 strategy. 15 00:01:20,580 --> 00:01:27,150 And as you can see, we make use of these two parameters, which will be further path to this to this 16 00:01:27,150 --> 00:01:28,010 method over here. 17 00:01:28,770 --> 00:01:32,640 And let's take a look at the app class and the main method here. 18 00:01:32,670 --> 00:01:35,370 So basically we initialize to integers. 19 00:01:35,790 --> 00:01:43,760 And as you can see here, we need to create a context object, which we need to initialize it with a 20 00:01:43,770 --> 00:01:46,430 specific implementation of this strategy. 21 00:01:46,860 --> 00:01:54,270 And here we called execute strategy method that will add in our case. 22 00:01:54,270 --> 00:01:57,390 In the first case, it will add the two numbers together. 23 00:01:58,770 --> 00:02:04,690 In the second example, we use the same variable and we initialize it with a multiply operation. 24 00:02:04,980 --> 00:02:11,360 So this essentially, when we call execute strategy, will multiply these two numbers over here. 25 00:02:11,370 --> 00:02:13,720 So let's run this and see this in action. 26 00:02:14,310 --> 00:02:19,410 So as you can see, we get 12, which is the result of this, which is correct. 27 00:02:19,410 --> 00:02:22,530 And 20, which is the result of the second operation. 28 00:02:22,890 --> 00:02:29,300 So this is what I mean when I say that we change the algorithm at runtime. 29 00:02:30,030 --> 00:02:36,930 So basically at runtime we can change the algorithm for this execute strategy as we want. 30 00:02:37,140 --> 00:02:45,240 And also it is very easy to add other strategies we don't have to modify and we don't have to change 31 00:02:45,540 --> 00:02:46,590 the existing code. 32 00:02:46,740 --> 00:02:53,310 We just add another class, another concrete class that will implement the strategy and we will perform 33 00:02:53,310 --> 00:02:54,370 our operation here. 34 00:02:54,390 --> 00:02:59,640 We will write whatever algorithm we want inside this new operation method. 35 00:03:00,690 --> 00:03:02,560 That's it for this story. 36 00:03:02,580 --> 00:03:07,860 You'll join me in the next one when we are going to discuss about the template method design pattern.