What Are Design Patterns?
This lesson introduces design patterns, the definition, their uses, advantages, and a few drawbacks.
We'll cover the following
Definition
As developers, we want to write efficient, reusable, and easily maintainable code. As JavaScript developers, you can end up writing code for large applications; hence the structuring of the code becomes important. While developing, you may also find yourself writing similar code for separate problems. In such scenarios, it’ll be helpful to have a structure that could be used to solve various common issues. This is where Design Patterns come into play.
Design patterns are solutions to commonly occurring problems in software design, such as writing JavaScript web applications. They can also be considered as templates that can be used to solve various issues in different situations.
The illustration above shows a real-life implementation of design patterns. The ice cream factory is acting as a template that can be used to produce different types of ice cream according to the requirement. This is similar to how a design pattern can be used as a template in different circumstances.
So what are the advantages of using design patterns?
Advantages
Let’s look at some of the advantages of using design patterns:
-
They provide one of the best solutions as they have been derived and optimized by various experienced programmers over time; hence, you can be certain that these solutions are correct and efficient.
-
They are generic templates that can be modified and used for solving different problems.
-
They can provide a clean and elegant solution to a large problem by avoiding repetition in code.
-
They provide a template on which the developers can build upon. This allows developers to spend less time on the structuring of the code and more on the overall quality of the solution.
-
Since design patterns provide optimal solutions, they reduce the size of the codebase.
Where there are many advantages of using design patterns, it is also important to be aware of their drawbacks in order to implement them carefully. Design patterns can complicate the architecture of the application if they are managed poorly. On top of that, developers who are not familiar with them might end up getting confused as to why certain patterns are being used.
Now that you know what design patterns are, we will look at their different types in the next lesson.