I'm a Java web beginner. Today I came onto the following scenario:
I have several classes with no inheritance relationship like ClassA
and ClassB
public class ClassA {
private String a1;
private String a2;
}
public class ClassB {
private String b1;
private String b2;
private String b3;
}
Functions to process them are exactly the same, except for the input parameter type and some get methods.
public void function functionA(ClassA a) {
...
Object[] obj = new Object[]{a.getA2()};
...
}
public void function functionB(ClassB b) {
...
Object[] obj = new Object[]{b.getB1(),b.getB2(),b.getB3()};
...
}
Apparently, writing in this way causes code duplication and low scalability, I've tried to make it tidy, but I have no idea about handling with the input type difference.
Could any one tell me how to extract the common logic of functionA
and functionB
?