我有三个具体的课程 -
public class ClassA{
public C1 processA(C1 c1){
//lots of ugly code that copies
//one field to another
//return c1
}
public List<C1> processA(List<C1> c1s){
//iterate over c1s
//call process(c1)
//add returned value to list
//return list
}
}
public class ClassB{
public C2 processB(C2 c2, C3 c3){
//lots of ugly code that copies
//one field to another
//return c2
}
public List<C2> processB(List<Pair<C2, C3> pairs){
//iterate over pairs
//call process(c2, c3)
//add returned value to list
//return list
}
}
public class ClassC{
public C4 processC(C4 c4, C5 c5, C1 c1){
//lots of ugly code that copies
//one field to another
//return c4
}
public List<C4> processC(List<Triple<C4, C5, C1> triples){
//iterate over triples
//call process(c4, c5, c1)
//add returned value to list
//return list
}
}
我想删除进程(List ...)方法中的重复。我不关心processA(C1),processB(C2,C3),processC(C4,C5,C1)方法,它们将保持不变。重构此代码有哪些选择?
答案 0 :(得分:1)
除了代码重复之外,我还看到了代码中的另外两个问题,您可以解决这些问题以消除代码重复的气味: