If you have studied computer science or software engineering then you have probably came across design patterns. If you haven't, chances are you have faced problems that could have easily been avoided with a design pattern. In this article, I will explain what are design patterns and how it can save you a lot of effort and make your code cleaner.
Motivation
As your project gets larger, you can be tempted to just create code that works, however, this will inevitably introduce problems that are harder to solve in the future. Design patterns allow you to avoid those problems before they arise by writing your code in a way that handles those problem for you.
Prerequisite
To understand design patterns you have to know Object-oriented programming and be familiar with:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
What are design patterns?
Over the years, numerous software engineers have faced recurring problems, accumulated solutions to these problems by trial and error and created design patterns to avoid those problems in the future.
You can think of design patterns as blueprints written by other developers to help you avoid problems that they have already faced.
They are basically time tested solutions to software design problems.
Design patterns categories
Every design pattern falls into one of 3 categories.
- Creational
- Structural
- Behavioral
Creational Patterns
They provide mechanisms to create objects in a way that makes code reusable and clean. An example to that is Singleton pattern
Singleton pattern lets you create one instance of the class and allows for global access to such class. A common case where you need that is for accessing shared resources like the database or a shared state for example. You probably don't want your application to create a new database connection every time a function wants to access the database.
Structural Patterns
They provide mechanisms to assemble large objects and classes from smaller ones providing more flexibility. An example to that is Facade pattern
A facade is a class that provides a simple interface to complex systems. This is helpful in case you are using a library that provides a lot of functionality. Instead of exposing the whole library you can create a facade with only the functionality you need. It's also helpful if a client wants to integrate with you. Instead of giving them access to your whole system, you can give them a set of functionalities that they can use as a facade.
Behavioral Patterns
They provide mechanisms of running algorithms and communications between objects. An example to that is Strategy pattern.
Strategy pattern utilizes polymorphism to allow for using different behaviors for a specific task. If you have a class that does a specific task in different ways according to some input then the strategy pattern is useful. For example, if you have a class that calculates the area of a shape. Instead of having one class with a function Area() and changing the function behavior by checking what the shape is, you can create an interface that has the function Area() and create a class for each shape that implements that interface. Now other functions will not need to know what shape they're using they will simply call the function and each class will run its specific Area function.
Conclusion
That was a quick intro to design patterns just to give an idea about what they are. I gave a simple example to each category just to clarify what the category is about. I didn't dive into the examples too much here but I will probably create a separate post for each pattern explaining it more.
Photo by Glen Carrie on Unsplash