我有一个名为Transformer的超类。我有两个派生类,它们将A类转换为B,反之亦然。有课程:
class A{
private Long id;
private String name;
... getter and setters
}
class B{
private Long id;
private String name;
... getter and setters
}
interface Transformer<S,D> {
D convert(S source);
}
class ATransformer implements Transformer<A,B>{
public B convert(A a){
B b=new B();
b.setId(a.getId());
.....
return b;
}
}
class BTransformer implements Transformer<B,A>{
public A convert(B b){
A a=new A();
a.setId(b.getId());
.....
return a;
}
}
如您所见,派生的变换器类具有重复的代码。是否有任何设计或解决方案可以避免这种重复而不使用反射?