Generally, before learning a new knowledge, I will think about why I want to learn it and what help it has for my work and life.
In the case of design patterns, the main reason is to address the problem of code being difficult to maintain. For example, the structure is chaotic, the reusability is low, or a new requirement requires a lot of old code to be changed.
So what is good software design?
High cohesion, low coupling, good maintenance, good reuse
What is garbage design?
- New requirements require a lot of changes, even rewrites.
- Change a place to find the whole body, dare not change at all.
- It’s all if else, and you’re going to judge it step by step.
- Let the caller care about the implementation logic. So I’m going to take a method or a class, and I’m going to try to understand this class.
The general idea:
Abstract, reusable, single responsibility.
Learning design patterns is easy, but using them is hard. When should I use what model, is it abused, is it making my maintenance harder?
Here’s how I look at this question:
The fundamental problem of complexity in software design: changing requirements.
First, understand where the product is going and briefly analyze how the requirement might change. Then increase reusability as much as possible. Then start thinking about design patterns. And then at the end of the day, does that make it easier or does it make it more complicated?
For example, a very simple function to count the number of requests to an interface. If it is impossible to change in the future, then a direct method is finished, do not think too much. If you feel like you’re going to need more than one interface in the future, consider the design pattern and add interfaces without changing the code in this version. If you feel like you’re going to do a statistical analysis later, the design of the table should be optimized. If you feel this quantity is very large, is it necessary to divide the table?
conclusion
In the end, it’s all about being alive.
Many people like to pursue technology, then see a method paste copy and spray.
But in my understanding, everything is in the service of productivity. The act of pasting and copying does make the code shit, making it excruciating to maintain.
However, if you are already on the verge of sudden death, don’t think too much about it. Either change careers or do it as soon as possible.
Or if you’re writing one-time code that can be deleted in a few days, and the schedule is urgent, you don’t need to think too much about it. Change companies.
If you still have a heart that loves programming, do not stop learning. If you keep trying to be fast, your technology will not progress, and it is impossible to jump out of this level circle. You will keep paying the technical debt, and finally feel unable to do it.
Studying won’t raise your salary in the short term or even make you more tired, but it will plant a seed for your future that will germinate one day.