一、Preserve Whole Object(保持对象完整)
动机(Motivation)
要向某个方法内传递若干个值,可以改使用传递整个对象。
示例
1 |
int low = DaysTempRange().GetLow(); |
2 |
int high = DaysTempRange().GetHigh(); |
3 |
withinPlan = Plan.WithinRange(low, high); |
改为
1 |
withinPlan = Plan.WithinRange(DaysTempRange()); |
二、Replace Parameter with Methods(以函数取代参数)
动机(Motivation)
对象调用某个函数,并将所得结果作为参数,传递给另一个函数。而接收该参数的函数应直接调用该函数。
示例
01 |
public double GetPrice() |
02 |
{ |
03 |
int basePrice = _quantity * _itemPrice; |
04 |
double discountLevel = GetDiscountLevel(); |
05 |
double finalPrice = DiscountedPrice(basePrice, discountLevel); |
06 |
} |
07 |
08 |
private double DiscountedPrice( int basePrice, double discountLevel) |
09 |
{ |
10 |
|
11 |
} |
改为
01 |
public double GetPrice() |
02 |
{ |
03 |
int basePrice = _quantity * _itemPrice; |
04 |
double finalPrice = DiscountedPrice(basePrice); |
05 |
} |
06 |
07 |
private double DiscountedPrice( int basePrice) |
08 |
{ |
09 |
double discountLevel = GetDiscountLevel(); |
10 |
} |
1 |
|
1 |
|
三、Introduce Parameter Object(引入参数对象)
动机(Motivation)
以一个对象取代这些参数。
示例
1 |
public string GetUserInfo( int ID, string name, string age, string sex) |
2 |
{ |
3 |
4 |
} |
改为
01 |
public string GetUserInfo(User user) |
02 |
{ |
03 |
|
04 |
} |
05 |
06 |
07 |
08 |
public class User |
09 |
{ |
10 |
public int ID { get ; set ; } |
11 |
12 |
public int Name { get ; set ; } |
13 |
14 |
public int Age { get ; set ; } |
15 |
16 |
public int Sex { get ; set ; } |
17 |
18 |
} |
四、Remove Setting Method(移除设值函数)
动机(Motivation)
去掉该值域的所有设置函数(setter),class中的某个值域,应该在对象初创时被设值,然后就不再改变。
示例
01 |
public class Account |
02 |
{ |
03 |
private string _ID; |
04 |
public Account( string id) |
05 |
{ |
06 |
SetID(id); |
07 |
} |
08 |
09 |
private void SetID( string id) |
10 |
{ |
11 |
_ID = id; |
12 |
} |
13 |
} |
改为
1 |
public class Account |
2 |
{ |
3 |
private string _ID; |
4 |
public Account( string id) |
5 |
{ |
6 |
_ID = id; |
7 |
} |
8 |
} |
五、Hide Method(隐藏某个函数)
动机(Motivation)
一个函数,从来没有被其它任何class用到,将这个class改为private。
示例
1 |
public string GetUserInfo(User user) |
2 |
{ |
3 |
return null ; |
4 |
} |
改为
1 |
private string GetUserInfo(User user) |
2 |
{ |
3 |
return null ; |
4 |
} |
六、Replace Constructor with Factory Method(以工厂函数取代构造函数)
动机(Motivation)
在创建对象时不仅仅是对它做简单的建构动作(simple construction),将constructor(构造函数)替换为factory method(工厂函数)
示例
1 |
public Account( string id) |
2 |
{ |
3 |
_ID = id; |
4 |
} |
改为
1 |
public static Account Create( string id) |
2 |
{ |
3 |
return new Account(id); |
4 |
} |
七、Encapsulate Downcast(封装[向下转型]动作)
动机(Motivation)
某个函数返回的对象,需要由函数调用者执行[向下转型](downcast)动作。
示例
1 |
public object LastReading() |
2 |
{ |
3 |
return Readings.LastElement(); |
4 |
} |
改为
1 |
public Reading LastReading() |
2 |
{ |
3 |
return (Reading)Readings.LastElement(); |
4 |
} |
1 |
|
1 |
|
八、Replace Error Code with Exception(以异常取代错误码)
动机(Motivation)
某个函数返回一个特定的代码(special code),用以表示某种错误情况。
示例
01 |
private int _balance; |
02 |
03 |
public int Withdraw( int amount) |
04 |
{ |
05 |
if (amount > _balance) |
06 |
return -1; |
07 |
else |
08 |
{ |
09 |
_balance -= amount; |
10 |
return 0; |
11 |
} |
12 |
} |
改为
1 |
private int _balance; |
2 |
3 |
public int Withdraw( int amount) |
4 |
{ |
5 |
if (amount > _balance) |
6 |
throw new Exception( "error" ); |
7 |
_balance -= amount; |
8 |
} |
步步为营 .NET 代码重构学习笔记系列
步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method)
步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method)
步步为营 .NET 代码重构学习笔记 四、临时变量(Temporary Variable)
步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm)