步步为营 .NET 代码重构学习笔记 六、移动函数和移动值域(Move Method And Move Field)

Move Method

概述

程序中,有个函数与其所驻class之外的另一个class进行更多交流,调用后者或被后者调用

动机(Motivation)

如果一个class有太多行为,或如果一个class与另一个class有太多合作而形成高度耦合(highly coupled),我们就会搬移函数。通过这种手段,我们可以使系统中的classes更简单,这些classes最终也将更干净利落地实现系统交付的任务。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class MoveMethod
  {
      private AccountType _type;
      private int _daysOverdrawn;
      public double OverDraftCharge()
      {
          if (_type.IsPremium())
          {
              double result = 10;
              if (_daysOverdrawn > 7)
                  result += (_daysOverdrawn - 7) * 0.85;
              return result;
          }
          else
              return _daysOverdrawn * 1.75;
      }
      public double BankCharge()
      {
          double result = 4.5;
          if (_daysOverdrawn > 0)
              result += OverDraftCharge();
          return result;
      }
  }
 
  public class AccountType
  {
      public bool IsPremium()
      {
          return true;
      }
  }

改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class MoveMethod
{
    private AccountType _type;
 
    public double BankCharge()
    {
        double result = 4.5;
        if (_type._daysOverdrawn > 0)
            result += _type.OverDraftCharge();
        return result;
    }
}
 
public class AccountType
{
    private int _daysOverdrawn;
 
    public int DaysOverdrawn
    {
        get { return _daysOverdrawn; }
        set { _daysOverdrawn = value; }
    }
 
    public bool IsPremium()
    {
        return true;
    }
 
    public double OverDraftCharge()
    {
        if (IsPremium())
        {
            double result = 10;
            if (_daysOverdrawn > 7)
                result += (_daysOverdrawn - 7) * 0.85;
            return result;
        }
        else
            return _daysOverdrawn * 1.75;
    }
}

Move Field(搬移值域)

概述

在target class建立一个new field,修改source field的所有用户,令它们改用new field。

动机(Motivation)

对于一个field(值域),在其所驻class之外的另一个class中有更多函数使用了它,我就会考虑搬移这个field。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MoveMethod
{
    private AccountType _type;
    private double _interestRate;
    public double InterestForAmountDay(double amount,int days)
    {
        return _interestRate * amount * days / 365;
    }
}
 
public class AccountType
{
 
}

改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MoveMethod
{
    private AccountType _type;
 
    public double InterestForAmountDay(double amount, int days)
    {
        return _type.InterestRate * amount * days / 365;
    }
}
 
public class AccountType
{
    private double _interestRate;
    public double InterestRate
    {
        get { return _interestRate; }
        set { _interestRate = value; }
    }
}

总结

把公用函数和值域放到其基类中去,方便其它函数调用。

posted @   spring yang  阅读(1947)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示