Programming pain points

So what exactly are design patterns?

Before INTRODUCING this concept, let me ask you if you have ever worked on “family code” in your work. That’s when your boss asks you to maintain an old project written by a previous programmer.

Having been a programmer for 10 years myself, I’ve had this experience many times. Much of this ancestral code had no idea what it was written about, with very few comments and a chaotic structure that dared not be modified or deleted.

The person who wrote the code has either left his job, and there’s no one to ask.

Even when he was still employed, he could find people who wanted to ask them a few questions about the code, and they still had to look at them.

That’s the pain of heirloom code.

What are design patterns

What should we do?

Many older programmers have come up with a series of solutions through long-term practice. These solutions improve code readability, increase code reusability, and ensure code extensibility.

This set of solutions, known as design patterns, is a classic piece of object-oriented programming.

A design pattern is an abstract programming idea that is not limited to a particular programming language, but is interlinked with many languages. For example, design patterns can be used in Java, C#, and C++ languages.

But design patterns also have their boundaries, and they are applicable to object-oriented programming languages. For procedural, functional programming languages, it makes no sense to talk about design patterns.

If someone tells you that Fortran design patterns are great, or that they’re learning Lisp design patterns, they’re a fake programmer.

Classification of design patterns

So, how many design patterns did programmers come up with?

In 1995, four of the biggest names in programming co-wrote a book called Design Patterns: Elements of Reusable Object-oriented Software, which translates as Design Patterns: The Foundations of Reusable Object-oriented Software, contains 23 design patterns in all.

This book is an important milestone in software development. The Four authors of this book are known as GoF (Gang of Four) in the industry, so it is also known as GoF Design Patterns.

These 23 design patterns can be divided into major types according to the purpose of design:

Type 1: Creation pattern

The purpose of this type of design pattern is to create objects. For example, the common factory pattern, singleton pattern, is the creation pattern.

The second type: structural model

The purpose of this design pattern is to optimize the structural relationships among different classes, objects and interfaces. The more common agent pattern, decorator pattern, is a structural pattern.

The third type: behavioral patterns

The purpose of this type of design pattern is to better realize the interaction between classes and the execution of algorithms. For example, the strategy pattern and the observer pattern are behavioral patterns.

The chart below summarizes the categories of all 23 design patterns for your collection.

Some people may wonder: many programmers on the Internet always mention 24 design patterns, but you only mention 23 here.

In fact, both of these statements are correct, 24 design patterns are in the original 23 on the basis of the supplement of an empty object pattern, it belongs to the behavior pattern.

In addition, as the field of programming continues to evolve, many new design patterns are being proposed, and there are more than two dozen design patterns in use today.

Producer-consumer patterns, publish-subscribe patterns, etc., are not among the 24 design patterns, but are still very common.

Maybe some friends who do background development will ask: we usually use the MVC pattern, is it also a design pattern?

In my opinion, design patterns study the relationship between classes and objects, interfaces, and solve a particular problem.

MVC, on the other hand, studies the relationships between code modules and provides a package solution. So MVC is an architecture, not a design pattern.

How to learn design patterns

So how can we learn design patterns well?

Just like learning algorithms and data structures, we can learn about the ideas and implementations of various design patterns by reading books and watching online video courses.

The book of entry level, compare recommend cheng Jie teacher “big talk design pattern”.

Head First Design Patterns is recommended for class reading.

In terms of courses, I recommend the Beauty of Design Patterns by Professor Wang Zheng, a geek. At the same time, there are some high-quality design patterns courses on B website, you can watch teaching videos to learn.

At the same time, we can also take a look at some classic framework source code, which also uses a lot of design patterns. For example, the Spring framework, commonly used by Java programmers, uses factory pattern, proxy pattern, singleton pattern, adapter pattern, and so on.

This will help you become familiar with the principles of the framework and deepen your understanding of design patterns.

How do people learn design patterns? Feel free to write in the comments section.