This is my second article on getting started

I’m sure many people learn about design patterns in this way:

  1. In school, teachers tell you that it’s important to learn not only frameworks but also design patterns. You start to learn about design patterns, read the classic Head First Design Patterns, and you’ll be amazed at how wonderful design patterns are.

  2. You start your job search, you know the interview will ask you about design patterns, and you rededicate yourself to learning design patterns, but this time with a crash course in mind.

  3. In the work, you encounter complex business scenarios and write business code according to your own ideas, but you find that the code is not so elegant, with weak expansibility and serious coupling. Every time the business is expanded, you need to carefully maintain the old code. At this point, you think of design patterns, and you find design pattern related blogs on the Internet and try to introduce design patterns into your project.

Have you learned design patterns after many studies? I think the answer is no. Can you describe design principles naturally? Can you name most of the design patterns? Can you apply appropriate design patterns across the different business scenarios of your project? Can you answer design patterns questions in an interview?

Why should we learn design patterns?

Some people might think that you don’t have to learn design patterns, you just naturally apply them to coding. That’s right! However, most of us are not big, we need to study deliberately, in order to achieve twice the result with half the effort.

The average person may learn the essence of many design patterns in one or two projects, while the average person may learn them in three, five, or even longer (and may always be in a vague state). Through deliberate learning, we can master design patterns more effectively. Natural understanding is like “learning from no teacher”, and deliberate learning is like learning from an experienced master. After all, the probability of “great teachers out of students” is far greater than the probability of “no teacher”.

Write better code

When I’m coding, I often wonder what “good” code is.

From the micro perspective, we can easily say a few: naming conventions, words can convey meaning; High operation efficiency; Save memory; Reasonable data structure and algorithm are adopted.

At the macro level (system level), good code is highly aggregated, low coupled, and extensible. How can we write code like this? Follow basic design principles (such as the single responsibility principle) and adopt appropriate design patterns. The design pattern is the experience summed up by predecessors and the classic solution in various business scenarios.

Improve development efficiency

In real projects, iterations are very frequent. New functions continue to be iterated, and sometimes new functions will be found to be similar to existing functions of the system. You think this is easy to do, and you can simply reuse the original code. But when I started working on it, I realized that it was not easy: the old code did not support direct reuse, so I had to change the original code slightly. After the changes, the new function can be used normally. When you think you’re done, suddenly a test student comes up to you and tells you that the original function is out of order! You realize that the new changes affect the old logic! Ok ~ my old code does not move! Copy a copy of the code out of the transformation.

If you use design patterns, the system scales well, and you don’t have to spend a lot of time thinking about the impact on the old business with each iteration. This can greatly improve the efficiency of development

Need to interview

This may be the least worth mentioning, but interviews are not the purpose of learning. But for us, it’s probably the most important.

How to learn design Patterns

Back to the original question: why did you learn design patterns many times and still not learn them? There are three reasons:

  1. What you learn in school is not put to use.
  2. Study for interview, not in-depth study.
  3. There is no systematic learning for those who encounter problems.
Deep study — ask the essence of things

If only shallow learning design pattern, it will only leave an “impression” in the mind, with the passage of time, gradually forgotten. Asking questions about the nature of things is the brain’s way of thinking. For example, when learning a design pattern, you think about what the concept of the pattern is, why it is important, how to understand it, what makes sense to use it, how to use it correctly, and how it differs from other similar design patterns.

Reading source code is also a way to ask the essence of things.

Systematic learning — building a knowledge system

We often say that the mastery of knowledge, is the integration of all aspects of knowledge or truth through the integration, so as to get a thorough understanding of the system. Establish knowledge system, connect each knowledge point namely, form a knowledge net. Systematic and comprehensive study of design patterns is to establish a knowledge system of design patterns. When faced with a practical problem, you can find the answer by looking at a certain knowledge point and its various connections.

To apply what you have learned

The design patterns learned are nothing if not applied to the project. To truly master design patterns is to be able to use corresponding design patterns to solve practical problems. The actual application of design patterns, in turn, deepens the understanding of design patterns.

The end

Next, I will continue to update the series of design patterns, with “deep learning”, “systematic learning”, “learning for use” as the direction of learning design patterns together with you!