YourProgramA field is used by another class other than its resident class.Create a new field in the target class, modify all users of the Source Field, and change them to a new field.
Motivation: Moving status and behavior between classes is an essential measure in the refactoring process. As the system develops, you will find that you need a new class and need to drag the existing work responsibilities to the new class. This week's seemingly reasonable and correct design decisions may not be correct by the next week. This is no problem. If you have never encountered such a situation, it will be a problem.
If you find that more functions are used for a field in another class other than its resident class, you should consider moving this field. The above-mentioned "use" may be carried out indirectly by setting the value/value function. It is also possible to move the user (a function) of this field, depending on whether the interface needs to be kept unchanged. If these functions seem to be suitable for staying in the same place, choose to move the field.
When using extract class (extract class), you may also need to move fields. In this case, you can first move the field and then move the function.
Practice: 1. If the field access level is public, use encapsulated field to encapsulate them. If you may move functions that frequently access this field, or if many functions access a field, using self encapsulate field (self-encapsulated field) may be helpful.
2, Compilation, and testing.
3, Create fields with the same source field in the target class, and create the corresponding set value/value function at the same time.
4, Compile the target class.
5Determine how to reference the target object in the source object. First, check whether there is a ready-made field or function to help you get the target object. If not, you can easily create such a function. If not, create a new field in the source class to store the target object. This may be a permanent modification, but you can also make it temporary. This field may be removed from subsequent refactoring.
6, Delete the source field.
7Replace all references to the Source Field with calls to a target function. To read the variable, replace the reference to the source field with the call to the Set-value function. If the source field is not private, you must search for reference points of the Source Field in all subclasses of the source class and replace them accordingly.
8, Compilation, and testing.