Refactoring-improving existing code
Motivation
In case of attachment or unclear responsibilities, move the behavior to the correct class.
Mechanics
1. Check all the features used in the source function (including field and function)
2. Check whether the subclass and parent class have defined the function.
3. Delegate first, and then decide whether to replace the call to the source function with the call to the target function based on the actual situation.
The eclipse refactor menu directly contains the move option, which can be used directly to complete the move method reconstruction. However, there are still some detailed differences in actual application. The following details:
Default situation: [Move] method. The source class must use the target class object as the member variable; otherwise, the target object cannot be found. In addition, after moving, the source class object is always used as the function parameter by default.
Special Case 1: The target class is the internal class of the source class. Actually, the copy code method is used.
Special Case 2: objects in the source class already exist in the target class. The [Move] + [rename] + [change method signature] method is used.
Special Case 3: Sometimes, you only need to pass in a member variable of the source class as a function parameter, instead of the entire source class object. [Extractlocal variable] + [introduce parameter]/[Extract Method] + [Move],
You can also use[Extractlocal variable] + [Extract Method] + [inline] + [Move]
double bankCharge(Account account) {double result = 4.5;if (account._daysOverdrawn > 0)result += account.overdraftCharge();return result;}
// 1. Replace [Extract local variable] with a local variable in the function to be moved, and add a value assignment statement such as "localvariable = _ field.
PublicclassAccount {
DoubleOverdraftcharge (){
Finalintdaysoverdrawn= _ Daysoverdrawn;
If(_ Type. ispremium ()){
DoubleResult = 10;
If(Daysoverdrawn> 7)
Result + = (Daysoverdrawn-7) x 0.85;
ReturnResult;
}Else
Returndaysoverdrawn* 1.75;
}
// 2. Add the value assignment statement "final int daysoverdrawn = _ daysoverdrawn;", select field "_ daysoverdrawn", apply introduce parmeter, and pass this field as a function parameter.
Public classAccount {
DoubleOverdraftcharge (Int dayoverdrawn){
Final
Int
Daysoverdrawn =
_ Daysoverdrawn;
Final intDaysoverdrawn =Dayoverdrawn;
If(
_ Type. ispremium ()){
// 3. Delete the value assignment statement "localvariable = _ field ".
Public classAccount {
DoubleOverdraftcharge (Int dayoverdrawn){
Final
Int
Daysoverdrawn =
Dayoverdrawn;
If(
_ Type. ispremium ()){
========================================================== ========================================================== ==========
There is also a more general adjustment method. Its core idea is to delegate the old method interface to the new method, then apply inline to replace all old methods with the new method, and finally move.
The first step is the same as above. The second step is to use [Extract Method] to extract the value assignment statement "final int daysoverdrawn = _ daysoverdrawn;" the following sections are extracted as a new function with the same name as the target function, both are overdraftcharge.
Public classAccount {
DoubleOverdraftcharge (){
Final intDaysoverdrawn = _ daysoverdrawn;
Return overdraftcharge (daysoverdrawn );
}
Private double overdraftcharge (final int daysoverdrawn ){
If (_ type. ispremium ()){
Double result = 10;
If (daysoverdrawn> 7)
Result + = (daysoverdrawn-7) * 0.85;
Return result;
} Else
Returns daysoverdrawn * 1.75;
}
// 3. Replace the target function with [inline]
Apply [inline] To daysoverdrawn first
DoubleOverdraftcharge (){
Final
Int
Daysoverdrawn =
_ Daysoverdrawn;
ReturnOverdraftcharge (_ Daysoverdrawn);
}
Then apply [inline] To overdraftcharge ().
Double
Overdraftcharge (){
Return
Overdraftcharge (
_ Daysoverdrawn
);
}
The original calling of overdraftcharge () is automatically adjustedOverdraftcharge (_ daysoverdrawn)
Result + = overdraftcharge (); --> result + = overdraftcharge (_ daysoverdrawn );
Over
The complete source code can be downloaded here