`

使用内部类完成"Replace Method with Method Object"(以函数对象取代函数)重构

阅读更多
    “Replace Method with Method Object”(以函数对象取代函数)是一种重新组织函数(也就是Java中的方法,在本文中函数和方法这两个词表示的意思相同)的重构方法。其做法是将函数放进一个单独的对象当中,使用这个单独对象的值域(filed)来替代原函数中的局部变量。这样做的好处是对于一个拥有较多较复杂的局部变量的函数来说,进行“extract method”重构变得较为容易。
    偷懒,直接使用“重构——改善既有代码设计【Martin Flower】”一书中这个没有什么逻辑性的例子(有一些改动):
public class ReplaceMethodWithMethodObject extends TestCase {
	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;
		
		return importantValue3 - 2 * importantValue1;
	}
	
	public int delta() {
		return 5;
	}
	
	public void testGamma() {
		assertEquals(875, new Gamma(this, 5, 6, 7).gamma());
	}
}

//outer class method object
class Gamma {
	private ReplaceMethodWithMethodObject rmwmb;
	private int inputVal;
	private int quantity;
	private int yearToDate;
	
	public Gamma(ReplaceMethodWithMethodObject rmwmb ,int inputVal, int quantity, int yearToDate) {
		this.rmwmb = rmwmb;
		this.inputVal = inputVal;
		this.quantity = quantity;
		this.yearToDate = yearToDate;
	}
	
	int gamma() {
		int importantValue1 = (inputVal * quantity) + rmwmb.delta();
		int importantValue2 = (inputVal * yearToDate) + 100;
		if ((yearToDate - importantValue1) > 100) {
			importantValue2 -= 20;
		}
		int importantValue3 = importantValue2 * 7;
		
		return importantValue3 - 2 * importantValue1;
	}
}

    代码使用JUnit进行单元测试,在测试方法中调用时将ReplaceMethodWithMethodObject对象本身的引用传入Gamma对象的gamma方法。
    其实这里可以使用内部类来进行该项重构,代码如下:
public class ReplaceMethodWithMethodObject extends TestCase {
	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;
		
		return importantValue3 - 2 * importantValue1;
	}
	
	private int delta() {
		return 5;
	}
	
	/*
	 * replace method with inner class method object(could be better than outer class!)
	 */
	class Gamma {
		private int inputVal;
		private int quantity;
		private int yearToDate;
		
		public Gamma(int inputVal, int quantity, int yearToDate) {
			this.inputVal = inputVal;
			this.quantity = quantity;
			this.yearToDate = yearToDate;
		}
		
		int gamma() {
			int importantValue1 = (inputVal * quantity) + delta();
			int importantValue2 = (inputVal * yearToDate) + 100;
			if ((yearToDate - importantValue1) > 100) {
				importantValue2 -= 20;
			}
			int importantValue3 = importantValue2 * 7;
			
			return importantValue3 - 2 * importantValue1;
		}
	}
	
	public void testGamma() {
		assertEquals(875, new Gamma(5, 6, 7).gamma());
	}
}

    使用内部类进行“Replace Method with Method Object”重构带来两点好处:一是调用重构后的对象的函数时不用再传入当前对象的引用(因为内部类对象自动持有其外围类对象的引用);二是重构函数中调用的原对象中的其他函数可以不用public(请对比两段代码,注意delta()函数的访问修饰符),因为在内部类中可以访问到其外围类的任何成员(包括private的方法)。
分享到:
评论

相关推荐

    重构-改善既有代码的设计 中文版.pdf

    10.8 Replace Parameter with Method(以函数取代参数) 10.9 Introduce Parameter Object(引入参数对象) 10.10 Remove Setting Method(移除设值函数) 10.11 Hide Method(隐藏你的函数) 10.12 Replace Co ...

    重构-改善既有代码的设计 中文版

    10.8 Replace Parameter with Method(以函数取代参数) 10.9 Introduce Parameter Object(引入参数对象) 10.10 Remove Setting Method(移除设值函数) 10.11 Hide Method(隐藏你的函数) 10.12 Replace Co ...

    重构-改善既有代码的设计

    10.12 Replace Constructor with Factory Method(以工厂函数取代构造函数) 304 10.13 Encapsulate Downcast(封装向下转型) 308 10.14 Replace Error Code with Exception(以异常取代错误码) 310 10.15 ...

    重构:改善既有代码的设计.[美]Martin Fowler.epub【文字版手机格式】

    10.12 Replace Constructor with Factory Method(以工厂函数取代构造函数) 10.13 Encapsulate Downcast(封装向下转型) 10.14 Replace Error Code with Exception(以异常取代错误码) 10.15 Replace Exception ...

    重构-改善既有代码的设计+中文版

     Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动特性  *Move Method 移动方法   Move Field 移动...

    重构-改善既有代码的设计(中文版)

     Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动特性  *Move Method 移动方法   Move Field 移动...

    重构 改善既有代码的设计

     Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动特性  *Move Method 移动方法   Move Field ...

    重构_改善既有代码的设计[高清版]中文版

     Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动特性  *Move Method 移动方法   Move Field 移动...

    重构——改善既有代码的设计

     Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动特性  *Move Method 移动方法   Move Field 移动...

    重构,改善既有代码的设计

     Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动特性  *Move Method 移动方法   Move Field 移动...

    重构:改善既有代码的设计(中文高清版)

    6.4 ReplaceTempwithQuery(以查询取代临时变量)120 6.5 IntroduceExplainingVariable(引入解释性变量)124 6.6 SplitTemporaryVariable(分解临时变量)128 6.7 RemoveAssignmentstoParameters(移除对参数的赋值...

    重构:改善既有代码的设计(中文版).

    6.4 ReplaceTempwithQuery(以查询取代临时变量)120 6.5 IntroduceExplainingVariable(引入解释性变量)124 6.6 SplitTemporaryVariable(分解临时变量)128 6.7 RemoveAssignmentstoParameters(移除对参数的赋值...

    重构_改善既有代码的设计.pdf

    6.4 ReplaceTempwithQuery(以查询取代临时变量)120 6.5 IntroduceExplainingVariable(引入解释性变量)124 6.6 SplitTemporaryVariable(分解临时变量)128 6.7 RemoveAssignmentstoParameters(移除对参数的赋值...

    重构_改善既有代码的设计

     6.4 ReplaceTempwithQuery(以查询取代临时变量)120  6.5 IntroduceExplainingVariable(引入解释性变量)124  6.6 SplitTemporaryVariable(分解临时变量)128  6.7 RemoveAssignmentstoParameters(移除对...

    《重构改善既有代码的设计(2010年版)》(Martin Fowler[美] 著,熊节 译)

    6.4 ReplaceTempwithQuery(以查询取代临时变量)120 6.5 IntroduceExplainingVariable(引入解释性变量)124 6.6 SplitTemporaryVariable(分解临时变量)128 6.7 RemoveAssignmentstoParameters(移除对参数的赋值...

    Delphi字符串快速替换函数,速度超快 秒杀自带的StringReplace函数

    Delphi自带的StringReplace效率非常低,字符串长一些就很慢。这个速度绝对快,你可以和StringReplace比一下,

    重建——改善既有代码的设计

    6.4 ReplaceTempwithQuery(以查询取代临时变量)120 6.5 IntroduceExplainingVariable(引入解释性变量)124 6.6 SplitTemporaryVariable(分解临时变量)128 6.7 RemoveAssignmentstoParameters(移除对参数的赋值...

    python中的replace函数.docx

    定义 replace函数是Python中字符串对象的内置方法之一。它用于在字符串中查找并替换指定的子字符串。 2. 使用方法 replace方法可以通过以下方式调用: ```python string.replace(old, new[, count]) ``` 其中,...

    REPLACE()函数完成电话号码升位.xls

    REPLACE()函数完成电话号码升位.xls

Global site tag (gtag.js) - Google Analytics