违反 Java 中的 DRY 原则

我有三种方法可以按字段过滤一组设备。


public void filtrateByType(Device[] devices, String type) {

    if (devices == null) {

        return;

    }


    for (int i = 0; i < devices.length; i++) {

        if (devices[i] == null) {

            continue;

        }


        if (devices[i].getType() == null && type == null) {

            continue;

        } else if (devices[i].getType() == null) {

            devices[i] = null;

            continue;

        }


        if (!devices[i].getType().equals(type)) {

            devices[i] = null;

        }

    }

}

其他方法类似。唯一的区别是调用另一个应用过滤的字段的 getter。例如,有一个对getModel()instead的调用getType()。这是否违反了 DRY 原则,我该如何更改它以使其不会(没有泛型)?


PS 这是一个家庭作业,不幸的是我们还没有使用泛型。我也无法更改方法的签名。我有一个线索,我可以使用一种方法创建内部类,该方法将调用所需的 getter 并返回一个值。所以,我需要把我所有的检查都放在这个方法中,但我真的不明白我的逻辑是怎么做的(尤其是“继续”)。


四季花海
浏览 190回答 3
3回答

SMILET

你可以创建一个interface DeviceValueExtractor看起来像这样的:@FunctionalInterfacepublic interface DeviceValueExtractor {&nbsp; &nbsp; Object extractValue(Device device);}现在将您的方法重写为:public void filterByType(Device[] devices, DeviceValueExtractor extractor, Object expect) {&nbsp; &nbsp; if (devices == null) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < devices.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (devices[i] == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Object actual = extractor.extractValue(devices[i]);&nbsp; &nbsp; &nbsp; &nbsp; if (actual == null && expect== null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; } else if (actual&nbsp; == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; devices[i] = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!Objects.equals(actual, expect)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; devices[i] = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;}用法:filterByType(devices, Device::getType, "Hello");注意:我使用是Object因为要求没有泛型 - 因为调用的唯一方法是equals这实际上没什么大不了的。但是,对于更多类型安全性,您可以引入泛型(并取消DeviceValueExtractor:public static <T> void filterByType(Device[] devices, Function<Device, T> extractor, T expect) {&nbsp; &nbsp; if (devices == null) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < devices.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (devices[i] == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Object actual = extractor.apply(devices[i]);&nbsp; &nbsp; &nbsp; &nbsp; if (actual == null && expect== null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; } else if (actual&nbsp; == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; devices[i] = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!Objects.equals(actual, expect)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; devices[i] = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

catspeake

也许一些 Java 8 魔法会在这里有所帮助:public void filtrateByType(Device[] devices, String type) {&nbsp; &nbsp; filtrateBy(devices, Device::getType, type);}public void filtrateBy(Device[] devices, Function<? super Device, String> attributeGetter, String attribute) {&nbsp; &nbsp; if (devices == null) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < devices.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (devices[i] == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (attributeGetter.apply(devices[i]) == null && attribute == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; } else if (attributeGetter.apply(devices[i]) == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; devices[i] = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!attributeGetter.apply(devices[i]).equals(attribute)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; devices[i] = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

炎炎设计

这是更简单的版本。您可以使用原始类型,但这会更容易出错。public static <T> void filtrateByType(T[] objects, Function<T, String> function, String type) {&nbsp; &nbsp; if (objects == null || type == null)&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; for (int i = 0; i < objects.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (objects[i] == null) continue;&nbsp; &nbsp; &nbsp; &nbsp; String match = function.apply(objects[i]);&nbsp; &nbsp; &nbsp; &nbsp; if (match == null || !match.equals(type))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objects[i] = null;&nbsp; &nbsp; }}但是,我怀疑您真正想要的是使用 Stream APIDevice[] filtered = Stream.of(devices)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(d -> Objects.equals(d.getType(), type))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toArray(Device[]::new);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java