简体   繁体   English

我怎样才能更好地抽象?

[英]How can I abstract better?

Given a util class with two similar methods that differs only in the field setter/getter:给定一个具有两个相似方法的 util 类,这些方法仅在 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);
    }
  }
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.我会将它保留在 Stream 或 Optional 级别。 method_A and method_B above can simply be replaced by their content, without the need of passing a setter and getter.上面的method_A和method_B可以简单地替换为它们的内容,不需要传递setter和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.请注意,例如 instanceof+cast 下一个 java 可能有更好的解决方案。

I would use a Function<Dbo, T> to extract the field and a BiConsumer<Some, T> to set the field to each Some object:我会使用Function<Dbo, T>来提取字段和BiConsumer<Some, T>将字段设置为每个Some对象:

private static <T> void setField(
        Dbo dbo, 
        Function<? super Dbo, ? extends T> extractor, 
        BiConsumer<? super Some, ? super T> setter) {

    Consumer<Some> c = some -> setter.accept(some, extractor.apply(dbo));

    if (dbo instanceof Zdbo) ((Zdbo) dbo).getSome().forEach(c);
}

This adapts the BiConsumer<Some, T> setter to a Consumer<Some> c , by binding the 2nd argument of setter to the value returned by the extractor function.适应BiConsumer<Some, T> setterConsumer<Some> c ,通过结合的第二参数setter通过返回的值extractor的功能。

Then, you can invoke it as follows:然后,您可以按如下方式调用它:

public static void method_A(Dbo dbo) {
    setField(dbo, Some::getFieldX, Some::setFieldX);
}

public static void method_B(Dbo dbo) {
    setField(dbo, Some::getFieldY, Some::setFieldY);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM