Refactoring ----Move Method(将方法移至常被调用的class中)
时间:2011-02-16 来源:无为是最高
/// <summary>
/// 简单的银行帐号类
/// </summary>
public class BankAccount
{
public BankAccount(int accountAge, int creditScore,AccountInterest accountInterest)
{
AccountAge = accountAge;
CreditScore = creditScore;
AccountInterest = accountInterest;
}
public int AccountAge { get; private set; }
public int CreditScore { get; private set; }
public AccountInterest AccountInterest { get; private set; }
}
/// <summary>
/// 利息
/// </summary>
public class AccountInterest
{
public BankAccount Account { get; private set; }
public AccountInterest(BankAccount account)
{
Account = account;
}
public double InterestRate
{
get { return CalculateInterestRate(); }
}
public bool IntroductoryRate
{
get { return CalculateInterestRate() < 0.05; }
}
/// <summary>
/// 方法存活在被常调用的类中,其逻辑和此类的关系更为密切。
/// </summary>
/// <returns></returns>
public double CalculateInterestRate()
{
if (CreditScore > 800)
return 0.02;
if (AccountAge > 10)
return 0.03;
return 0.05;
}
}
可维护性是衡量代码质量重要的标准之一,此重构值得考虑!
相关阅读 更多 +