what are best ways to abstract?

public static Stream<Zdbo> getSome(Dbo dbo) {
    return dbo instanceof Zdbo ? ((Zdbo)dbo).getSome() : Stream.empty();
}

public static Optional<Zdbo> asZdbo(Dbo dbo) {
    return dbo instanceof Zdbo ? Optional.of((Zdbo)dbo) : Optional.empty();
}

public static void method_A(Dbo dbo) {
    getSome(dbo).forEach(z -> z.setFieldX(dbo.getFieldX()));
}

public static void method_B(Dbo dbo) {
    getSome(dbo).forEach(z -> z.setFieldZ(dbo.getFieldZ()));
}

I would keep it on the Stream or Optional level. method_A and method_B above can simply be replaced by their content, without the need of passing a setter and getter. Also it is more versatile, and does not generate the code overhead.

Notice that for instanceof+cast a next java might have a better solution.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top