The guiding light of the code world—six principles of design patterns

Foreword:

A good programmer can't be created by just immersing in code

When writing programs, we have to think about how to write the code more valuable, and what we must need are the six principles of design patterns. These are the necessary principles and skills for a smart programmer. , To put it more simply, learning design patterns can at least reduce your hair loss by half (just kidding)

The design pattern originated in the construction industry rather than the software industry. It
was summarized by Dr. Christopher Alexander, the director of the Center for Environmental Structure of the University of California, the father of Pattern,
and the six principles are the essence of it.

1. The principle of opening and closing

The principle of opening and closing means: open for extension and closed for modification. When the program needs to be expanded, the original code cannot be modified to achieve a hot-swappable effect. In short, it is to make the program extensible, easy to maintain and upgrade.

2. Single responsibility

Core idea: There should be only one reason for class changes. The
benefits are reduced class complexity, improved readability, improved maintainability, increased scalability, and reduced risks caused by changes.

Three. Richter substitution principle

The core idea: where the base class is used, its subclasses can be used arbitrarily to ensure that the subclasses perfectly replace the base class.
In layman's terms: as long as the parent class can appear, the child class can appear. On the contrary, the parent class may not be competent.
Benefits: Enhance the robustness of the program, even if subclasses are added, the original subclasses can continue to run.

4. Dependence inversion principle

The core idea: high-level modules should not rely on low-level modules, both should rely on their abstractions; abstractions should not rely on details; details should rely on abstractions;
benefits: the benefits of dependency inversion are difficult to reflect in small projects. But in large and medium-sized projects, the workload caused by changes in demand can be reduced. Make parallel development more friendly.

Five. Interface isolation principle

Core idea: The dependency between classes should be established on the smallest interface. In
layman's terms: Establish a single interface, don't build a huge and bloated interface, try to refine the interface as much as possible, and have as few methods as possible in the interface. In other words, we need to establish a dedicated interface for each class instead of trying to create a huge interface for all classes that depend on it to call.

Six. Dimit's Law

The core idea: decoupling between classes.
In layman's terms: The less a class knows about the classes it depends on, the better. Since we started with programming, we have known the general principles of software programming: low coupling and high cohesion. Whether it is process-oriented programming or object-oriented programming, only by making the coupling between the various modules as low as possible can the code reuse rate be improved. The advantages of low coupling are self-evident, but how to program to achieve low coupling? That is exactly what Dimit's Law is about to accomplish.

Endless learning hope the article will be helpful to everyone

Guess you like

Origin blog.csdn.net/weixin_44693109/article/details/108888649