August, 2022
Patterns are trusted recipes used to solve recurring problems. They are models used to guide the development of solutions to the kind of problems software engineers face everyday in their work. You may ask: why the hell do you need to learn about design patterns. I will help answer the question with some reasons based off my personal experience:
Now that I've shared some of the personal reasons why we need to learn
about design patterns, I will try to share example problems that certain
design patterns can help us solve.
Imagine building a class where some other classes are interested in the
changes that happen in this your class. Straightforward thinking says,
couple the classes together so that they can be close to the source of data changes.
However, design patterns teaches us about observers. We could create an observer
within the class for which other classes are interested in its changes and then
have the interested classes register their interest in the observer. When our class
eventually changes, our class observer notifies all the other interested classes
and if possible provides the classes with snapshots of the before and after
states of our class data.
Also, sometimes, we don't want direct access to our classes or we want to hide
our API implementation from the consumers, imperative thinking will be to reimplement
our class or API and use that as a cover for original API or class.
However, with the proxy pattern we can have a framework for solving this kind
of problems by encapsulating the class of interest inside a proxy and
have other interested parties access it via the proxy.
Some real life examples of design patterns that we use in our everyday work are:
In conclusion, design patterns are a great tool in any software engineer's toolbox as they are a sure way to write clean, DRY, extensible and easy to maintain code. They also help us to box problems into frameworks from which they can be easily solved which invariably makes our job easier ie: our job becomes more of finding the interactions between state, behavior and other 3rd parties and the design patterns that best model/solve the problem would evolve accordingly. To learn more about design patterns, read the book: Design patterns in Ruby by Russ Olsen. Hopefully, I've succeeded in stirring your interest towards seeing the need to learn about design patterns. Go into the world and explore.