Understanding and understanding of software design (principles)

Version 1: It is the simplest and most rigid thing, there is no way to reuse it. It's just a simple print out of the bar value, and there is no coupling contact, which is a strong coupling.
Version 2: It is not as coupled as version 1, and decoupled a lot, with the awareness of defining variables.
Version 3: Continue decoupling on top of version 2, and basically encapsulate the calculation results.
Each subsequent version has been decoupled to varying degrees.
These all show that our principles are very important. Here are a few principles of design patterns.
So what are the principles of design patterns?

Single Responsibility Model
Open-Closed Principle
Reliance Inversion Principle
Li Substitution Principle
Dimit Method Principle
Composition/Aggregation Reuse Principle

What is the single responsibility principle?

Single Responsibility Principle (SRP): For a class, there is only one reason for its change.
Example: Let's talk about people. There are many things to do every day at home, so we say that we should do one thing whole-heartedly, which is actually in line with this principle.
Xiao Ming wanted to do his homework and watch TV dramas, so he chose to study and watch TV dramas. Then Xiao Ming's state changed, one would be difficult to learn, and one would be attracted by the plot. The last two things were not done well. I forgot the plot, and I didn't learn it. So we need to follow the single responsibility principle. Let your heart only learn

Conclusion: By analogy, if a class has too many responsibilities, it is equivalent to coupling these responsibilities together. A change in one responsibility may weaken or inhibit the ability of this class to perform other responsibilities.

What is the open-closed principle?

Open-closed principle: software entities (classes, modules, functions, etc.) should be extensible, but not modifiable.
Example: Continuing the above example, learning is a very important thing for Xiao Ming, but he still wants to watch a drama, but he can’t modify the plan he studied before, so he can expand his own. . Use the break time between studies to relax and watch a drama. Xiao Ming in this way complies with the open-closed principle.
Conclusion: In the face of demand, changes to the program are made by adding new code, rather than changing the existing code.

What is the principle of dependency inversion?

Dependency inversion principle:
① High-level modules should not depend on low-level modules. Both should rely on abstraction (abstraction is an interface or abstract class).
② Abstraction should not rely on details. Details should rely on abstraction.
In fact, it means that it should be aimed at interface programming, not implementation programming.
Example: Give an example of our project. There are many projects in the improvement class. The project team leader should be regarded as the senior staff, and then the team members are the lower staff. The project team leader distributes the tasks, and the team members work according to the tasks. As long as there is no problem with the task, the person in charge of the task can be replaced. They are all achieved through tasks, which means that they all rely on an interface.

What is the principle of Li substitution?

Li Substitution Principle (LSP): Subtypes must be able to replace their supertypes. In fact, it means that if a software entity uses the parent class, it must be used in the subclass, and it cannot detect the difference between the parent class object and the subclass object. In other words, in the software, all the parent classes are replaced with his subclasses, and the program remains unchanged.
Example: For example, there is a computer that needs to be moved. And college students and teachers are both subclasses of people. I used to find someone Xiao Ming to move the computer, but Xiao Ming asked for leave today, and a teacher, Xiao Li, moved the computer. As long as the work of moving the computer is completed, no change will be felt.

What is the principle of Dimitra law?

迪米特法原则:如果两个类之间不必彼此直接通信,那么这两个类就不应当发生直接的相互作用。如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用。
举例:平时我们的网线坏了,那么你是找网线部门的某个人来修呢?还是找网线部门呢?其实我们只需要发个审批网线部门就会找人来给我们修,我们不需要知道是谁来修。

什么是合成/聚合复用原则呢?

合成/聚合复用原则(CARP):尽量使用合成/聚合,尽量不要使用类继承。

Guess you like

Origin blog.csdn.net/make_1998/article/details/107800125