您的位置:首页 > 编程语言 > Go语言

6.9 Substitute Algorithm(替换算法)

2013-11-27 15:12 381 查看
概括 把某个算法替换为另一个更清晰的算法。
将函数本体替换为另一个算法。 例子
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 "";
}

动机[/b] 使用此项重构手法之前,先要确定自己已经尽可能分解了源函数。替换一个巨大而复杂的算法很困难,只有先将它分解为较简单的小型函数,然后才可能很有把握的进行算法替换。做法[/b].实践[/b].

来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: