Substitute Algorithm(替换你的算法)
来源:互联网 发布:mac pro怎么截图 编辑:程序博客网 时间:2024/05/17 23:07
String foundPerson(String[] people) {
for(int i = 0; i < people.length; i++) {
if(people[i].equals("Don")) {
return "Don";
}
if(people[i].equals("John")) {
return "John";
}
if(people[i].equals("Kent")) {
return "Kent";
}
}
return "";
}
==〉
String foundPerson(String[] people) {
List candidates = Arrays.asList(new String[]{"Don", "John", "Kent"});
for(int i = 0; i < people.length; i++)
if(candidates.contains(people[i]))
return people[i];
return "";
}
动机
有时侯你会想要修改原先的算法,让它去做一件与原先动作略有差异的事。这时候你也可以先把原先的算法替换为一个较易修改的算法,这样后续的修改会轻松许多。
使用这项重构手法之前,请先确定自己已经尽可能分解了原先函数。替换一个巨大而复杂的算法是非常困难的,只有先将它分解为较简单的小型函数,然后你才能很有把握地进行算法替换工作。
作法
1. 准备好你的另一个(替换用)算法,让它通过编译。
2. 针对现有测试,执行上述的新算法。如果结果于原本结果相同,重构结束
3. 如果测试结果不同于原先,在测试和调试过程中,以旧算法为比较参照标准。
对于每个test case(测试用例),分别以新旧两种算法执行,并观察两者结果是否相同。这可以帮助你看到哪一个test case出现麻烦,以及出现了怎么的麻烦。
- Substitute Algorithm(替换你的算法)
- 6.9substitute algorithm(替换算法)
- 《Refactoring》,Substitute Algorithm(替换算法)
- 重新组织你的函数之九 :Substitute Algorithm(替换你的算法)
- 重新组织函数--Substitute Algorithm(替换算法)
- 重构笔记4-替换算法(substitute algorithm)
- 重构---重构手法-----替换算法(Substitute Algorithm)
- 重构--Inline Method(内联函数),Inline Temp(内联临时变量),Substitute Algorithm(替换算法)(五)
- java 代码细节(Substitute Algorithm)
- 利用数组和Substitute替换字符
- 替换文本中的$占位符 Template.substitute、 Template.safe_substitute
- Python Template中substitute的使用
- Algorithm(算法
- 算法(algorithm):
- Algorithm算法
- 如何替换你的LOGO
- 简单的遗传算法(Genetic Algorithm)源代码
- 常用算法(Algorithm)的用法介绍
- 2009.01.04
- rpm 命令
- js 解决中文乱码函数-js中escape,encodeURI,encodeURIComponent三个函数的区别
- HTTP 头部解释,HTTP 头部详细分析,最全HTTP头部信息(2)
- PROTEL DXP原理图无变化更新PCB时却反复删除添加元器件的处理方法
- Substitute Algorithm(替换你的算法)
- 优化epoll_ctl
- js 解决ajax post 中文乱码问题
- Richfaces应用--actionparam传值
- 在RHEL5.0安装subversion-1.4.2-2的过程
- 白话Oracle数据库和实例的关系
- Filter过滤器经典应用
- HTTP 头部解释,HTTP 头部详细分析,最全HTTP头部信息
- 查找表中重复数据