The six principles
-
Single responsibility principle
-
Richter’s substitution principle
-
Dependency inversion principle
-
Interface Isolation Principle
-
Demeter principle
-
The open closed principle
Single responsibility
-
Concept: Categorize functionality and decouple code
-
Chestnut: A network request framework is roughly divided into: request class, cache class, configuration class; Instead of mixing these three functions together, they must be divided into three classes to implement different functions
Replacement on the Richter scale
-
Concept: When inheriting a class, try not to delete or modify references to the methods of the parent class, and try not to overload the methods of the parent class, except to extend some new functions
-
Every class is a subclass of Object, and Object has a toString() method. If a subclass overrides the method and returns NULL, the next level of inheritance in that subclass returns NULL, then this problem may not be considered when maintained by different developers, and will probably crash the program
Dependency inversion
-
Concept: A high-level module does not depend on the details of a low-level module. A high-level module relies not on details but on abstractions (not on concrete classes, but on interfaces)
-
A web framework can use either the efficient OkHttp framework or a native API to meet the needs of different developers. So-called turnip highly uneven, so how to switch, this time need to an interface programming ideas, some of the network request methods in an interface, and then create OkHttp respectively and native API interface implementation class, of course, also facilitate subsequent other developers to extend other network framework of the application
Interface segregation
-
Concept: The interface methods should be rationalized and simplified to avoid interface overcrowding
-
Chestnut: in the actual development, often in order to save time, could be more functional methods smoke into an interface, in fact this design concept is not correct, it will make the interface in the bloated state, this is where the reasonable split the interface methods, and extracted into a separate interface, avoid the original interface bloated code to understand the difficulties caused
Di milt | at least know
-
Concept: One object should know the least about other objects; A class should know the least about the class it needs to couple or call. It doesn’t matter to the caller or the dependent how complex the inner implementation of the class is. The caller or the dependent only needs to know the method he needs, and nothing else. The closer the relationship between classes, the greater the degree of coupling, when one class changes, the greater the impact on the other class. Correspond only with direct friends. Each object is bound to have a coupling relationship with other objects, and the coupling between two objects becomes a friend relationship. There are many types of this relationship, such as composition, aggregation, dependency and so on.
-
Chestnut: In commonly used frame, frame of the developers take a class for external calls, and the main kind of like a mediation to call framework inside other classes, just framework inside other classes are generally inaccessible (call), the framework will abide by the principle of di milt, other developers only care about the method called, You don’t really care how the functionality is implemented, right
Open and close
-
Concept: Classes, modules, and functions should be open for extension and closed for modification
-
Chestnut: In the software life cycle, because change, upgrade and maintenance of need for software to modify the original code, may introduce errors, to the old code may also make us have to refactor the whole function, and the need to the original code after the test, the whole process had a great influence on the development cycle, this time you need to open closed principle to solve this problem
conclusion
-
The single responsibility principle tells us to implement a class with a single responsibility
-
Richter’s substitution tells us not to break the inheritance system
-
The dependency inversion principle tells us to program toward interfaces
-
The interface isolation principle tells us to keep interfaces simple and simple when designing them
-
Demeter’s principle tells us to reduce coupling
-
The open closed principle is the general principle that tells us to be open to expansion and closed to modification
Exquisite article
There are six principles of design patterns
Six basic principles of object orientation – Network engine switching
If not accurate, please help to point out
Give this article a thumbs up if it helps you