When i was at fourth semester of my study, i found that its easier for us programmer to code by practicing design pattern to problems that we currently working. Anyway, what is design pattern? Why it is considered best practice in programming? Lets find more about it!
What is Design Pattern?
Generally, we can say design pattern is a pair of problem-solution. This concept is borrowed from Civil and Electrical Engineering domains where there are architecture that has been defined before if we want to accomplish certain goal. In software engineering, the problem usually can be defined as commonly occurring problem. The examples of the common problem are:
- We have a problem where we want an object can update their attributes according changes that has been made in other class
- Another example where we don’t want user exposed with complex system of our program.
From those examples above, we almost can see that these problem are keeps being asked by developers out there. Therefore, the Design Pattern born from it. It is made from programmer by programmer. It purposes to help us speed up development process by using tested and proven development paradigms. Still not convinced? Lets talk about why we should use design pattern!
Why use Design Pattern?
There are several reason why we should use design patterns in our software development. They are:
- It helps us speed up development process because we using tested and proven development paradigms. We just need to reuse it and synchronize the desired pattern with our case.
- Using design pattern can help us to prevent unwanted issue such as code smell. It also minimizes bug in our program.
- It improves our code readability, reusability, scalability, and maintainability because it is derived from best programming practice like OOP, encapsulation, sub-classing, etc.
- Design pattern can minimize needs for code refactoring. This is because the solution that has been made already classified as the optimal one.
- It is language independent, which means design pattern can be applied in most of programming language.
Now we have learned why should we use design pattern. Next, lets talk about types of design pattern.
Types of Design Pattern
We can divide many design patterns out there into three categories according to their main purpose:
- Creational — Design pattern used for helping us in terms of object creation. The examples of popular creational design pattern are: Singleton Pattern, Factory Method and Abstract Factory Pattern.
- Behavioral — Design pattern that used to manipulate object behavior and interaction with other object so that it becomes more efficient and effective when executing a task. Some of the common behavioral design pattern we can find are Observer Pattern and Strategy Pattern.
- Structural — Design pattern used to define and compose class structure. Facade Pattern and Composite Pattern is one of the structural pattern that commonly used in programming.
When not to use Design Pattern?
There are some cases where design pattern cannot be applied. Even if its applied, it will make our code become more complicated and hard to manage.
Initially, you should not think about Design Pattern. An expressive and less code is always the first line of defence.
— Vishal Chovatiya
For example in my current on going project that i taken from Proyek Perangkat Lunak (PPL) course, we are tasked to create a mobile based information system with REST API backend. In the backend, all of our defined entity does not interact too much with other entity. We can say that the purpose of the backend is just for CRUD purpose.
The CRUD itself considered as no-pattern program. We can’t implement any design pattern in it because the function is quite simple. It does not needs behavioral changes nor creating or composing class structure nor creating objects. Therefore based on that facts, we will not implement design pattern into it.
Conclusion and Thoughts
We can conclude that by practicing design pattern, it will increase our work efficiency and also quality of it. It really helps our code become more maintainable, readable, and scalable. Design pattern also makes our code has less need for refactor. Personally, i think it really helps us developer if we are facing the problem that requires design pattern. We just need to reuse the proven and tested paradigms and adjust it according our cse, and voila! The problem is already handled.
However, keep in mind that not all problem should be solved by design pattern. We cant force the problem must be solved by design pattern because it will make our program hard to maintain and scale. We should be careful with our decision, don’t let it become boomerang for our work.
Thanks for reading!