How to get started with Design Patterns
Tags: designpatterns, solid, beginners, codequality
Published: 2020-02-13 23:36:12

When we are learning how to code, we usually only worry about making software work - and that’s already pretty important. However, as we make progress towards our journey as software developers, we start considering other aspects that can improve the quality of the things we develop. For instance, if we are working on a project with other people, how can we make the code more readable, easy to understand, to maintain, and to evolve? How can we avoid code duplication? How can we make the code more flexible? 

We often hear some concepts related to trying to solve these issues, such as the famous Design Patterns. However, what exactly are Design Patterns? We can start talking about the things they **are not**. 

Design Patterns are not magic formulas that solve problems. They are not code snippets that can be inserted into our application. They are also not algorithms. Design Patterns, as the name suggests, are just patterns that offer a concept of a solution for a design problem that happens frequently. Implementation of each pattern can change according to the domain, language, and other factors. These patterns were created from the experience of several developers who were trying out different solutions for recurring problems they faced. A book called [Design Patterns: Elements of Reusable Object-Oriented Software] (https://www.amazon.com/dp/B000SEIBB8/) was published a long time ago and presented 23 Design Patterns. Since then, other patterns were identified. These patterns have even come to influence programming languages and, as everything in software engineering, they are frequently changing. 

<h3> Why should we learn Design Patterns? </h3>

Imagine you’re doing something for the first time. For instance, learning a new recipe. You know where you want to get and you have an ideia of how to mix the ingredients to do so. However, it would be very helpful if you could follow a recipe that would guide you to achieve this result in a way that several people have done before and avoiding common problems in the way. That’s sort of how it works with design patterns.
A lot of developers have tested these patterns, faced these problems, and documented these solutions. Besides that, Design Patterns define a common language for us to talk about possible solutions with our teams, for instance, while pairing. 

<h3> Is it the same as SOLID principles? </h3>

No, but it has everything to do with it. Actually, I believe it’s necessary to have a better understanding of **SOLID principles** before starting to study Design Patterns, because these principles are used in most of the patterns.  The goal is to help us create software that is easy to maintain and to understand. The name SOLID is an acronym for: 

* **S**ingle Responsibility: a class should have only one responsibility.
* **O**pen/Closed: classes should be closed for change and open for extension. In other words, if a class works properly and has been tested, we should avoid changing it. To insert new behaviour, we should extend the class instead of modifying it.
* **L**iskov Substitution: a subclass should be able to substitute the superclass without changing the behaviour of the system.
* **I**nterface Segregation: a class shouldn’t depend on methods it doesn’t use.
* **D**ependency Inversion: dependencies can determine how easy it is to make changes to the system. If we have a lot of them, that can lead to high coupling. This principle says that high level modules should depend on high level generalisations, and not on details. 

Design Patterns follow some of these design principles to cover topics such as flexibility and maintainability. For instance, a lot of Design Patterns use the Open/Closed system, following the idea that it should be possible to extend your software, or the Dependency Inversion patterns, abstracting behaviour through interfaces. That’s why it makes sense to first have a good understanding of design principles and then continue to Design Patterns. 

<h3> What type of Design Patterns exist? </h3>

<code> Design Patterns were classified in three different types, based on its purpose. </code>

**Creational Patterns** are about the process of creating objects. Depending on the context or on our goal, the traditional way of creating objets might not be the best solution. For example, if we have a class that connects to a database, we may want to create just one instance of this class, instead of having new connections in different places of the system. For that we can use a pattern called Singleton. There are other patterns included in Creational Patterns, such as Factory Method, Builder, Prototype, and others, all of them trying to solve problems related to the creation of objects. 

 **Structural Patterns** entails how objects are connected and their relationships. These patterns are necessary because the more a system grows, the more complex it gets. With more complexity, we need to find a sustainable way of maintaining the relationship between objects in which we allow for the creation of new functionality in a simple manner. For instance, let’s say we created a hamburger and we want to add items dynamically, such as extra cheese or a salad. Instead of creating a hamburger class and several subclasses with each type of additional item, we could apply the Decorator patterns, which uses aggregation to dynamically combine behaviours. Other well known patterns in this category include Adapter, Composite, and Facade.  

 **Behavioural Patterns** deal with how objects distribute work, how they collaborate to achieve a common goal. If we go back to the hamburger case and we want to let our customers know every time a hamburger is ready for delivery, we can use a pattern from this category, which is called Observer. We would have clients observing and waiting to be notified. This type of pattern include others well known, such as Command, Iterator, and Strategy. 

<h3> How can we know which patterns to use? </h3>

There is a huge catalog of Design Patterns and it’s very unlikely we will remember all of them after reading a book about the subject. The important thing is to first know that these patterns exist. After that, we need to understand what problem we are trying to solve. Then, we can deepen our knowledge in the patterns that try to solve these problems. Having a better understanding of how the pattern is applied and looking at example code is a good starting point. It’s important to remember there is no “right answer” when it comes to Design Patterns. It is a choice that should be made based on the context and its needs, and evaluating the trade-offs. 

<h4> References and Study Materials </h4>

* Book: Design Patterns: Elements of Reusable Object-Oriented Software
* https://sourcemaking.com/design_patterns
* https://refactoring.guru/design-patterns
* https://www.coursera.org/learn/design-patterns/
* https://www.baeldung.com/solid-principles

<i> <a href="https://dev.to/thayseonofrio/como-comecar-com-design-patterns-31f2"> Originally posted in Portuguese - PT-BR </a>