How to refactor Android code

Tell me about the ideas of Android code refactoring

1. New extraction function

<1> Complex logic is split into functions
<2> Large functions are split into small functions

2. Transfer function between parent class and child class

<1> Extract the same logic of the subclass x as the parent class function
<2> Extract the non-universal logic of the parent class into the functions of the individual subclasses used

3. Transfer function between classes

If you repeatedly call coupling between classes, you can consider extracting all the coupling logic into a class, which conforms to the single principle

4. Split the complex class into several classes

According to a single principle, for
example, the image download class is disassembled into three:
1.catch cache strategy class
2.Download download class
3.ImageDownloader image loading class

5. Logic judgment (if or switch) changes to state mode

For example, the behavior of the music player class is: paly (), pause (), stop () corresponds to three states, if you go to the method to determine the logic is not very clear. You can implement three state classes and implement three methods respectively, and the caller only cares about the state change.

Published 589 original articles · praised 1040 · 1.87 million views

Guess you like

Origin blog.csdn.net/u010321471/article/details/105697987