以算法替代过长方法,解决由于多个if,case导致方法过长
- package com.netsoft;
- /**
- * <p>Date : 2012-03-16</p>
- * <p>Description: 以算法替代过长方法,解决由于多个if,case导致方法过长</p>
- *
- * @author 小俊垃圾回收站
- *
- */
- public class SubstituteAlgorithm {
- /*
- * 复杂算法
- * @param peoples
- * @return
- *
- public String foundPerson(String[] peoples) {
- for (String people : peoples) {
- if (people.equals("Jim"))
- return "Jim";
- else if (people.equals("Tom"))
- return "Tom";
- else if (people.equals("Lili"))
- return "Lili";
- }
- return "";
- }*/
- /** 替换算法
- * @param peoples
- * @return
- */
- public String foundPerson(String[] peoples) {
- String[] pName = {"Jim","Tom","Lili"};
- int index = 0;
- for (String people : peoples) {
- if(people.equals(pName[index]))
- return pName[index];
- }
- return "";
- }
- }
本代码参照《重构改善代码的设计》
——————————————————————————————————————————小俊垃圾回收站
转载于:https://blog.51cto.com/12772226/807958