In the development process, we should try to ensure the cleanliness of the code, the method as short as possible, a way to do only one thing, and the class should not be too complex. Most of the time, we will always add code to a class, and eventually, this class may become a "monster". In iOS development, we can use category to split the class.
As shown, I want to implement a function, the upper rectangle is a toolbar, for the answer to provide some auxiliary functions, the lower rectangular box to display the title and answer.
I named this class PRACTICEVC, I draw the class diagram (UML is my own blind, I do not know this painting is right).
It contains a answertopview and a uiscrollview,uiscrollview and contains a bunch of things.
Enter PRACTICEVC, and later, I need:
1. Request the problem data, analysis
2. Set the Answertopview Click event
3. Set Uiscrollview (display title, switch topic, set answer card)
4. Submit the user's reply data.
Although, I have encapsulated two classes Answertopview and Displayarea, in PRACTICEVC only need to implement the Proxy method, indicating what to do with the button on the toolbar, initialize the Displayarea,answercardview. But I still feel very bloated, this class is very long, good hundreds of lines. I have obsessive-compulsive disorder, I want to split this class.
iOS category use more, we often achieve a variety of xx+helper, very useful. Category you can add methods to the class (which may overwrite the original method of the Class) and properties (some books think that it is only appropriate to add attributes to the anonymous category, and the other category should not add attributes). We can use this feature to split the classification, split it into different categories, look at the system's class header file, basically using a lot of category, I believe there is one of the reasons is to avoid the class is too complex.
To this end, I will request the topic and submit the user answer data into a class practicevc+pvcserver, the above Answertopview management to split into another class Practicevc+pvctopview. The attributes I will need to use are all declared in PracticeVC.h, only methods in class category. After this split, PRACTICEVC has 180 rows, Pvctopview has 80 rows, and Pvcserver has 100 rows. If the function changes, we only need to modify the corresponding function of the file on it. After this split, for the inheritance, there will be no effect, in the PRACTICEVC import these two categories, subclasses also have these methods.
Copyright Notice: Welcome to comment and reprint, but please keep the source!
IOS uses category to split complex classes