The six main principles of object-oriented:
1. Single Duty principle (Responsibility Principle SRP)
2. Opening and closing principle (Open Close Principle OCP)
3. Richter Replacement principle (Liskov Substitution Principle LSP)
4. Dependency reversal principle (dependence inversion Principle DIP)
5. Interface Isolation principle (Interface segregation Principle ISP)
6. Dimitri principle (Least knowledge Principle LKP)
Single principle of responsibility:
In the case of a class, there should be only one cause for it to change, in short: a class should be a set of highly correlated functions, the encapsulation of data.
A class as far as possible only one responsibility (related), two completely different functions should not be placed in a class, by constantly looking at their own code, according to the specific business, the function of the corresponding splitting of the class.
Opening and closing principle:
objects in the software (classes, modules, functions, etc.) should be open for expansion and closed for modification.
Once the program is developed, the implementation of a class in the program is modified only by mistake, and the new or changed features should be implemented by creating a different class, and the newly created class can reuse the code of the original class in a way that inherits.
Eg: abstract as interface, for extensions, new classes to implement.
The Richter replacement principle:
1. If for each object of type S O1, there is an object of type T O2, yes all program P defined in T is replaced by O2 in all exclusive O1, the behavior of program p does not change, then the type S is a subtype of type T.
2. All references accumulate where the object of its subclasses must be used transparently.
The three main features of object-oriented language: Data abstraction (encapsulation), inheritance, polymorphism
The Richter substitution principle relies on inheritance, polymorphism. Generally speaking: As long as the parent class can appear where the subclass can appear, and the substitution of subclasses will not produce any errors or exceptions, but in turn may not be able to do, there is a subclass of the place, the parent may not be able to adapt. Replace the place used by the parent class with a subclass, a matter polymorphic. This is the principle of the Richter replacement.
Dependency reversal principle:
Refers to a particular form of decoupling, so that high-level modules do not rely on the implementation of low levels of the details of the module.
1. High-level modules should not rely on the underlying modules, both should be dependent on their abstraction.
2. Abstraction should not depend on details.
3. Details should be based on abstraction.
Abstract: Refers to an interface or abstract class
Details: Classes that implement classes, implement interfaces, or inherit abstract classes are details, features: can be instantiated directly
That is to say, the dependencies between the modules occur through abstraction, the implementation of the class does not have a direct dependency, its dependency is through the interface or abstract class to achieve, rely on abstraction and do not rely on concrete implementation, this is the principle of dependency reversal.
Interface Isolation principle:
As the name implies, when designing a class, we need to isolate the various interfaces, using the fewest interfaces, and the dependencies between classes should be based on the smallest interfaces. His goal: The system unlocks the coupling, making it easy to refactor, change, and redeploy.
Dimitri Principle:
An object should have a minimum understanding of other objects, the principle of the proposed also to reduce the coupling degree, easy to maintain the whole system, upgrades and so on.
The closer the relationship between classes and classes, the greater the coupling, and the greater the impact on another class when one class changes, only the talk to Your Immedate Friends communicates with the direct friend.
In fact, these six principles, in general, the purpose is the same: reduce coupling, easy to maintain, expand.
Of course, we can't always think about these six principles when we are programming, which limits our thinking. It takes a lot of practice to get the six principles in order.
The six principles of object-oriented