How Do I Learn Design Patterns? Which Design Patterns Should I Know?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
understanding design patterns can be a tricky subject should you learn all the design patterns how many are there is it good to apply all the design patterns to your code which ones are important which ones aren't what about the gang of four these kind of questions i get a lot so in this video i'm going to answer those questions so if you don't know me my name is tim cory and my goal is to make learning c-sharp easier and sometimes that means taking a break from writing code and talking about the code so you have a better understanding of what's going on and why you should do what you do so if you're interested in videos like this there's a whole playlist for these dev questions and i also have playlists for other topics as well as getting started in c-sharp and data access and so on check them all out also make sure you hit the subscribe button to subscribe now if you also have any questions that you think might be a great question for the dev questions series leave it down the comments below and i'll be sure to add to the list if you have questions you want to see answered in code format leave those questions as well i'll put it on the list okay so let's talk about design patterns now one of the things that is a little bit frustrating for me and it's not anybody's fault at least not the learner's fault but one thing that frustrates me is that people that are just starting out are told you need to learn all these design patterns and it makes the barrier to entry for c sharp very very high and here's the deal you can write good code you can write great code without using design patterns now you need to write design pattern at some point but your code doesn't have to have those patterns yet learn how to write code because the very first thing about a c-sharp application the thing that separates it good from bad is does it do the job it was intended to do if it does it passes that test it's a good application could it be better sure but that doesn't mean it's not a good application so take a step back and don't get overly concerned about design patterns now with that don't just stop there because design patterns do have a very important role in software development they allow us to kind of have this this ready-made pattern or written a design to put into our code to make it better in some way hopefully now i often encourage people to start if you want to start down this path and this is a more advanced path so don't start when you're just starting to learn c sharp but once you're kind of ready once you're at that intermediate to maybe even starting to think about advanced level at least intermediate though a level of knowledge of c sharp then i suggest starting with dry it's not a pattern it's a principle okay so what's the difference a pattern and a principle first a a pattern is a specific do it this this way in this language a principle is a a guiding factor okay so dry the guiding factor is don't repeat yourself so you find yourself writing the same code for the same reason more than one time that's a good time to think about how can i refactor that code that's both an easy and hard way to do your first pattern your first principle because it's easy to spot those things it can be a little bit more tricky to learn how to start implementing so that's a great place to start because it's easy to spot and then after that i recommend the solid principles again principles and even there there's five principles in solid s-o-l-i-d um each one is an it's an acronym for a for actually for an acronym so we have the single responsibility principle the open close principle list cost substitution the um i'm drawing a blank on eye and depends the inversion principle i'll think about it interface segregation principle there we go that's the i and then d is depends the inversion not depends the injection so those five principles they're they kind of work as a team but at the same time you can pull some of them out so i usually say start with s start with single responsibility principle when you're writing a method or a class especially a method make sure it does one thing not two things not three things not four things which is often the place where newer developers get stuck is they write this massive block of code that does everything and it works and that's awesome you did a great job you got something working the next level in your programming may means make srp a thing in your code where a method does one thing and then once you got through that then think about maybe the end one d which is depends the inversion which usually takes the practical implementation view of dependency injection okay i have videos on these so if you want to check out my drive video or my solid videos you'll see these things as well as a video on dependency injection so these are principles not patterns they kind of go in the same category because a principle just applies as an overall idea to your code you have to figure out how to actually make it work but a a pattern is something that is is pretty prescriptive here's what you do okay so i recommend starting the principles and those are the principles i recommend and even there's the order i recommend so first dry then srp then depends the inversion principle or dip i love that acronym and then go back and look at the other solid principles now before we go further and talk about the gang of four and others um let's stop from it and talk about even what i've talked about so far these things are if you're not careful dangerous to your coat it may sound like well tim these are good things right so therefore i must be able to apply all of my to my code and the answer is not necessarily no because you can over engineer your application if i'm going to write a small hello world application i'm not going to apply uh solid i'm not going to apply any design patterns probably i'm not going to even necessarily apply dry i might have you know a few lines that might repeat and that's okay because the scale the application as the application grows i might start applying those principles and patterns so don't look at design patterns or principles as this you must do this to have a good application again if your application runs you've got a good application can it be better sure that does not always mean applying a principle or pattern okay so that's what we get into the game of four now where the gang of four is very prescriptive and it says here's how to fix a specific situation okay it's not going to be this this overarching it applies to everything like solid does or like dry does this is very specific to a specific uh point in time a specific piece of your application you might apply a pattern so here's what you don't want to do you don't want to say i need to learn four patterns there's i think 23 of them don't learn those and then say okay how can i apply those to my application it's kind of backwards what you want to do is when you're building your application if you see a pain point if you see something where you say i just kind of have some friction here maybe i'm having too much repetition or maybe it's too tightly coupled or something like that then you can look and see hey is there a pattern that can make my life easier in this situation but notice you had the problem first then you came up with a solution using a pattern and that's where i'd like to see you apply these patterns is i want to see you look at your problem identify your problem and then go see if you can find a solution in the patterns or principles that's a much better way of going about it otherwise you can write a ton of code and really some of it's getting bad code in fact when i look through the gang of four there's a lot i almost don't want to teach and i've really hesitated to teach the gig of four because what i don't want to do is come across as saying use this all the time okay because that's more dangerous than you not using it at all there's some patterns where it's really an anti-pattern something you should not do unless you are a specific very specific situation so you gotta be careful how you use patterns don't approach it as in order to be a better developer i need to apply these patterns approach it as i have a problem in my code let's see if i can solve that with a pattern let's see a pattern makes my code better in some way now it is helpful to know these patterns so you have a better frame of reference to say oh you know what i could probably solve my problem here by applying this principle or this pattern okay but don't go looking for how to apply it look for your code at the problems and look to solve them okay so that's my thoughts on design patterns they are great design principles are great just be very careful that when you're applying them you're doing so to solve a problem not just to theoretically make your code better more complex code isn't better what you want is simple code that runs well if you have a problem with your code where it may be simple but not running well or not not function the way you need it to that's when you might be able to improve it with your pattern or principle okay so that's that's my thoughts on this that's why i haven't really gone into the gang of four yet to teach it i will at some point when i get some time but um i want to be very careful about how i do that in order to not lead you down a path that's actually going to hurt you rather than help you okay so that's it for this video if you have other questions about design patterns or just questions in general leave them down below in the comments i will definitely get back to you and answer you as soon as i can and if it's a great question i think we can all benefit from i'm going to put it on a list and hopefully address it in a future video okay also leave a thumbs up if you like this video share it i really appreciate that okay thanks for watching as always i am tim cory [Music]
Info
Channel: IAmTimCorey
Views: 62,584
Rating: 4.9452753 out of 5
Keywords: c#, c# developer questions, dev questions, dev questions series, developer questions, how do i, iamtimcorey, learn c#, tim corey, design patterns, design patterns c#, design patterns software engineering, which design patterns, gang of four, gof, go4
Id: Ao-uWfNKLtE
Channel Id: undefined
Length: 12min 23sec (743 seconds)
Published: Thu Jun 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.