One, the first step to optimize the code: a single responsibility principle
English Name: SingleResponsibility Principle (SRP)
Definition: as far as a class is concerned, there should be only one cause for it to change.
In simple terms, a class should be a set of highly correlated functions and data encapsulation.
Example code:
Ii. making the procedure more stable and flexible: the principle of opening and shutting
English name:Open Close Principle (OCP)
Definition: objects in the software (classes, modules, functions, etc.) should be open to the extension, when the modification is closed.
In the software life cycle, because of changes, upgrades and maintenance and other reasons need to modify the software's original code, may introduce errors into the original already tested old code, destroy the original system.
Therefore, when software requirements change, we should try to extend the way to achieve change, rather than by modifying the existing code to achieve. Of course, in the real development process, only through the inheritance of the way to upgrade,
The maintenance of the original system is only an idealized vision, so in the actual development, the modification of the original code, extension code is often present at the same time.
code example:
Third, build a more scalable system: the Richter Replacement principle
English name:Liskov Substitution Principle (LSP)
The first definition: if for each object of type S O1, there is an object of type T O2, so that all program P defined in T is replaced by O2 for all objects O1, the behavior of program p does not change.
Then the type S is a subtype of type T.
The second definition: all references to base classes must be able to transparently use objects of their subclasses.
The core principle is abstract , abstract and dependent on inheritance ;
The advantages of inheritance are:
① code reuse, reducing the cost of creating classes, with each subclass having methods and properties of the parent class
The ② subclass is similar to the parent class, but differs from the parent class
③ Improve Code Extensibility
Disadvantages:
① inheritance is intrusive, as long as inheritance must have all the properties and methods of the parent class
② may cause subclass code redundancy, reduced flexibility, because subclasses must have properties and methods of the parent class
code example:
Iv. ability to change the project: dependency inversion principle
English name: Dependence inversion Principle (DIP)
Definition: Refers to a specific decoupling form, so that the high-level module does not rely on the implementation of low levels of the details of the purpose of the module
Key points:
① high-level modules should not rely on low-layer modules, both should rely on their abstraction
② abstraction should not depend on details
③ details should be dependent on abstraction
In Java, abstraction refers to an interface or abstract class, both of which cannot be instantiated directly;
The detail is the implementation of the class, the implementation of interfaces or inherited abstract class is the result of the class is the details, characterized by, can be directly instantiated, that is, you can create an object through the keyword new.
The high-level module is the call side;
A low-level module is a concrete implementation class.
Use:
Dependencies between modules occur through abstraction, and there is no direct dependency between classes, and their dependencies are generated through interfaces or abstract classes.
To put it simply: interface-oriented (interface or abstract class) programming, or for abstract programming.
Example code:
Five, the system has higher flexibility: the principle of interface isolation
English name: Interfacesegregation Principle (ISP)
The first definition: The client should not rely on interfaces it does not need
The second definition: dependencies between classes should be based on the smallest interface
The goal is to decouple the system so that it is easy to refactor, change, and redeploy, and to split a large, bloated interface into smaller, more specific interfaces.
Example Code:
Vi. Better scalability: Dimitri Principles
English name: Law of Demeter (LOD)
Definition: An object should have minimal knowledge of other objects,
In layman's words, a class should know the least about the classes that it needs to be coupled or called, and how the inner implementation of the class does not matter to the caller or the relying person, the caller or the relying person needs only to know what it requires.
Method can be, the other can be no tube.
The road to flexible software--the six principles of object-oriented