1 00:00:00,670 --> 00:00:06,430 Let's take a look at the context object here so we can see the product object with reference to the 2 00:00:06,430 --> 00:00:14,380 product state, which is the interface that will serve as a contract for the state implementation classes. 3 00:00:14,930 --> 00:00:22,090 We can see that inside the constructor we assign the first state as the available state, which is for 4 00:00:22,090 --> 00:00:22,530 sale. 5 00:00:22,540 --> 00:00:22,900 Right. 6 00:00:23,290 --> 00:00:27,340 This would be the first the initial state of of a product inside the store. 7 00:00:27,610 --> 00:00:34,100 And we also have the letters and the setters because we encapsulate this property in a private variable. 8 00:00:34,350 --> 00:00:36,030 Here we are. 9 00:00:36,070 --> 00:00:41,350 Of course, we have some methods for getting to the previous state or to the next state. 10 00:00:41,560 --> 00:00:47,260 And we also have a method to print the current state, which is the one that you see here, which will 11 00:00:47,800 --> 00:00:55,330 have a statement, a system out of control and statement which will display the current state of the 12 00:00:55,330 --> 00:00:55,700 product. 13 00:00:55,740 --> 00:00:59,030 Plus, this is the interface for for the states. 14 00:00:59,050 --> 00:01:00,700 So this will be the contract. 15 00:01:00,700 --> 00:01:02,430 We we have only two methods. 16 00:01:02,440 --> 00:01:03,460 The next and the previous. 17 00:01:03,730 --> 00:01:06,850 Both of them will take a product as an argument. 18 00:01:07,300 --> 00:01:14,320 So let's see the first state here, the available state, so you can see that the next method here will 19 00:01:14,320 --> 00:01:20,680 switch, will set the new state of the product, which will be the product from from its arguments to 20 00:01:20,680 --> 00:01:22,420 the solid state, which is the next one. 21 00:01:22,450 --> 00:01:28,130 So basically, we have two states, this one being the first and the solid state being the second. 22 00:01:28,440 --> 00:01:35,320 So the next method will switch the state to the next one to to the last state, basically. 23 00:01:35,560 --> 00:01:40,120 And the previous state won't do anything, will just output a system. 24 00:01:40,120 --> 00:01:45,370 Although the saying that the previous state, there is no previous state for this, this product, which 25 00:01:45,370 --> 00:01:52,300 is of course understandable, and the souled will have the same problem for the next because we don't 26 00:01:52,300 --> 00:01:57,370 have a next state and for the previous it will switch the previous state. 27 00:01:57,940 --> 00:02:01,320 It was the product to its previous state, which is the available state. 28 00:02:01,690 --> 00:02:03,040 So let's see how this works. 29 00:02:03,070 --> 00:02:10,900 So basically, reinitialize here a new product and we here put some system of different elements to. 30 00:02:12,960 --> 00:02:19,800 To bring some clarity to to this to this example, and here we switch the states to the next step, 31 00:02:19,800 --> 00:02:28,380 we print the new status and we also do another action that is supposed to bring the product to its previous 32 00:02:28,380 --> 00:02:28,670 state. 33 00:02:28,680 --> 00:02:32,770 And then we print the status again, the status, meaning the current state. 34 00:02:32,790 --> 00:02:33,960 So let's run this. 35 00:02:37,120 --> 00:02:42,850 So as you can see, when the smartphone has been sold, which is our product, the product that we use 36 00:02:42,850 --> 00:02:46,500 in our example, the status for the state will be equal to salt. 37 00:02:46,510 --> 00:02:51,550 But when the smartphone will be returned by the customer, the status will switch again to the available 38 00:02:51,550 --> 00:02:51,920 state. 39 00:02:52,750 --> 00:03:00,520 So as you can see, we do not make use of complex case statements or if else, statements. 40 00:03:00,820 --> 00:03:07,510 We just delegate the state of the product to a different class, to a different set of classes, actually, 41 00:03:07,780 --> 00:03:13,990 which will handle all the implementation for that state and all the actions that we need to do in that 42 00:03:13,990 --> 00:03:15,830 specific state for us. 43 00:03:15,880 --> 00:03:16,240 Right. 44 00:03:16,250 --> 00:03:25,060 So we won't need to to create here a lot of code, a lot of complexity, because this will eventually 45 00:03:25,060 --> 00:03:29,710 make the code unreadable and very, very hard to refactor. 46 00:03:31,020 --> 00:03:34,060 All right, so that's it for this party. 47 00:03:34,140 --> 00:03:38,220 Join me in the next tutorial where we are going to discuss about the strategy of putting.