Design patterns help you write cleaner, more elegant code. Learn why you should use them to improve your coding style.
- [Narrator] So what is a design pattern? A design pattern is a way of structuring code to solve a specific problem. The aim is to make use of solutions to common problems and make code more elegant and flexible. Writing code in Java is difficult and writing codes that's maintainable and reusable is really difficult. Coming up with the best solution to a design problem also takes a lot of time. And in reality, your solution might not be the best one possible especially when considering all the trade offs. But the good news is that if you're stuck with a difficult decision about how to design your code the chances are someone else has come across the same problem. And there's likely to be a tried and tested solution. It makes sense to use a solution that you know is proven to be a good one. Imagine a junior developer has to implement the links list or something. They're probably going to Google it and have a look at the stack overflow answer with the most upvotes to see how someone else did it and then copy and paste that code. And imagine a senior developer has to do the same thing, if they haven't had to do this for a while, they're probably going to Google it too and do the exact same thing. Using design patterns is kind of like doing this on a bigger scale. You're reusing other people's accepted solutions for design decisions. Knowing about design patterns also makes it easier to work with existing code because if someone else has used the design pattern, you'll already be familiar with that solution and you'll understand why they chose to do it that way. And knowing about how and why design patterns are used is a good way of broadening your knowledge of object-oriented design. Knowing the reason why designing code in a certain way is better than other ways is a good way to develop your skills and become a better developer. Some more good news is that learning design patterns doesn't require knowledge of any complex or obscure codes to be able to use them. It doesn't matter what sorts of Java applications you might develop or have experience in, but knowing how to use the right design pattern in the right situation, will make you a much better developer. And knowing when not to use a certain pattern is important too. The concept of design patterns originally came from a group of four developers who are referred to as the Gang of Four. They wrote a book called "Design patterns, elements of reusable object-oriented software" this book is often credited with being a milestone in how software is written. So design patterns are an important and useful tool for writing simple, elegant well-designed code and for writing it quickly and efficiently.
- Passing requests to objects with Chain of Responsibility
- Encapsulating a request as an object with Command
- Interpreting sentences in a language with Interpreter
- Accessing objects in order with Iterator
- Defining how objects work together with Mediator
- Capturing and restoring an object's state with Memento