重构第2天:方法搬移(Move Method)

     现在就重构来说是非常普通的,虽然我们经常会漏掉或忽略一些需要重构的地方。方法搬移,正如所定义的那样,把方法搬移到更适合他的位置。让我们看看下面这一段重构前的代码:

理解:方法搬移,正如所定义的那样,把方法搬移到更适合他的位置。

详解:如果一个类中某个方法,经常被其他类使用(比自身使用的次数还多),或者这个方法本身不适合这个类,可以考虑把这个方法搬移到更适合他的类中。

 

 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; }

        public double CalculateInterestRate()
        {
            if (CreditScore > 800)
                return 0.02;

            if (AccountAge > 10)
                return 0.03;

            return 0.05;
        }
    }
 public class AccountInterest
    {
        public BankAccount Account { get; private set; }

        public AccountInterest(BankAccount account)
        {
            Account = account;
        }

        public double InterestRate
        {
            get { return Account.CalculateInterestRate(); }
        }

        public bool IntroductoryRate
        {
            get { return Account.CalculateInterestRate() < 0.05; }
        }
    }

重构后的代码:

 1 public class BankAccount
 2     {
 3         public BankAccount(int accountAge, int creditScore, AccountInterest accountInterest)
 4         {
 5             AccountAge = accountAge;
 6             CreditScore = creditScore;
 7             AccountInterest = accountInterest;
 8         }
 9 
10         public int AccountAge { get; private set; }
11         public int CreditScore { get; private set; }
12         public AccountInterest AccountInterest { get; private set; }
13 
14         
15     }
16  public class AccountInterest
17     {
18         public BankAccount Account { get; private set; }
19 
20         public AccountInterest(BankAccount account)
21         {
22             Account = account;
23         }
24 
25         public double InterestRate
26         {
27             get { return CalculateInterestRate(); }
28         }
29 
30         public bool IntroductoryRate
31         {
32             get { return CalculateInterestRate() < 0.05; }
33         }
34 
35         public double CalculateInterestRate()
36         {
37             if (Account.CreditScore > 800)
38                 return 0.02;
39 
40             if (Account.AccountAge > 10)
41                 return 0.03;
42 
43             return 0.05;
44         }
45     }

我们将CalculateInterestRate方法从BankAccount类搬移到 AccountInterest类中,因为这个方法在AccountInterest类中使用,而在自身没有看到使用。并且CalculateInterestRate方法更适合AccountInterest类。

搬移后大家可以看到BankAccount类的职责也单一,同时CalculateInterestRate也放到了经常使用且适合它的类中了,所以此重构是一个比较好的重构,能让整个代码变得更加合理。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值