YourProgramIn, there is a function that communicates with another class other than the resident class: Call the latter, or be called by the latter.Create a new function with similar behavior in the class most commonly referenced by this function. Program the old function as a pure delegate function, or completely remove the old function.
Motivation: "moving function" is the pillar of refactoring theory. If a class has too many behaviors, or if a class is highly coupled with another class, you need to move the function. Through this method, classes in the system can be simpler, and these classes will eventually implement system delivery more cleanly.
Browse all functions of the class and find out the function: the number of times that another object is used is more than the number of times that the object is resident. This check should be performed once some fields are moved. Once a function that may be moved is found, observe the end that calls it and the end that it calls. It has inherited any of its redefinition functions in the system. Then, the moving path is determined based on "The function communicates more with which object.
This is often not an easy decision. If you are not sure whether to move a function, continue to observe other functions. Moving other functions makes this decision easier. Sometimes, even if you move other functions, it is difficult to make a decision on the current function. In fact, this is no big deal. If it is really difficult to make a decision, maybe "moving this function or not" is not that important. Therefore, it can be changed by instinct.
Practice: 1. Check all the features (including fields and functions) used by the source function in the source class and check whether they should be removed as well. If a feature is used only by the function you intend to move, it should be moved together. If other functions use this feature, you can consider moving all the functions using this feature. Sometimes, moving a group of functions is easier.
2Check the subclass and super class of the source class to see if other declarations of the function exist. If there are other statements, you may not be able to move them. Unless the target category also shows polymorphism.
3Declare this function in the target class. You can select a new name for this function-a name that is more meaningful to the target class.
4, Convert the source functionCodeCopy to the target function. Adjust the latter so that it can run normally in the new home. If the target function uses the features in the source class, you have to decide whether to reference the source object from the target function. If the reference mechanism is not used in the target class, the reference of the source object is used as a parameter and passed to the newly created target function.
5, Compile the target class.
6Determines whether any source function correctly references the target object. There may be a ready-made field or function to help you get the target object. If not, you can easily create a function. If the problem persists, you must create a new field in the source class to save the object. This may be a permanent modification, but you can also make it temporary, because other refactoring projects may remove this new field.
7Modify the source function to make it a pure delegate function.
8Compile and test.
9Determine whether to delete the source function, or retain it as a delegate function. If you often need to reference the target function in the source object, it is easier to keep the source function as a delegate function.
10If you want to remove the source function, replace all calls to the source function in the source class with calls to the target function. You can compile and test it once without modifying a reference point. You can also use "Search/Replace" to remove all reference points.
11, Compilation, and testing.