ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
你想要把某个算法替换为另一个更清晰的算法。

将函数本体(method body)替换为另一个算法。

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 "";
}
posted on 2005-08-29 17:03 ivaneeo 阅读(195) 评论(0)  编辑  收藏 所属分类: refactoring-从地狱中重生

只有注册用户登录后才能发表评论。


网站导航: