1 00:00:07,200 --> 00:00:07,680 Hello. 2 00:00:08,250 --> 00:00:12,180 In this video, we're going to talk about the topic 41. 3 00:00:12,480 --> 00:00:15,420 What is the decorator design pattern? 4 00:00:15,900 --> 00:00:24,000 The character is a design partner that dynamically adds extra functionality to an existing object without 5 00:00:24,000 --> 00:00:27,990 affecting the behavior of other objects from the same glass. 6 00:00:28,680 --> 00:00:35,460 Let's start with something simple here we have a class that reads information about people from some 7 00:00:35,460 --> 00:00:36,250 data source. 8 00:00:36,630 --> 00:00:39,720 This class is nice, simple and focused. 9 00:00:40,170 --> 00:00:44,700 At some point, we are asked to add an optional feature of logging. 10 00:00:44,710 --> 00:00:49,020 Human elements have been read that's added to this class. 11 00:01:25,540 --> 00:01:29,860 It was such a pretty class, and now it grew large and ugly. 12 00:01:30,310 --> 00:01:31,810 Well, never mind. 13 00:01:32,080 --> 00:01:34,780 At least it does what it's supposed to do. 14 00:01:35,350 --> 00:01:43,300 Soon after that change, we are asked to add one more optional feature to be able to limit data to some 15 00:01:43,300 --> 00:01:44,890 given count of people. 16 00:01:45,250 --> 00:01:46,660 Let's try to add this. 17 00:02:21,010 --> 00:02:22,780 This cult is terrible now. 18 00:02:23,200 --> 00:02:30,440 This class has a big chunks of logic which will or will not be executed depending on the flags. 19 00:02:30,820 --> 00:02:35,320 Its logic, so simple before, is no mercy and complex. 20 00:02:36,010 --> 00:02:39,250 It takes more parameters than it may need. 21 00:02:39,370 --> 00:02:46,210 For example, we want Mick Gallagher if logging is not enabled and we won't need the account to meet 22 00:02:46,360 --> 00:02:53,920 if the meeting is not enabled, it would be a nightmare to test this class as more and more extra features 23 00:02:53,920 --> 00:02:56,410 are required to be added to this class. 24 00:02:56,590 --> 00:03:02,200 It will keep growing, becoming an unmanageable mess that no one wants to work with. 25 00:03:02,800 --> 00:03:06,640 It's time to introduce the decorator design pattern. 26 00:03:07,000 --> 00:03:13,240 This pattern allows adding some behaviour to an object dynamically without touching its gold. 27 00:03:13,540 --> 00:03:18,430 If you know the open closed principle from solid, you know it's a good thing. 28 00:03:18,850 --> 00:03:24,720 It also allows us to keep the single responsibility principle happy first. 29 00:03:24,910 --> 00:03:28,630 That's reverted this class to how it was before changes. 30 00:03:30,060 --> 00:03:32,160 Beautiful in its simplicity. 31 00:03:32,760 --> 00:03:39,960 Now let's add a logging decorator across it will decorate the people, decorate their houses. 32 00:03:39,960 --> 00:03:47,910 The ability of logging, implementing the decorate or design pattern boils down to two steps making 33 00:03:47,910 --> 00:03:56,580 the decorator implement the same interface as the decorated object and making the decorator own an object 34 00:03:56,580 --> 00:03:58,350 implementing this interface. 35 00:03:58,800 --> 00:04:05,970 It will be the creative class itself or another decorator, which allows us to compose many decorators 36 00:04:05,970 --> 00:04:06,660 together. 37 00:04:07,080 --> 00:04:08,640 Let's see this in cold. 38 00:04:54,730 --> 00:05:02,650 As you can see, the decorator owns an object that it wants to decorate, it implements the same interface 39 00:05:03,040 --> 00:05:06,580 in the real method that comes from this interface. 40 00:05:06,850 --> 00:05:13,210 It goes well, whatever implementation is provided, but it adds only to something from itself. 41 00:05:13,510 --> 00:05:15,940 In this case, it writes to a dog. 42 00:05:16,720 --> 00:05:21,370 Remember that the decorated reader doesn't need to be the plain people. 43 00:05:21,370 --> 00:05:22,690 They carry their object. 44 00:05:23,020 --> 00:05:29,830 It can be anything implementing the AI people data reader interface, including unaware decorator. 45 00:05:30,190 --> 00:05:37,480 Of course, at some point, one of the decorators in the structure must own the basic decorated object 46 00:05:37,570 --> 00:05:39,190 of people data reader type. 47 00:05:39,880 --> 00:05:46,240 Now, let's add the decorator that will be limiting the count of returned person objects. 48 00:06:15,990 --> 00:06:16,620 Great. 49 00:06:17,100 --> 00:06:21,210 We can now compose those decorators to our liking. 50 00:06:21,690 --> 00:06:28,800 Let's create an object that reads people data, logs the original count and then limits it. 51 00:07:04,410 --> 00:07:12,390 Here, the real magic happens, each decorator takes an object implementing the AI people data reader 52 00:07:12,390 --> 00:07:17,400 interface as a parameter, but also implements this interface itself. 53 00:07:17,790 --> 00:07:24,810 It means we can pass a decorator as a parameter to other decorator stacking their functionalities. 54 00:07:25,320 --> 00:07:31,920 That's why this final object will be able to both log and limit the count of elements. 55 00:07:33,880 --> 00:07:40,810 Here you can see that the count has been limited and here that originally five elements has been right. 56 00:07:42,210 --> 00:07:49,140 Please be aware that the order of the decorators creation matters if we turn to this cold to this. 57 00:07:55,860 --> 00:08:05,150 The result will be different because the limiting the Reid method was executed before the logging decorators 58 00:08:05,160 --> 00:08:05,940 with method. 59 00:08:06,360 --> 00:08:12,780 From the point of view of the logging decorator, the amount of data will be free, not five as before, 60 00:08:13,140 --> 00:08:18,060 because the data was limited before it was given to the logging decorator. 61 00:08:19,020 --> 00:08:24,710 The features of logging and limiting are optional, but which the decorator pattern? 62 00:08:24,720 --> 00:08:26,820 It's easy to choose what we need. 63 00:08:27,150 --> 00:08:33,780 Let's create a people data reader that only logs some information, but it does not lead me to the count. 64 00:08:47,860 --> 00:08:50,980 Now, in the result, we see all five elements. 65 00:08:51,280 --> 00:08:58,030 In other words, the result was not limited because here we create only the logging the crater. 66 00:08:58,810 --> 00:09:04,630 And now let's create an object that does not log, but it does limited the data. 67 00:09:21,020 --> 00:09:26,690 As you can see, there is no log string in the result, which we have been seeing before. 68 00:09:27,950 --> 00:09:28,700 All right. 69 00:09:28,880 --> 00:09:36,110 As you can see, the decorator pattern allows us to easily art functionality to objects without touching 70 00:09:36,110 --> 00:09:37,490 the original glasses. 71 00:09:37,730 --> 00:09:41,330 So it's very much in line with the open closed principle. 72 00:09:41,780 --> 00:09:46,040 It also helps us to be in line with the single responsibility principle. 73 00:09:46,130 --> 00:09:53,390 As each class now has a very focused responsibility, the reader class only reads the data. 74 00:09:54,990 --> 00:10:02,790 The logging the great or simply adds the logging feature and the limiting the greater the data, those 75 00:10:02,790 --> 00:10:08,250 gases would be easy to test, maintain and generally pleasant to work with. 76 00:10:09,030 --> 00:10:15,510 During the interview, you can be asked What are the benefits of using the decorator or design pattern? 77 00:10:16,050 --> 00:10:22,590 The decorator pattern allows us to easily out functionality to objects without touching the original 78 00:10:22,590 --> 00:10:23,310 glasses. 79 00:10:23,700 --> 00:10:25,890 It allows us to keep glasses simple. 80 00:10:26,190 --> 00:10:32,970 It makes it easy to tuck functionalities together, building complex objects from simple glasses. 81 00:10:33,480 --> 00:10:39,600 It helps us to be in line with both open closed principle and single responsibility principle. 82 00:10:40,110 --> 00:10:46,110 The glasses extended with the decorating design pattern would be easy to test and maintain. 83 00:10:46,770 --> 00:10:47,550 Oh right. 84 00:10:47,970 --> 00:10:49,740 I hope you liked this pattern. 85 00:10:50,190 --> 00:10:53,640 Thank you for your attention, and I'll see you in the next video.