5 reasons to finally learn design patterns
Crafted by experienced object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain.
If you’ve encountered design patterns only in passing, in a nutshell, they’re general object-oriented solutions that you can use in your own designs. Crafted by experienced object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain. So, if design patterns still aren’t part of your development toolbelt, here are five reasons you should add them.
1: Make your life easier by not reinventing the wheel
There’s a constant in software development, and no, it isn’t that projects will always ship late and over budget. It’s change. Change occurs when requirements change, systems grow, new features are added, when performance is optimized, and we’re sure you can think of a few more reasons why change happens. The question is, how can you build software so the impact of this change is minimized? After all, some of the most difficult aspects of software development are understanding existing code (perhaps written by others), and altering existing code without introducing new defects or unintended consequences.
So, what’s the answer? There isn’t one magic bullet, but there are techniques you can use to help you make your object-oriented designs more resilient to change. These are hard-won techniques that have been developed through the experience of experts, and they’ve been cataloged for you. They’re called design patterns, and they provide ready-made design templates that can be applied to your own designs to solve common problems. They’re not libraries or modules; they’re guidelines you integrate into the core of your designs, giving you a leg up in creating flexible and maintainable object-oriented systems.
So, why reinvent a (bad) wheel when you can use the hard-earned experience of the best object-oriented designers?
2: Improve your object-oriented skills
We’re all taught that the cornerstones of object-oriented programming are abstraction, inheritance, polymorphism, and encapsulation—and they are—but designing object-oriented systems doesn’t stop there. In fact, routine application of these basic principles can quickly lead to problems such as code duplication, fragile designs, and explosions in the number of classes you’re using.
At the foundation of design patterns you’ll find another set of design principles that go beyond the object-oriented basics. These design principles can be applied as you create and compose the classes and objects of your design, and come in the form of guidelines like “encapsulate what varies” or “favor composition over inheritance” or “classes should be closed for extension but open for modification.” By knowing these additional principles and how they are incorporated into design patterns, you’ll make yourself a much better OO designer and architect.
3: Recognize patterns in libraries and languages
Design patterns are general solutions to common object-oriented design problems; that is, they are solutions to your design problems. You don’t download and install a design pattern module to add to your design; rather, you implement a design pattern within your system. That said, you’ll often encounter patterns in the libraries, packages, and modules you use. Take for example Java’s file I/O package. Java uses the decorator pattern, which allows you to take a core file I/O object, and then decorate it with new capabilities depending on your needs. This a great way to approach a file I/O system design. And, if you are already familiar with design patterns (and the decorator pattern more specifically), you can immediately understand how this object design is put together and how it works. That gives you a leg up understanding the design.
4: Use the power of a shared vocabulary
As we mentioned in #3, one of the benefits of learning design patterns is familiarity with common patterns—once you and your team are up to speed on these you gain a new power to communicate your design’s intentions with your teammates. As an example, say your teammate needs to describe to you a new part of her design. Here are two ways to describe the same design:
“So I created this broadcast class. It keeps track of all the objects listening to it, and anytime a new piece of data comes along it sends a message to each listener. What’s cool is that the listeners can join the broadcast at any time or they can even remove themselves. It is really dynamic and loosely coupled!”
“So I created this broadcast class that’s based on the observer pattern.”
In the first description, you’ve got a lot to think through and understand to truly get what this “broadcast class” does. In the latter, you already know as soon as the sentence is finished. That’s the power of a shared patterns vocabulary. If you communicate in patterns, then other developers know immediately and precisely the design you’re describing.
5: Find truth and beauty
While there is much learning to be had crafting and building object-oriented systems all on your own, you can benefit from the wisdom that comes from studying and using design patterns. As we’ve already mentioned, design patterns aren’t just thought up or invented; they emerge from deep insight gained through hard work and experience building many systems. As you learn design patterns, you might feel like you’re looking over the shoulder of an experienced software architect—an activity you’ll find illuminating, often surprising, and sometimes counter-intuitive—on your path toward building better, more maintainable software.
Learn design patterns today
A decade ago we created a book that teaches everything you need to understand basic object-oriented design principles and patterns. That book, Head First Design Patterns, won the Jolt award, remains one of O’Reilly’s top selling titles, and has helped hundreds of thousands to learn patterns. We’re excited to announce that we’re about to embark on a bootcamp-style online version of that material in our O’Reilly live training: Design Patterns Boot Camp.We hope to see you there.