重构 改善既有代码的设计 Replace Method with Method Object(以函数对象取代函数)...

你有一个大型函数,其中对局部变量的使用使你无法采用Extract Method

将这个函数放进一个单独对象中,如此一来局部变量就成了对象内的字段。然后你可以在同一个对象中将这个大型函数分解为多个小型函数。

动机

我们一直在强调,小型函数优美动人。只要将相对独立的代码从大型函数中提炼出来,就大大提高了函数的可读性。

但是,局部变量的存在会增加函数分解难度。如果一个函数中局部变量泛滥成灾,那么这个时候Replace Temp with Query可以帮助你。有时候根本无法拆解一个需要拆解的函数,这时候Replace Method with Method Object就发挥作用了。

Replace Method with Method Object会将所有局部变量都变成函数对象的字段。然后就可以对这个新函数使用Extract Method创造新函数,从而达到拆解的目的。

范例

如果要找到合适的例子,那么需要很长的篇幅,所以我们杜撰了这样一个函数。

复制代码
class Account
{
      int Gamma(int inputVal, int quantity, int yearToDate)
      {
           int importantValue1 = inputVal * quantity + Delta();
           int importantValue2 = inputVal * yearToDate + 100;
           if (yearToDate - importantValue1 > 100)
           {
               importantValue2 -= 20;
           }
           int importantValue3 = importantValue2 * 7;
           //and so on...
           return importantValue3 - 2 * importantValue1;
       }
       public int Delta()
       {
           return 100;
       }
}
复制代码

为了把这个函数变成函数对象,首先声明一个新类。在新类中,提供一个字段用于保存原对象,同时也对函数的每个参数和每个临时变量,提供字段用于保存。

复制代码
class Gamma
{

        private readonly Account _account;

        private readonly int _inputVal;

        private readonly int _quantity;

        private readonly int _yearToDate;

        private int _importantValue1;

        private int _importantValue2;

        private int _importantValue3;
}
复制代码

接下来,加入一个构造函数:

复制代码
public Gamma(Account account, int inputVal, int quantity, int yearToDate)
{
       _account = account;
       _inputVal = inputVal;
       _quantity = quantity;
       _yearToDate = yearToDate;
}
复制代码

接下来,将原本的函数搬到Compute()中。

复制代码
public int Compute()
{
    _importantValue1 = _inputVal * _quantity + _account.Delta();
    _importantValue2 = _inputVal * _yearToDate + 100;
    if (_yearToDate - _importantValue1 > 100)
    {
       _importantValue2 -= 20;
    }
    _importantValue3 = _importantValue2 * 7;
    //and so on...
    return _importantValue3 - 2 * _importantValue1;
}
复制代码

完整的Gamma函数如下:

复制代码
class Gamma
{

   private readonly Account _account;

   private readonly int _inputVal;

   private readonly int _quantity;

   private readonly int _yearToDate;

   private int _importantValue1;

   private int _importantValue2;

   private int _importantValue3;
public Gamma(Account account, int inputVal, int quantity, int yearToDate) { _account = account; _inputVal = inputVal; _quantity = quantity; _yearToDate = yearToDate; } public int Compute() { _importantValue1 = _inputVal * _quantity + _account.Delta(); _importantValue2 = _inputVal * _yearToDate + 100; if (_yearToDate - _importantValue1 > 100) { _importantValue2 -= 20; } _importantValue3 = _importantValue2 * 7; //and so on... return _importantValue3 - 2 * _importantValue1; } }
复制代码

最后,修改旧函数,让它的工作委托给刚完成的这个函数对象。

int Gamma(int inputVal, int quantity, int yearToDate)
{
    return new Gamma(this, inputVal, quantity, yearToDate).Compute();
}

这就是本项重构的基本原则。它的好处是:现在我们可以轻松地对Compute()函数采取Extract Method,不必担心参数传递的问题。

比如说我们对Compute进行如下重构:

复制代码
public int Compute()
{
    _importantValue1 = _inputVal * _quantity + _account.Delta();
    _importantValue2 = _inputVal * _yearToDate + 100;
    GetImportantThing();
    _importantValue3 = _importantValue2 * 7;
    //and so on...
    return _importantValue3 - 2 * _importantValue1;
}

void GetImportantThing()
{
    if (_yearToDate - _importantValue1 > 100)
    {
        _importantValue2 -= 20;
    }
}
复制代码

转载于:https://www.cnblogs.com/gaoqiaoliangjie/p/10151072.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值