1 00:00:01,090 --> 00:00:05,890 As you may already know from my previous tutorials, it is better to start with the abstraction first. 2 00:00:05,890 --> 00:00:13,060 So let's see this observer interface, which has only one method that updates the observers and doesn't 3 00:00:13,060 --> 00:00:13,880 return anything. 4 00:00:14,080 --> 00:00:22,230 And these two are the classes that concrete observers that implement this interface to store observer 5 00:00:22,240 --> 00:00:24,180 and the supply chain observer. 6 00:00:24,430 --> 00:00:28,000 But before diving into the observers. 7 00:00:28,180 --> 00:00:29,830 Let's take a look at the product first. 8 00:00:29,850 --> 00:00:36,650 So basically here we have a program that is concentrated around the product subject here. 9 00:00:36,850 --> 00:00:42,330 So whenever the product will change it state, so, for example, a product will be sold, the both 10 00:00:42,350 --> 00:00:48,000 of the observers will be notified and the bolt will trigger some some actions on their behalf. 11 00:00:48,250 --> 00:00:53,530 So let's see this product subject first before taking a look in the observers. 12 00:00:53,800 --> 00:01:02,890 So basically, the product subject will have will contain a list of observers and also a state that 13 00:01:02,890 --> 00:01:04,670 is under the form of a string. 14 00:01:04,990 --> 00:01:07,510 So we have a gathering center for the state. 15 00:01:07,510 --> 00:01:14,090 But as you can see, the center here will also call this method notify all observers. 16 00:01:14,320 --> 00:01:21,130 This is because whenever, like we said, whenever a product will change at state, we need to notify 17 00:01:21,130 --> 00:01:23,520 the first of the of the state change. 18 00:01:23,920 --> 00:01:31,420 Here we have an attached observer method so that we can attach multiple observers to the same product, 19 00:01:32,200 --> 00:01:37,780 to the same object and then notify all observers will basically go through this list, this observer 20 00:01:37,780 --> 00:01:42,370 observers, and it will call the update method for all the observers. 21 00:01:42,640 --> 00:01:48,310 And now let's see the observers and we can see that they saw reference to the product subject. 22 00:01:49,000 --> 00:01:57,660 And within the constructor, the product subject is initialized and is assigned to the value of of its 23 00:01:57,670 --> 00:01:57,960 type. 24 00:01:58,290 --> 00:02:06,790 And as you can see, also the attach observer will attach this product, this object to to the product 25 00:02:06,790 --> 00:02:07,200 subject. 26 00:02:07,210 --> 00:02:09,790 So you can see that this is very clear. 27 00:02:10,360 --> 00:02:15,860 Whenever an observer is created, we need to attach it to to the subject. 28 00:02:16,330 --> 00:02:20,700 So the object method will just contain a system of the printing. 29 00:02:20,700 --> 00:02:24,420 And this is applicable for the supply chain observer as well. 30 00:02:24,640 --> 00:02:33,250 And as you can see, the system now on will output a different message so that it is clear that both 31 00:02:33,250 --> 00:02:41,220 of them were called and the update works well whenever the product will suffer a change in its state. 32 00:02:41,530 --> 00:02:44,000 So let's see the application, the main method here. 33 00:02:44,020 --> 00:02:46,690 So basically we create a new product subject. 34 00:02:46,690 --> 00:02:49,840 So supposedly we have a computer store. 35 00:02:50,020 --> 00:02:52,260 So we create the new computer here. 36 00:02:52,720 --> 00:03:00,220 And as you can see, we initialize the store observer first with this parameter, the computer parameter. 37 00:03:00,490 --> 00:03:05,050 And here we do some changes in the computer state. 38 00:03:05,260 --> 00:03:12,070 So we we call we basically call the center with new strengths, with new states, new sold. 39 00:03:12,370 --> 00:03:17,050 And after that, we need to also employ the supply chain observer. 40 00:03:17,320 --> 00:03:21,580 And we basically created based on the same product. 41 00:03:21,580 --> 00:03:30,820 And then we change the state again to order basically a new product, the product that has been already 42 00:03:30,820 --> 00:03:31,270 sold. 43 00:03:31,300 --> 00:03:36,300 So basically, we need to reorder it just to have it in our store again. 44 00:03:36,490 --> 00:03:38,120 So let's see this in action. 45 00:03:38,140 --> 00:03:41,590 Let's start by running this main method. 46 00:03:42,720 --> 00:03:51,750 So as you can see, the sole observer is updated, so the the first state here is printed then for the 47 00:03:51,750 --> 00:03:58,350 second update, whenever the computer has been sold, the sole observer will print this information, 48 00:03:58,350 --> 00:03:59,570 will print the new state. 49 00:03:59,910 --> 00:04:04,880 And finally, it will take the final stand statement here. 50 00:04:05,190 --> 00:04:12,000 As you can see, also, the supply chain observer will print it state because this one is particularly 51 00:04:12,000 --> 00:04:17,060 interested to trigger an event after the computer has been sold. 52 00:04:17,220 --> 00:04:19,680 So that's a new computer will be ordered. 53 00:04:19,710 --> 00:04:24,750 Again, this is this is it for for this tutorial. 54 00:04:25,110 --> 00:04:31,660 So you may view this this pattern as having one to many relationship between your objects. 55 00:04:31,880 --> 00:04:35,240 So basically, we have a single product and we have multiple observers. 56 00:04:35,430 --> 00:04:41,170 So whenever the product will change the state, the observers will trigger some action. 57 00:04:41,190 --> 00:04:47,580 So actually, it is pretty straightforward, but it would be very useful for you to download this code 58 00:04:47,820 --> 00:04:54,180 from from the video page, from the attachments section, and you can start playing with it so that 59 00:04:54,180 --> 00:04:59,430 you can get a firm grasp on on what this observer pattern is about. 60 00:05:00,450 --> 00:05:02,520 So that's it for this tutorial. 61 00:05:02,520 --> 00:05:05,850 Joining the next one when we are going to discuss about the state paper.