重构之Pull Up Field ,Push Down Field 与Rename(method,class,parameter)笔记
Pull Up Field
两个子类中包含重复的常量,为了提高复用性将字段上移到基类中。
public abstract class Account
{
}
public class CheckAccount : Account
{
private decimal _minCheckBalance = 5m;
}
public class SaveAccount : Account
{
private decimal _minSaveBalance = 5m;
}
{
}
public class CheckAccount : Account
{
private decimal _minCheckBalance = 5m;
}
public class SaveAccount : Account
{
private decimal _minSaveBalance = 5m;
}
提高复用性,如下
public abstract class Account
{
//protected 该类以及子类有特有的
protected decimal _minCheckBalance = 5m;
}
public class CheckAccount : Account
{
}
public class SaveAccount : Account
{
}
{
//protected 该类以及子类有特有的
protected decimal _minCheckBalance = 5m;
}
public class CheckAccount : Account
{
}
public class SaveAccount : Account
{
}
Push Down Field
基类中的一个字符串字段只被一个子类使用,因此可以进行下移。只要没有其他子类使用基类的字段时,就应该立即执行该重构。
public abstract class Task
{
protected string _resolution;
}
public class BugTask : Task
{
}
public class FeatureTask : Task
{
}
{
protected string _resolution;
}
public class BugTask : Task
{
}
public class FeatureTask : Task
{
}
修改后
public abstract class Task
{
}
public class BugTask : Task
{
//为BuugTask特有
private string _resolution;
}
public class FeatureTask : Task
{
}
{
}
public class BugTask : Task
{
//为BuugTask特有
private string _resolution;
}
public class FeatureTask : Task
{
}
Rename(method,class,parameter)
这个重构只需将名称修改得更具描述性、更容易传达其含义即可。
代码
public class Emp
{
public string FN { get; set; }
public decimal ClcHrP()
{
return 0m;
}
}
//修改后更加清晰,一目了然
public class Employee
{
public string FirstName { get; set; }
public decimal CalculateHourPay()
{
return 0m;
}
}
{
public string FN { get; set; }
public decimal ClcHrP()
{
return 0m;
}
}
//修改后更加清晰,一目了然
public class Employee
{
public string FirstName { get; set; }
public decimal CalculateHourPay()
{
return 0m;
}
}