Given a util class with two similar methods that differs only in the field setter/getter:
public static void method_A(Dbo dbo) {
if (dbo instanceof Zdbo) {
((Zdbo)dbo)
.getSome()
.forEach(z -> z.setFieldX(dbo.getFieldX()));
}
}
public static void method_B(Dbo dbo) {
if (dbo instanceof Zdbo) {
((Zdbo)dbo)
.getSome()
.forEach(z -> z.setFieldZ(dbo.getFieldZ()));
}
}
my question is: how can I rid of the duplicate code?
My approach was to achieve something like this:
private static void xxx(Dbo dbo, Consumer c) {
if (dbo instanceof Zdbo) {
((Zdbo)dbo).getSome().forEach(c);
}
}