重构手法之重新组织函数【5】

• Replace Method with Method Object（以函数对象取代函数）
• Substitute Algorithm（替换算法）

• 阶段性小结

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

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

`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();}`

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

9 Substitute Algorithm（替换算法）

范例

`string FoundPerson(string[] people){ foreach (var person in people) { if (person == "Don") { return "Don"; } if (person == "John") { return "John"; } if (person == "Kent") { return "Kent"; } } return string.Empty;}`

`string FoundPerson(string[] people){ var candidates = new List<string>() { "Don", "John", "Kent" }; foreach (var person in people) { if (candidates.Contains(person)) { return person; } } return string.Empty;}`

阶段性小结

To Be Continued...