Why You Should Know About Design Patterns6:25 with Craig Dennis
Knowledge of design patterns is crucial to developer life. Let's explore why.
So design patterns seem pretty great, right? 0:00 Well, I got some bad news for you. 0:03 Design patterns are not a magic bullet. 0:05 They aren't going to solve all of your coding issues. 0:07 In fact, you shouldn't ever really start with a pattern you should reach for 0:10 them only when you need them never before you start coding. 0:13 Most languages these days have some sort of package manager that allows you to use 0:17 packaged up code. 0:21 Now whether those packages are called modules, libraries, pods, Gems, JARs, 0:22 NuGet whatever. 0:27 They represent code that is available for you to use so 0:28 that you don't need to recreate the wheel Design patterns are a lot like this. 0:31 Except instead of viewing them as code reuse 0:35 you should view them more like experience reuse. 0:37 People have been there and they've done that, so 0:40 you should lean on their experience. 0:43 Again, for clarity's sake, 0:45 don't go grabbing from the experience repository right out of the gate. 0:47 Always go for the simplest solution first and 0:51 introduce the pattern only when the need arises. 0:53 The great acronym that we should all live by goes well with this, K-I-S-S. 0:56 Kiss, Keep It Simple Smarty pants, or something more rude than that. 1:01 So then you ask, should I even bother learning about them if I can't use them? 1:08 And that's a great question, and I'm glad you asked. 1:11 The simple answer is this, you need to be familiar with them. 1:14 There are two major reasons I want you to get familiar with design patterns. 1:19 Well, look at both of them in more depth here. 1:23 So the most amazing benefit of design patterns is that they create a shared 1:25 vocabulary. 1:30 Since these patterns are named they can be used to communicate 1:31 a very specific solution in just a few words. 1:34 Now there's a huge difference between these two examples. 1:38 You should just kind of make the first class behave like 1:42 the other one by making the methods in here the same as the other one, so 1:44 that the caller thinks that it's actually doing its thing but 1:48 really is calling specific methods on the original object. 1:51 And you should use the adapter pattern. 1:55 That first statement is pretty much exactly what the adapter pattern does. 1:59 The adapter pattern is a lot like one of these power adapters here, 2:03 that you would use when you're traveling the world. 2:06 There are many different types of electrical outlets. 2:08 And this hair dryer that you brought for 2:10 home, might not plug into the outlet in the other country. 2:12 A power adapter lets you connect your hair dryer to the outlet. 2:16 You plug the hair dryer into one side of the adapter and 2:20 the other side of the adapter fits into the outlet. 2:23 This is your adapter. 2:26 The port in the wall is what you're trying to adhere to, 2:28 and this is your original class. 2:30 By applying the adapter, they work together. 2:33 Now, this is the level of familiarity that i suggest 2:36 that you need when understanding design patterns. 2:39 You don't need to know how exactly to code up the adapter in your specific language 2:42 because you've got your catalog of patterns to draw that experience from. 2:46 Now take a quick second with me here and try to feel the difference 2:51 between these two approaches and the power that the shared vocabulary word brings. 2:54 Okay, so I have this device and it has this plug that doesn't fit into the other 2:59 outlet right, so the side has like a three standard prongs right and 3:03 the other one has like a two broad thing sort of scene going on right? 3:06 So, what I need is like a thing-a-ma-jig kind of one like, one side has it works 3:11 with that and then When I go on the other side needs a thing like that two thing. 3:15 I think that's all right? 3:21 Something else? 3:22 Okay, thanks bye. 3:23 And I need a power adapter. 3:25 It's a different world right? 3:28 That direct kind of communication is what your team wants and 3:30 it's definitely something that hiring managers look for when building a team. 3:34 This is why questions about design patterns are often part 3:38 of the interview process. 3:41 It's not because interviewers are trying to stump you. 3:43 They're trying to make sure that you can understand the shared vocabulary, and 3:46 communicate with your new potential teammates. 3:49 Check the teacher's notes for more on this. 3:52 Now the second, equally important, reason that you should get familiar with design 3:54 patterns is because you are already surrounded by them and 3:58 you probably don't even realize it. 4:01 You can hardly refresh your browser these days without 4:03 landing on a new web framework. 4:06 Most of these frameworks organize their code using something known as MVC, or 4:08 model view controller. 4:13 MVC is called a complex pattern because it combines several design patterns. 4:15 The controller follows the mediator pattern. 4:21 The view usually performs the composite pattern. 4:24 And the model employs the observer pattern to control communications with the view. 4:27 Like I said, you're surrounded by these patterns. 4:31 The reason why frameworks are heavy users of patterns 4:35 is because their authors want you to integrate your code by extending theirs. 4:38 Writing extensible code like that makes the architecting of a framework 4:43 quite the difficult task and patterns are definitely used to lighten the load. 4:47 Patterns allow framework authors to structure their code in a way 4:51 that is predictable and 4:54 understandable by developers looking to integrate their framework. 4:55 Without requiring their users, and 4:59 that's us, to edit the source code of the framework. 5:01 In fact, most software libraries use design patterns for the same reasons. 5:04 The author's intent you to extend their code and use the library for 5:08 your specific application while not needing to modify their library directly. 5:12 So most likely just about any one of those gyms jars new gets or 5:18 pods that you use include one or more design patterns. 5:21 And that makes sense, right? 5:25 It's a known solution to make the code more extensible. 5:27 Why reinvent the wheel? 5:30 Recognizing the patterns at play will help you better grok or 5:32 understand what the code is attempting to do from a bird's eye view. 5:35 If you're going to contribute to open source software, 5:40 which I highly recommend that you do You'll be able to jump right in and 5:42 start coding if you're able to recognize the patterns being used. 5:45 Here's a tip to help you recognize design patterns in other people's code. 5:49 The patterns name is usually added to the name of the class method or 5:53 function that uses the pattern. 5:57 For instance, string builder, event facade, and message mediator. 5:59 Now you can bet that the result of the conversation that I just had with my 6:05 imaginary team members result in a class that had adapter in its name. 6:09 Okay, so now that you know why you should familiarize yourself with these patterns, 6:13 why don't we take a look at one pattern in a little more detail. 6:18 At the same time you'll learn the general make up of a pattern. 6:21
You need to sign up for Treehouse in order to download course files.Sign up