1 00:00:00,310 --> 00:00:03,240 So design patterns seem pretty great, right? 2 00:00:03,240 --> 00:00:05,510 Well, I got some bad news for you. 3 00:00:05,510 --> 00:00:07,560 Design patterns are not a magic bullet. 4 00:00:07,560 --> 00:00:10,090 They aren't going to solve all of your coding issues. 5 00:00:10,090 --> 00:00:13,930 In fact, you shouldn't ever really start with a pattern you should reach for 6 00:00:13,930 --> 00:00:16,839 them only when you need them never before you start coding. 7 00:00:17,880 --> 00:00:21,830 Most languages these days have some sort of package manager that allows you to use 8 00:00:21,830 --> 00:00:22,990 packaged up code. 9 00:00:22,990 --> 00:00:27,140 Now whether those packages are called modules, libraries, pods, Gems, JARs, 10 00:00:27,140 --> 00:00:28,440 NuGet whatever. 11 00:00:28,440 --> 00:00:31,280 They represent code that is available for you to use so 12 00:00:31,280 --> 00:00:35,390 that you don't need to recreate the wheel Design patterns are a lot like this. 13 00:00:35,390 --> 00:00:37,840 Except instead of viewing them as code reuse 14 00:00:37,840 --> 00:00:40,740 you should view them more like experience reuse. 15 00:00:40,740 --> 00:00:43,050 People have been there and they've done that, so 16 00:00:43,050 --> 00:00:45,360 you should lean on their experience. 17 00:00:45,360 --> 00:00:47,570 Again, for clarity's sake, 18 00:00:47,570 --> 00:00:51,210 don't go grabbing from the experience repository right out of the gate. 19 00:00:51,210 --> 00:00:53,740 Always go for the simplest solution first and 20 00:00:53,740 --> 00:00:56,590 introduce the pattern only when the need arises. 21 00:00:56,590 --> 00:01:01,880 The great acronym that we should all live by goes well with this, K-I-S-S. 22 00:01:01,880 --> 00:01:06,770 Kiss, Keep It Simple Smarty pants, or something more rude than that. 23 00:01:08,080 --> 00:01:11,990 So then you ask, should I even bother learning about them if I can't use them? 24 00:01:11,990 --> 00:01:14,730 And that's a great question, and I'm glad you asked. 25 00:01:14,730 --> 00:01:18,120 The simple answer is this, you need to be familiar with them. 26 00:01:19,130 --> 00:01:23,090 There are two major reasons I want you to get familiar with design patterns. 27 00:01:23,090 --> 00:01:24,700 Well, look at both of them in more depth here. 28 00:01:25,830 --> 00:01:30,110 So the most amazing benefit of design patterns is that they create a shared 29 00:01:30,110 --> 00:01:31,680 vocabulary. 30 00:01:31,680 --> 00:01:34,600 Since these patterns are named they can be used to communicate 31 00:01:34,600 --> 00:01:38,090 a very specific solution in just a few words. 32 00:01:38,090 --> 00:01:40,870 Now there's a huge difference between these two examples. 33 00:01:42,180 --> 00:01:44,810 You should just kind of make the first class behave like 34 00:01:44,810 --> 00:01:48,310 the other one by making the methods in here the same as the other one, so 35 00:01:48,310 --> 00:01:51,420 that the caller thinks that it's actually doing its thing but 36 00:01:51,420 --> 00:01:53,990 really is calling specific methods on the original object. 37 00:01:55,630 --> 00:01:58,340 And you should use the adapter pattern. 38 00:01:59,410 --> 00:02:03,360 That first statement is pretty much exactly what the adapter pattern does. 39 00:02:03,360 --> 00:02:06,740 The adapter pattern is a lot like one of these power adapters here, 40 00:02:06,740 --> 00:02:08,800 that you would use when you're traveling the world. 41 00:02:08,800 --> 00:02:10,890 There are many different types of electrical outlets. 42 00:02:10,890 --> 00:02:12,770 And this hair dryer that you brought for 43 00:02:12,770 --> 00:02:16,340 home, might not plug into the outlet in the other country. 44 00:02:16,340 --> 00:02:20,450 A power adapter lets you connect your hair dryer to the outlet. 45 00:02:20,450 --> 00:02:23,420 You plug the hair dryer into one side of the adapter and 46 00:02:23,420 --> 00:02:25,910 the other side of the adapter fits into the outlet. 47 00:02:26,950 --> 00:02:28,480 This is your adapter. 48 00:02:28,480 --> 00:02:30,890 The port in the wall is what you're trying to adhere to, 49 00:02:30,890 --> 00:02:33,010 and this is your original class. 50 00:02:33,010 --> 00:02:36,580 By applying the adapter, they work together. 51 00:02:36,580 --> 00:02:39,480 Now, this is the level of familiarity that i suggest 52 00:02:39,480 --> 00:02:41,220 that you need when understanding design patterns. 53 00:02:42,250 --> 00:02:46,340 You don't need to know how exactly to code up the adapter in your specific language 54 00:02:46,340 --> 00:02:50,590 because you've got your catalog of patterns to draw that experience from. 55 00:02:51,650 --> 00:02:54,670 Now take a quick second with me here and try to feel the difference 56 00:02:54,670 --> 00:02:58,250 between these two approaches and the power that the shared vocabulary word brings. 57 00:02:59,350 --> 00:03:03,200 Okay, so I have this device and it has this plug that doesn't fit into the other 58 00:03:03,200 --> 00:03:06,760 outlet right, so the side has like a three standard prongs right and 59 00:03:06,760 --> 00:03:11,250 the other one has like a two broad thing sort of scene going on right? 60 00:03:11,250 --> 00:03:15,470 So, what I need is like a thing-a-ma-jig kind of one like, one side has it works 61 00:03:15,470 --> 00:03:19,960 with that and then When I go on the other side needs a thing like that two thing. 62 00:03:21,140 --> 00:03:22,500 I think that's all right? 63 00:03:22,500 --> 00:03:23,360 Something else? 64 00:03:23,360 --> 00:03:25,340 Okay, thanks bye. 65 00:03:25,340 --> 00:03:27,560 And I need a power adapter. 66 00:03:28,760 --> 00:03:30,550 It's a different world right? 67 00:03:30,550 --> 00:03:34,160 That direct kind of communication is what your team wants and 68 00:03:34,160 --> 00:03:38,660 it's definitely something that hiring managers look for when building a team. 69 00:03:38,660 --> 00:03:41,890 This is why questions about design patterns are often part 70 00:03:41,890 --> 00:03:43,480 of the interview process. 71 00:03:43,480 --> 00:03:46,050 It's not because interviewers are trying to stump you. 72 00:03:46,050 --> 00:03:49,390 They're trying to make sure that you can understand the shared vocabulary, and 73 00:03:49,390 --> 00:03:52,210 communicate with your new potential teammates. 74 00:03:52,210 --> 00:03:54,400 Check the teacher's notes for more on this. 75 00:03:54,400 --> 00:03:58,300 Now the second, equally important, reason that you should get familiar with design 76 00:03:58,300 --> 00:04:01,300 patterns is because you are already surrounded by them and 77 00:04:01,300 --> 00:04:02,810 you probably don't even realize it. 78 00:04:03,840 --> 00:04:06,510 You can hardly refresh your browser these days without 79 00:04:06,510 --> 00:04:08,920 landing on a new web framework. 80 00:04:08,920 --> 00:04:13,800 Most of these frameworks organize their code using something known as MVC, or 81 00:04:13,800 --> 00:04:15,690 model view controller. 82 00:04:15,690 --> 00:04:21,120 MVC is called a complex pattern because it combines several design patterns. 83 00:04:21,120 --> 00:04:24,130 The controller follows the mediator pattern. 84 00:04:24,130 --> 00:04:27,130 The view usually performs the composite pattern. 85 00:04:27,130 --> 00:04:31,920 And the model employs the observer pattern to control communications with the view. 86 00:04:31,920 --> 00:04:34,010 Like I said, you're surrounded by these patterns. 87 00:04:35,120 --> 00:04:38,080 The reason why frameworks are heavy users of patterns 88 00:04:38,080 --> 00:04:43,170 is because their authors want you to integrate your code by extending theirs. 89 00:04:43,170 --> 00:04:47,130 Writing extensible code like that makes the architecting of a framework 90 00:04:47,130 --> 00:04:51,470 quite the difficult task and patterns are definitely used to lighten the load. 91 00:04:51,470 --> 00:04:54,340 Patterns allow framework authors to structure their code in a way 92 00:04:54,340 --> 00:04:55,220 that is predictable and 93 00:04:55,220 --> 00:04:59,060 understandable by developers looking to integrate their framework. 94 00:04:59,060 --> 00:05:01,140 Without requiring their users, and 95 00:05:01,140 --> 00:05:04,030 that's us, to edit the source code of the framework. 96 00:05:04,030 --> 00:05:08,590 In fact, most software libraries use design patterns for the same reasons. 97 00:05:08,590 --> 00:05:12,400 The author's intent you to extend their code and use the library for 98 00:05:12,400 --> 00:05:16,850 your specific application while not needing to modify their library directly. 99 00:05:18,130 --> 00:05:21,730 So most likely just about any one of those gyms jars new gets or 100 00:05:21,730 --> 00:05:25,790 pods that you use include one or more design patterns. 101 00:05:25,790 --> 00:05:27,100 And that makes sense, right? 102 00:05:27,100 --> 00:05:30,190 It's a known solution to make the code more extensible. 103 00:05:30,190 --> 00:05:31,390 Why reinvent the wheel? 104 00:05:32,610 --> 00:05:35,920 Recognizing the patterns at play will help you better grok or 105 00:05:35,920 --> 00:05:40,210 understand what the code is attempting to do from a bird's eye view. 106 00:05:40,210 --> 00:05:42,170 If you're going to contribute to open source software, 107 00:05:42,170 --> 00:05:45,740 which I highly recommend that you do You'll be able to jump right in and 108 00:05:45,740 --> 00:05:48,510 start coding if you're able to recognize the patterns being used. 109 00:05:49,700 --> 00:05:53,690 Here's a tip to help you recognize design patterns in other people's code. 110 00:05:53,690 --> 00:05:57,770 The patterns name is usually added to the name of the class method or 111 00:05:57,770 --> 00:05:59,530 function that uses the pattern. 112 00:05:59,530 --> 00:06:05,970 For instance, string builder, event facade, and message mediator. 113 00:06:05,970 --> 00:06:09,090 Now you can bet that the result of the conversation that I just had with my 114 00:06:09,090 --> 00:06:12,630 imaginary team members result in a class that had adapter in its name. 115 00:06:13,780 --> 00:06:18,270 Okay, so now that you know why you should familiarize yourself with these patterns, 116 00:06:18,270 --> 00:06:21,540 why don't we take a look at one pattern in a little more detail. 117 00:06:21,540 --> 00:06:24,640 At the same time you'll learn the general make up of a pattern.