I read "big talk Design Patterns" and extract the principles of the design patterns mentioned in this book for future use.
I. Single Responsibility Principle
For a class, there should be only one reason for its change. 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 fulfill other responsibilities. This coupling will lead to a fragile design. When a change occurs, the design will suffer unexpected damages. What the software really wants to do is to discover its responsibilities and separate them from each other. If you can think of more than one motive to change a class, this class has more than one responsibility, and you should consider the separation of duties of the class.
Ii. principles of openness and Closure
Software entities (classes, modules, functions, etc.) should be extensible, but cannot be modified. That is, they are open to extensions and closed to changes. The use of the open-closed principle can make the software remain relatively stable in the face of changes in demand, so that the software can continuously launch new versions after the first version. In other words, when faced with requirements, changes to the program are made by adding new Code, rather than changing the existing code. No matter how closed the module is, there will be some changes that cannot be closed to it. Since it cannot be completely closed, the designer must choose which change should be closed for the module he designed. He must first guess the most likely types of changes, then construct an abstraction to isolate these changes and take immediate action when the changes occur. Therefore, when we first write the code, we assume that the changes will not happen. When a change occurs, we create an abstraction to isolate similar changes that occur later.
The open-closed principle is the core of object-oriented design. Following this principle can bring about the huge benefits of object-oriented technology, that is, it can be maintained, scalable, reusable, and flexible. Developers should only abstract those parts that present frequent changes in the program. Then, it is also not a good idea to deliberately abstract each part of the application. Rejecting immature abstractions is just as important as abstraction itself.
Iii. Li's replacement principle
Definition: child types must be able to replace their parent types. That is to say, 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, in the software, replace the parent class with its Child class, and the behavior of the program remains unchanged. Only when the child class can replace the parent class and the functions of the software unit are not affected can the parent class be reused. The Child class can also add new behaviors based on the parent class.
Iv. Dependency inversion principle
1. High-level modules should not depend on low-level modules. Both of them should depend on abstraction.
2. abstraction should not depend on details. Details should depend on abstraction.
In other words, it is necessary to program the interface, not to implement programming. The dependency inversion principle is a sign of object-oriented design. It is not important to write a program in which language. 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, and vice versa, procedural design.
It is with the Li's replacement principle that the dependency inversion principle becomes possible. Because the child type can be replaced, the module that uses the parent class can be expanded without modification.
V. dimit's Law
It is also called the minimum knowledge principle. If the two classes do not need to communicate with each other directly, the two classes should not interact directly. If one class needs to call a method of another class, it can be forwarded by a third party. The premise of this rule is that in the structure design of the class, each class should minimize the access permissions of members, that is, a class should wrap its own private State, do not disclose fields or behaviors that other classes know. The fundamental idea of the dimit Law emphasizes the loose coupling between classes. The weaker the coupling between classes, the more conducive to reuse. A class with weak coupling is modified and will not affect related classes.