ivaneeo's blog

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

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          你想要把某個算法替換為另一個更清晰的算法。

          將函數(shù)本體(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 閱讀(207) 評論(0)  編輯  收藏 所屬分類: refactoring-從地獄中重生
          主站蜘蛛池模板: 涟源市| 九龙县| 瓦房店市| 威信县| 临颍县| 会宁县| 察隅县| 布拖县| 西林县| 浙江省| 区。| 顺义区| 本溪市| 门头沟区| 嘉荫县| 盘山县| 泸定县| 姚安县| 德庆县| 化隆| 南漳县| 利辛县| 思茅市| 洞头县| 富顺县| 大石桥市| 辽源市| 乌拉特前旗| 寿阳县| 曲阜市| 乐清市| 新野县| 白玉县| 精河县| 合山市| 琼结县| 宜丰县| 鲁甸县| 清镇市| 革吉县| 福泉市|