摘要: 概括 把某个算法替换为另一个更清晰的算法。 将函数本体替换为另一个算法。 例子String foundPerson(String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ return "Don"; } if (people[... 阅读全文
posted @ 2013-11-27 15:12 tanhaiyuan 阅读(183) 评论(0) 推荐(0) 编辑
摘要: 概括 大型函数中对局部变量的使用使得无法采用Exrtact Method方法。 将此函数放进一个单独对象中,此时局部变量就成了对象内的字段。然后可以在同一个对象中将这个大型函数分解为多个小型函数。 例子class Order{ ... double price(){ double primaryBasePrice; double secondaryBasePrice; ... 阅读全文
posted @ 2013-11-27 15:00 tanhaiyuan 阅读(145) 评论(0) 推荐(0) 编辑
摘要: 概括代码对一个参数进行赋值。以一个临时变量取代该参数的位置。 例子int discount(int inputVal, int quantity, int yearTodate){ if(inputVal > 50) inputVal = -2;}重构后int discount(int inputVal, int quantity, int yearTodate){ int result =... 阅读全文
posted @ 2013-11-27 14:44 tanhaiyuan 阅读(189) 评论(0) 推荐(0) 编辑
摘要: 概括 某个临时变量被赋值超过一次,且它既不是循环变量,也不是被用于收集计算结果。 针对每次赋值,创造一个独立,对应的临时变量。 例子double temp = 2 * (_height + _width);System.out.println(temp);temp = _height * _width;System.out.println(temp);重构后final double... 阅读全文
posted @ 2013-11-27 14:29 tanhaiyuan 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 概括 你有一个复杂的表达式。 将该复杂表达式(或其中一部分)的结果放进一个临时变量,以此变量名来解释表达式用途。 例子if((platform.toUperCase().indexOf("MAC") > -1) && (browser.toUperCase().indexOf("IE") > -1) && wasInitialized() && resize > 0 ){ //do... 阅读全文
posted @ 2013-11-27 14:10 tanhaiyuan 阅读(228) 评论(0) 推荐(0) 编辑
摘要: 概括以一个临时变量保存某一个表达式的运算结果。将这个表达式提炼到一个独立函数汇总。将这个临时变量的所有引用点替换为对新函数的调用。此后,新函数就可被其它函数使用。 例子double basePrice = _quantity * _itemPrice;if(basePrice > 1000) return basePrice*0.95;else return basePrice*0.5;重构... 阅读全文
posted @ 2013-11-27 13:45 tanhaiyuan 阅读(192) 评论(0) 推荐(0) 编辑
摘要: 概括一个临时变量,只被一个简单表达式赋值一次,而它妨碍了其他重构方法。将所有对该变量的引用替换为对它赋值的那个表达式本身。 例子double basePrice = anOrder.basePrice();return (basePrice > 1000)重构之后return (anOrder.basePrice() > 1000)动机Iniline Temp多半是作为Replace Temp ... 阅读全文
posted @ 2013-11-27 10:39 tanhaiyuan 阅读(441) 评论(0) 推荐(0) 编辑
摘要: 概括一个函数的内部代码与名称同样清晰易懂。在函数调用点插入函数本体,然后移除该函数。 例子int getRating(){ return (moreThanFiveLateDeliveries()) ? 2 : 1;}boolean moreThanFiveLateDeliveries(){ return _numberOfLateDeliveries > 5;} 重构之后int getR... 阅读全文
posted @ 2013-11-27 10:38 tanhaiyuan 阅读(197) 评论(0) 推荐(0) 编辑
摘要: 概括有一段代码可以被组织在一起并独立出来。将这段代码放进一个独立函数中,并让函数名称解释该函数的用途。 例子voidprintOwing(doubleamount){ printBanner();//print details System.out.println("name:"+_name); System.out.println("amount:"+amount);} 重构之后... 阅读全文
posted @ 2013-11-27 10:18 tanhaiyuan 阅读(223) 评论(0) 推荐(0) 编辑