With the C # language foundation, we still need to learn the design pattern principle before learning the design pattern. This is the premise of the design pattern and is the criterion for us to judge whether the design is reasonable. So in this blog, I will first learn and summarize the principles of the design model.
I. Single Responsibility Principle (Singleresponsibility principle, shortSRP ):
Accurate explanation: for a class, there should be only one reason for its change.
Popular Understanding: like P3, which is used to listen to songs, cameras are used to take photos, TVs are used to watch TV, and radios are used to listen to broadcasts ...... They all have specific functions and are doing very well, instead of having all functions on some mobile phones, but that function is not doing well.
Consequence: If a class has too many responsibilities, it is equivalent to coupling these responsibilities. A change in responsibilities may weaken or suppress the class's ability to complete other functions, this coupling will lead to a fragile design. When a change occurs, the design will suffer unexpected damages.
Ii. open-closed principle (OCP, open closed principle ):
Accurate explanation: software entities (classes, modules, functions, and so on) should be extensible, but cannot be modified.
General understanding: it can be understood from two aspects: 1. software entities are open for extension; 2. software entities are closed for changes (closed for modification ). that is to say, for software entities, we can add some functions, but cannot modify the implemented functions. It can also be said that in the face of requirementsProgramBy adding a newCodeInstead of modifying the existing code.
Importance: the open-closed principle is the core of object-oriented design. Following this principle can bring about the huge benefits that object-oriented technology claims, that is, maintenance, scalability, and reusability, good flexibility. Of course, it is also not a good idea to deliberately Abstract Every part of an application. Rejecting immature abstractions is equally important as abstracting itself.
Iii. Dependency inversion principle (DIP, dependence inversion principle ):
Accurate explanation: A. The high-level module should not depend on the underlying module. Both of them should depend on abstraction. B. abstraction should not depend on details, and details should depend on abstraction.
In general: We need to program the interface, not the implementation programming. A very simple example is our computer in life. If our hard disk breaks down, change the hard disk. If the motherboard breaks down, change the motherboard, and if the memory breaks down, change the memory. That is to say, these parts are not mutually dependent, but depend on the plug-in. The dependency reversal principle of software is to achieve this effect. This is what we often call high cohesion and low coupling.
Purpose: Dependency inversion is actually an identifier of object-oriented design. It doesn't matter which language is used for programming. If the programming is based on abstract programming rather than detailed programming, that is, all dependencies in the program are terminated from abstract classes or interfaces, that is, object-oriented design.
Iv. liskov substitution principle, LSP ):
Accurate explanation: child types must be able to replace their parent types;
In general: If a software entity uses a parent class, it must be applicable to its Child class, and it cannot detect the difference between the parent class object and the Child class object. That is to say, in the software, the parent class is replaced with its Child class, and the behavior of the program remains unchanged.
Purpose: with this principle, the parent class can be reused only when the child class can replace the parent class and the functions of the software unit are not affected, child classes can also add new behaviors based on parent classes. This shows the effect of inheritance.
5. Law ofdemeter is also known as LKP ):
Accurate explanation: If two classes do not need to communicate with each other directly, they should not interact directly. If one class needs to call a method of another class, it can be forwarded by a third party. That is to say, an object should have as little understanding as possible about other objects and should not talk to strangers.
In plain terms: for example, a company's departments are connected by managers to coordinate their work, and there is no need to deal with each other between the two departments. In programming, each class should minimize the access permissions of members.
The fundamental idea is to emphasize the loose coupling between classes, and the weaker the coupling between classes, the more conducive to reuse.
6. compositionaggregation Reuse Principle, Carp)
Accurate explanation: Try to use synthesis/aggregation, and try not to use class inheritance. It is to use some existing objects in a new object to make it a part of the new object. New objects reuse existing functions by delegating these objects.
Advantage: preferential use of object merging/aggregation will help you to keep each class encapsulated and concentrated on a single task, so that class and class inheritance will remain relatively small, and it is unlikely that it will grow into a giant. The goal is high cohesion and low coupling.
To sum up, we should learn the six principles of the design model and also the important principles of programming. This is what we need to abide by. Only by ensuring these principles, our programs, to achieve the fundamental objective of object-oriented: Maintenance, scalability, reusability, and good flexibility.