"One of the most important aspects of writing maintainable code is being able to notice the recurring themes in that code and optimize them. This is an area where knowledge of design patterns can prove invaluable."
- Addy Osmani
It's important to understand the significance of how and why your favorite libraries are written the way they are.
Why are design patterns important?
"The implementation of a design pattern per day keeps the angry developer away."
- Old Chinese proverb
Let's take a look at some reasons as to why design patterns are extremely useful.
- Patterns are an easy way to refer to a very specific implementation of code while avoiding the additional discussion it would take to get everyone on the same page if the vocabulary used for patterns didn't exist.
- Patterns can help to thwart minor issues that can create major problems later down the road.
- Patterns exemplify a level of sophistication that can provide both performance boosts and reduction of excess memory usage.
- Patterns are established through rigorous testing which means they are, for the most part, "tried and true" and do aid in solving a particular type of problem.
Categories of Design Patterns
There are numerous ways we can categorize all of the design patterns but for the sake of brevity, the three most popular are: creational, structural & behavioral. Creational focuses on object creation (imagine that). Structural focuses on object composition. Behavioral focuses on communication between objects. While no single implementation of these types of design patterns are perfect, it's important to understand that being familiar with all of them can educate you to make an informed decision based on the needs of your application. Take some time to review the following tables as they summarize the methods that each design pattern implements.
# Creational Design Patterns
Creational design patterns deal directly with object initialization procedures focusing on the creation of situation-specific objects. Without thinking about how objects are created, a level of complexity can be added to the design of an application. Creational design patterns work to solve this problem by adding a layer to the object creation process.
# Structural Design Patterns
Structural design patterns are ones that focus on easing the relationship between different components of an application. They help to provide stability by ensuring that if one part of the app changes, the entire thing doesn't need to as well.
# Behavioral Design Patterns
Behavioral design patterns emphasize flexibility through the identification of common communication patterns between various objects.
If you've made it this far, you should have a pretty basic understanding of the different design patterns and what they do. Please feel free to refer back to these tables at any time as a point of reference; they're perfect for jogging your memory on a slow Monday morning.
- The Module Pattern
- The Revealing Module Pattern
- The Singleton Pattern
- The Observer Pattern
- The Mediator Pattern
- The Prototype Pattern
- The Facade Pattern
- The Factory Pattern
I mentioned this before but I feel like it's worth mentioning again as a conclusive note: there is no one design pattern that is perfect but rather the use of multiple design patterns can help to facilitate a better approach to the web applications you write.
"You have your way. I have my way. As for the right way, the correct way, and the only way, it does not exist."
- Friedrich Nietzsche