1 00:00:01,180 --> 00:00:04,490 Let's start by looking at the machine interface here. 2 00:00:04,510 --> 00:00:09,040 So this is the component that we see in our diagram. 3 00:00:09,910 --> 00:00:15,820 It has only one function that one and only one method that will return the street to us. 4 00:00:15,820 --> 00:00:20,570 And here is the implementation of the machine. 5 00:00:21,250 --> 00:00:27,770 I decided to go it with colors because it's a good example for the decorator partner. 6 00:00:27,790 --> 00:00:32,710 You can decorate a car with several functionality, several several features. 7 00:00:32,980 --> 00:00:37,340 So it's easier to understand using this example. 8 00:00:37,360 --> 00:00:43,850 So we just invented this operation method and we only risky work car. 9 00:00:43,900 --> 00:00:44,500 You will see. 10 00:00:44,500 --> 00:00:45,360 You will see why. 11 00:00:46,150 --> 00:00:53,530 And on the other side, we have the decorator, the car decorator abstraction in the form of an abstract 12 00:00:53,530 --> 00:00:57,600 class which implements the same machine interface. 13 00:00:57,970 --> 00:01:00,050 We have a machine object here. 14 00:01:00,070 --> 00:01:09,820 This doesn't need to be encapsulated, meaning that it doesn't have to have private access modifier 15 00:01:10,390 --> 00:01:16,140 because you will need this object in the concrete decorator's that we will implement. 16 00:01:16,480 --> 00:01:20,050 And here we have the constructor. 17 00:01:20,710 --> 00:01:30,460 We will take a machine as an argument and we set the machine, the property of this decorator with this 18 00:01:30,880 --> 00:01:34,090 machine argument and the operation will return. 19 00:01:34,390 --> 00:01:41,050 What the machine wants to do, operation implementation of the machine will return. 20 00:01:41,440 --> 00:01:43,610 So this is very straightforward. 21 00:01:45,250 --> 00:01:51,040 This these are these two of the navigation and the parking assistant are the concrete decorators that 22 00:01:51,040 --> 00:01:51,460 we have. 23 00:01:52,240 --> 00:01:57,130 So as you can see, it extends the navigation, extends the car decorator class. 24 00:01:58,180 --> 00:02:07,630 The constructor is basically the call to the super method, the super constructor and the operation 25 00:02:07,630 --> 00:02:15,050 will return the result of the machine, the operation, but also plus navigation. 26 00:02:15,070 --> 00:02:21,670 So basically, we are adding a new feature to the to the car, which is to our base object. 27 00:02:22,690 --> 00:02:25,750 And the same applies for the parking system as well. 28 00:02:25,840 --> 00:02:32,200 So as you can see, the same code, but instead of adding navigation, we are the parking system and 29 00:02:32,470 --> 00:02:36,770 the class which contains our main method is looking like this. 30 00:02:36,790 --> 00:02:43,210 We basically create the new machine object with the following implementation. 31 00:02:43,240 --> 00:02:49,300 So basically we are adding all the decorators in order to create a car, which parking assistant and 32 00:02:49,300 --> 00:02:49,870 navigation. 33 00:02:50,380 --> 00:02:57,000 So this the system, although through the lens, should output what the machine looks like, what the 34 00:02:57,010 --> 00:02:58,820 what are the features that the machine has. 35 00:02:59,200 --> 00:03:01,000 So let's run this. 36 00:03:03,240 --> 00:03:10,440 So as you can see, we have the base class near the base object, which is a car, and we add the parking 37 00:03:10,440 --> 00:03:13,870 incident, the parking assistant and a navigation features to it. 38 00:03:14,280 --> 00:03:17,460 So basically, this is the stuff that's about it. 39 00:03:18,330 --> 00:03:22,610 This is how you create this is how you implement the decorator pattern in your code. 40 00:03:23,580 --> 00:03:31,230 It's always helpful to have this pattern whenever you want to decorate an object with additional additional 41 00:03:31,230 --> 00:03:32,430 stuff, additional features. 42 00:03:32,440 --> 00:03:32,630 Right. 43 00:03:33,120 --> 00:03:36,510 So in this way, you won't affect your base object. 44 00:03:36,510 --> 00:03:38,430 You won't affect the structure of the base object. 45 00:03:39,090 --> 00:03:45,570 But also you can add at the same time, you can add new functionalities, new features, and each class, 46 00:03:45,570 --> 00:03:50,050 each class that you add will be responsible of each feature. 47 00:03:50,070 --> 00:03:52,520 So it's actually very, very straightforward. 48 00:03:52,710 --> 00:03:57,840 But like I said in the previous video, the downside is that you will have a lot of classes. 49 00:03:57,840 --> 00:04:04,650 If you have a complex object, like a car, for example, you will have a lot of objects that a lot 50 00:04:04,650 --> 00:04:08,970 of classes that you you need to create for each feature because a car has a lot of features. 51 00:04:09,140 --> 00:04:16,080 So if you want to be granular about it, then you will have to add you have to transpose each feature 52 00:04:16,080 --> 00:04:22,110 of a car into a class which will increase the volume of your code. 53 00:04:22,950 --> 00:04:25,690 That's it about the decorator pattern. 54 00:04:25,710 --> 00:04:30,180 Join me in the next story of where we are going to discuss about the facade pattern. 55 00:04:30,420 --> 00:04:31,900 So, so and I story.