The first step of code optimization, the single responsibility principle (Single Responsibility Principle). For a Java class, there should be only one reason for its change, that is, a class should be a set of highly related functions and data packages. However, the boundaries of this principle are not so clear, and to a large extent depend on the personal experience of the developer. The biggest problem with the division of the boundaries of a single responsibility is what is the responsibility of the class and how to divide the responsibility of the class.
The single responsibility principle can be seen everywhere in our actual work. For example, in the frameworks MVC and MVP we are more concerned about, the Activity, Fragment, and various Views responsible for page display are only responsible for the display of the UI, and the specific business logic is delivered to Controller or Presenter... Another example is various popular image loading frameworks, in which you can find classes specifically responsible for image loading and image caching. Therefore, the single responsibility principle is the first and most important step of our code optimization.
2. The principle of opening and closingThe Open Close Principle (Open Close Principle) is the most basic design principle in the Java world. It guides us how to build a stable and flexible system. Definition of the opening and closing principle: The objects (classes, modules, functions, etc.) in the software should be open for extension and closed for modification. In the life cycle of the software, when the original code of the software needs to be modified due to changes, upgrades, maintenance, etc., errors may be introduced into the old code that has been tested, and the original system will be damaged. Therefore, when the software needs to be changed At the time, we should be able to achieve changes through extensions, rather than by modifying existing code. But this is not absolute. In the actual development process, it is an ideal situation to upgrade and maintain the original system only through inheritance. Therefore, in actual development, modification of the original code and extension code often exist at the same time. . And how to ensure the correctness of the original software modules and minimize the impact on the original code modules, the answer is to follow the open and close principle as much as possible.
3. The principle of Richter substitutionLiskov Substitution Principle (Liskov Substitution Principle), definition: If for every object a of type ClassA, there is an object b of type ClassB, so that all programs P defined by ClassB are replaced by all objects b with a , The behavior of program P has not changed, then type ClassA is a subtype of type ClassB. However, this narrative is useless. A more direct definition is: all references to the base class must be able to transparently use the objects of its subclasses.
We know that the three major characteristics of object-oriented are: inheritance, encapsulation, and polymorphism. The Richter substitution principle relies on the two characteristics of inheritance and polymorphism. The Richter substitution principle is simply that all references to the base class must be able to use subclass objects. That is to say, as long as the parent class can appear, its subclasses can appear. And replacing it with a subclass will not cause any errors or differences. The user may not need to know whether it is a parent or a child at the root, but the reverse is not possible. The parent may not be able to adapt to the place where the child appears. In the final analysis, the Richter substitution principle is based on these two words: abstract
For example, in Android, the display of the page Window depends on the View, and the View defines a view abstraction. The measure and draw are methods shared by its subclasses. The subclasses can implement various functions and views by overwriting the measure and draw methods. View, any subclass inherited from View can be passed to Window, and Window is responsible for organizing the View and displaying the View on the screen. This is the so-called Richter substitution principle.
The core principle of the Richter substitution principle is abstraction, and abstraction relies on inheritance. In OOP, the advantages and disadvantages of inheritance are quite obvious.
Advantages of inheritance:
Code reuse, reducing the cost of creating classes, each subclass has the methods and properties of the parent class
The subclass and the parent class are basically similar, but they are different from the parent class
Improve code scalability
Disadvantages of inheritance:
Inheritance is invasive, as long as inheritance must have all the properties and methods of the parent class
May cause subclass code redundancy and reduced flexibility, because the subclass must have the methods and properties of the parent class
However, things always have two sides, in accordance with trade-offs and utilization, you need to make choices based on specific circumstances. The use of abstraction in the development process is an important step towards code optimization.
4. Dependence inversion principleDependence Inversion Principle. The Dependence Inversion Principle specifies a specific form of decoupling so that high-level modules do not depend on the implementation details of low-level modules, and dependent modules are inverted. However, the definition is often difficult to understand. The principle of dependency inversion has the following key points:
High-level modules should not rely on low-level modules, both should rely on their abstractions
Abstraction should not depend on details
Details should rely on abstraction
In the Java language, abstraction refers to interfaces or abstract classes, both of which cannot be directly instantiated. The detail is the implementation class, and the class generated by implementing the interface or inheriting the abstract class is the detail. Its characteristic is that it can be instantiated directly. The high-level module is the calling side, and the low-level module is the specific implementation class. The manifestation of the dependency inversion principle in the Java language is that the dependencies between modules occur through abstraction, and there is no direct dependency between implementation classes, and the dependencies are generated through interfaces or implementation classes. In fact, one sentence is: interface-oriented, or abstract programming. If the class is directly dependent on the details, then there is a direct coupling between them. When the specific implementation needs to change, it means that the code of the dependent must be modified at the same time, which limits the scalability of the system.
5. Interface isolation principleInterface Segregation Principle (Interface Segregation Principle), its definition is: the client should not rely on the interface it does not need. Another definition is: the dependency between classes should be established on the smallest interface. The interface isolation principle splits very large and bloated interfaces into smaller interfaces and more specific interfaces so that customers only need to know the methods they are interested in. The purpose of the interface isolation principle is to uncouple the system so that it is easy to refactor, change and redeploy.
The definition is always difficult to understand. Let's use a piece of code to understand the specific use of the interface isolation principle. For example, our common output stream OutputStream needs to be closed after use:
We have seen that the readability of this code is very poor, and all kinds of try catch nesting are extremely simple codes, so how do we solve this problem? There is a Closeable interface in Java:
This interface identifies a closeable object, it has only one close method, and our FileOutputStream also implements this interface. This is easy for us, we can rely on the Closeable interface to implement a tool class:
In actual application, we only need this:
The code is much more concise, and this tool class can be applied to all kinds of closeable objects, ensuring the reusability of the code. The basic principle of the closeQuietly method of CloseUtils is to rely on CLoseable abstraction rather than concrete implementation, and is based on the principle of minimizing dependence. It only needs to know that the object is closable, and nothing else is concerned, which is what we propose The principle of interface isolation.
6. Dimit's principleThe law of Demeter has also become the principle of least knowledge: an object should have the least knowledge of other objects. In other words, a class should know the least about the class that it needs to couple or call. How the internal implementation of the class is implemented has nothing to do with the caller or the dependent. The caller and the dependent only need to know the methods it needs, and everything else Regardless of. The closer the relationship between the class and the class, the greater the degree of coupling, when one class changes, the greater the impact on the other class.
For example, the implementation of the Model layer in the MVP framework, we all know that the Model abstraction provides specific data to the View, and our View layer does not need to know how the data is obtained, even if our back-end interface changes, as long as the data The structure is unchanged, then we do not need to notify the View layer to change.
This invisible Hydrogel film is more suitable for curved screens and various styles of mobile phones. Tempered glass film is not suitable. In this era when most mobile phones are 2.5D, tempered glass film is no way to solve it. Here are some explanations of the problem, through customer responses to the following questions
Material: This invisible soft film is a new type of material called quantum hydrogel film. It is an explosion-proof soft material, but not the plastic film that was produced earlier. This hydrogel film comes with a scratch repair function, which is completely different. type of material
About the bubbles: We guarantee that the edges will not be deformed. The material of the quantum soft film can solve the problem of bubbles generated when the film is warped. But it's alright. The quantum soft film has an automatic exhaust function, and the bubbles will disappear automatically 1-2 days after pasting.
About the size: Due to the actual situation of the mobile phone case and the film, we will reserve a little space (down 0.1mm from the top, bottom, left and right according to the size of the mobile phone) mainly for the convenience of pasting. So that the top film will not appear after the phone case is installed
Hydrogel Screen Protector,Nano Hydrogel Tpu Sheet Film,Cutting Machine Tablet Films,Cellphone Protector Machine
Mietubl Global Supply Chain (Guangzhou) Co., Ltd. , https://www.mietublmachine.com