论坛首页 Java企业应用论坛

请问这段代码如何重构

浏览 7938 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-08-30  
public class Main {
    
    public List init() {
        List list = new ArrayList();
        list.add(new Student("张三",13));
        list.add(new Student("是三",12));
        list.add(new Student("句三",14));
        list.add(new Student("李三",18));
        list.add(new Student("飞三",11));
        list.add(new Student("也三",17));
        list.add(new Student("去三",15));
        list.add(new Student("提三",12));
        list.add(new Student("李三",13));
        list.add(new Student("人三",18));
        list.add(new Student("他三",13));
        list.add(new Student("米三",15));
        list.add(new Student("平三",16));
        
        return list;
    }
    
    public int getTotalAge(List list) {
        Student student = null;
        int age = 0;
        for(int i = 0; i < list.size(); i++) {
            student = (Student)list.get(i);
            age = age + student.getAge();
        }
        
        return age;
    }
    
    public int getNum(List list) {
        Student student = null;
        int num = 0;
        for(int i = 0; i < list.size(); i++) {
            student = (Student)list.get(i);
            if(student.getName().startsWith("李")) 
                num ++;
        }
        
        return num;
    }
    
    public static void main(String args[]) {
        Main main = new Main();
        List list = main.init();
        System.out.println(main.getTotalAge(list));
        System.out.println(main.getNum(list));
    }
}

上面的代码中,有2个方法的for循环部分重复。如何消除重复的代码。
   发表时间:2007-08-30  
这段代码我觉得没必要重构,如果整合起来反而会影响对业务逻辑的理解,比如如此:
public int computer(List list, int type){
   ...
   int ret = 0;
   for(int i = 0; i < list.size(); i++) { 
      student = (Student)list.get(i); 
      switch(type){
          case 0:
              ret += student.getAge();
              break;
          case 1:
              if(student.getName().startsWith("李"))
                 ret ++;
              break;
      }
      return ret;
}
0 请登录后投票
   发表时间:2007-08-30  

这种for不需要重构,要么将

java 代码
  1. getNum(List list)  

增加一个参数,改为

java 代码
  1. getNumOfNameStartWith(List list,String prefixName)  
就可以统计不同姓氏的num了
0 请登录后投票
   发表时间:2007-08-30  

这段代码确实没有必要重构,他逻辑上也不复杂,for循环的重复也是很常见的,如果lz硬是要重构,可以这样

 

java 代码
  1. package com.actmaps;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6.   
  7. /**  
  8.  * TODO   
  9.  * @author <a href="mailto:pheh.lin@gmail.com">PHeH</a>PHeH</a><br>  
  10.  * Created On 2007-8-30 16:04:27  
  11.  */  
  12. public class Test {   
  13.               
  14.     public List init() {      
  15.         List list = new ArrayList();      
  16.         list.add(new Student("张三",13));      
  17.         list.add(new Student("是三",12));      
  18.         list.add(new Student("句三",14));      
  19.         list.add(new Student("李三",18));      
  20.         list.add(new Student("飞三",11));      
  21.         list.add(new Student("也三",17));      
  22.         list.add(new Student("去三",15));      
  23.         list.add(new Student("提三",12));      
  24.         list.add(new Student("李三",13));      
  25.         list.add(new Student("人三",18));      
  26.         list.add(new Student("他三",13));      
  27.         list.add(new Student("米三",15));      
  28.         list.add(new Student("平三",16));      
  29.               
  30.         return list;      
  31.     }   
  32.        
  33.     public int iterList(List list,IDoExecute tool){   
  34.         int result = 0;   
  35.         Student student = null;   
  36.         for(int i = 0; i < list.size(); i++) {      
  37.             student = (Student)list.get(i);      
  38.             result += tool.execute(student);   
  39.         }   
  40.         return result;   
  41.     }   
  42.           
  43.     public int getTotalAge(List list) {   
  44.         return this.iterList(list,new DoExecute(){   
  45.             public int execute(Student stu){   
  46.                 return stu.getAge();   
  47.             }   
  48.         });   
  49.     }      
  50.           
  51.     public int getNum(List list) {      
  52.         return this.iterList(list,new DoExecute(){   
  53.             public int execute(Student stu){   
  54.                 if(stu.getName().startsWith("李"))   
  55.                     return 1;   
  56.                 else  
  57.                     return 0;   
  58.             }   
  59.         });   
  60.     }      
  61.           
  62.     public static void main(String args[]) {   
  63.         Test test = new Test();   
  64.         List list = test.init();      
  65.         System.out.println(test.getTotalAge(list));      
  66.         System.out.println(test.getNum(list));      
  67.     }   
  68.        
  69.     class Student{   
  70.         private String name = "";   
  71.         private int age = 0;   
  72.         public Student(String n,int a){   
  73.             name = n;   
  74.             age = a;   
  75.         }   
  76.         public int getAge() {   
  77.             return age;   
  78.         }   
  79.         public void setAge(int age) {   
  80.             this.age = age;   
  81.         }   
  82.         public String getName() {   
  83.             return name;   
  84.         }   
  85.         public void setName(String name) {   
  86.             this.name = name;   
  87.         }   
  88.     }   
  89.     interface IDoExecute{   
  90.         public int execute(Student stu);   
  91.     }   
  92.     class DoExecute implements IDoExecute{   
  93.         public int execute(Student stu){   
  94.             return 0;   
  95.         }   
  96.     }   
  97. }   

 

这样确实只有一个for语句,不过对于这种简单的逻辑,是不是有点过度了,程序只要运行正确,便于维护,开发快速就可以了,多一个for少一个for,就lz的例子来说,性能上的差异几乎可以忽略

 

0 请登录后投票
   发表时间:2007-08-30  
如果那“张三,李三”等一共是10w个兄弟姐妹的时候,我想还是需要重构一下的,一次循环,传回一个List<int ,int>, 但是现在计划生育抓的好,他们一共才那么几个兄弟姐妹,for他10次也不慢哇
0 请登录后投票
   发表时间:2007-08-30  
我记得ajoo以前有个帖子和你的问题很像,思路是抽出一个Getter接口,在Getter接口的方法中传入数据对象,思路和pheh的有些类似,你可以在je里搜索一下。
0 请登录后投票
   发表时间:2007-08-30  
import java.util.ArrayList;
import java.util.List;

public class Main {   
       
    public List init() {   
        List list = new ArrayList();   
        list.add(new Student("张三",13));   
        list.add(new Student("是三",12));   
        list.add(new Student("句三",14));   
        list.add(new Student("李三",18));   
        list.add(new Student("飞三",11));   
        list.add(new Student("也三",17));   
        list.add(new Student("去三",15));   
        list.add(new Student("提三",12));   
        list.add(new Student("李三",13));   
        list.add(new Student("人三",18));   
        list.add(new Student("他三",13));   
        list.add(new Student("米三",15));   
        list.add(new Student("平三",16));   
           
        return list;   
    }
    
    public TotalResult getTotalResult(List list){
        Student student = null;   
        int age = 0;
        int num = 0;
        for(int i = 0; i < list.size(); i++) {   
            student = (Student)list.get(i);   
            age = age + student.getAge();   
            if(student.getName().startsWith("李"))    
                num ++;   
        }
        return new TotalResult(age,num);
    	
    }
       
    public int getTotalAge(List list) {   
        Student student = null;   
        int age = 0;   
        for(int i = 0; i < list.size(); i++) {   
            student = (Student)list.get(i);   
            age = age + student.getAge();   
        }   
           
        return age;   
    }   
       
    public int getNum(List list) {   
        Student student = null;   
        int num = 0;   
        for(int i = 0; i < list.size(); i++) {   
            student = (Student)list.get(i);   
            if(student.getName().startsWith("李"))    
                num ++;   
        }   
           
        return num;   
    }   
       
    public static void main(String args[]) {   
        Main main = new Main();   
        List list = main.init();   
//        System.out.println(main.getTotalAge(list));   
//        System.out.println(main.getNum(list));
        System.out.println(main.getTotalResult(list));
    }   
}   
class Student{
	private String name;
	private int age;
	
	public Student(){
	}
	
	public Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
class TotalResult{
	private int age;
	private int num;
	
	public TotalResult(int age,int num){
		this.age = age;
		this.num = num;
	}
	
	public String toString(){
		return "age:"+age+",num:"+num;
	}
	
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
}
0 请登录后投票
   发表时间:2007-09-11  
import java.util.ArrayList;  
import java.util.List;  
 
public class Main {     
public List init() {     
    List students = new ArrayList();     
    students.add(new Student("张三",13));     
    students.add(new Student("是三",12));     
    students.add(new Student("句三",14));     
    students.add(new Student("李三",18));     
    students.add(new Student("飞三",11));     
    students.add(new Student("也三",17));     
    students.add(new Student("去三",15));     
    students.add(new Student("提三",12));     
    students.add(new Student("李三",13));     
    students.add(new Student("人三",18));     
    students.add(new Student("他三",13));     
    students.add(new Student("米三",15));     
    students.add(new Student("平三",16));
    return students;
    }  
    public static void main(String args[]) {    
    Main m = new Main();
   
    Student first = new Student("new one",12);
    Student second = new Student("new second",23);
   
    List list = m.init();
   
    ManagerStudent ms = new ManagerStudent(list);
    ms.addStudent(first);
    ms.addStudent(second);
   
    ms.removeStudent(first);
   
        System.out.println(ms.getTotalAge());  
        System.out.println(ms.getNum());  
    }     
}     
class Student{  
    private String name;  
    private int age;  
      
    public Student(){  
    }  
      
    public Student(String name,int age){  
        this.name = name;  
        this.age = age;  
    }  
      
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
}  
class ManagerStudent{
    private Student student;
    private List students = new ArrayList();
   
    public ManagerStudent(List list){
    students = list;
    }
   
    public int getTotalAge() {     
        Student student = null;
        int age = 0;     
        for(int i = 0; i < students.size(); i++) {     
            student = (Student)students.get(i);     
            age = age + student.getAge();     
        }

        return age;     
    }

    public int getNum() {     
        Student student = null;     
        int num = 0;     
        for(int i = 0; i < students.size(); i++) {     
            student = (Student)students.get(i);     
            if(student.getName().startsWith("李"))      
                num ++;     
        }     
             
        return num;     
    }

public List getStudents() {
return students;
}

public void setStudents(List students) {
this.students.addAll(students);
}   
   
    public void addStudent(Student one){
    students.add(one);
    }
   
    public void removeStudent(Student one){
    if(students.contains(one)){
    students.remove(one);
    }else{
    System.err.println("不包含此学生!");
    return;
    }
   
    }
}
0 请登录后投票
   发表时间:2007-09-11  
public class Main {  
  
   public int num = 0;
   public int age = 0;   
    public List init() {  
        List list = new ArrayList();  
        list.add(new Student("张三",13));  
        list.add(new Student("是三",12));  
        list.add(new Student("句三",14));  
        list.add(new Student("李三",18));  
        list.add(new Student("飞三",11));  
        list.add(new Student("也三",17));  
        list.add(new Student("去三",15));  
        list.add(new Student("提三",12));  
        list.add(new Student("李三",13));  
        list.add(new Student("人三",18));  
        list.add(new Student("他三",13));  
        list.add(new Student("米三",15));  
        list.add(new Student("平三",16));  
          
        return list;  
    }  
      
    public void getTotalAge(List list) {  
        Student student = null;    
        for(int i = 0; i < list.size(); i++) {  
            student = (Student)list.get(i);
            if(student.getName().startsWith("李"))   
                num ++;   
            age = age + student.getAge();  
        }  
           
    }  
        
      
    public static void main(String args[]) {  
        Main main = new Main();  
        List list = main.init();
        System.out.println(age);
        System.out.println(num);        
    }  
}
0 请登录后投票
   发表时间:2007-09-11  
lz有点过分追求了吧。。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics