0

我有很多类,每个类都实现GeneralInterface并可能附加功能接口:

interface GeneralInterface {}

class MyObjectTypeOne implements GeneralInterface { /*...*/}

class MyObjectTypeTwo implements GeneralInterface, InterOne { /*...*/}

class MyObjectTypeThr implements GeneralInterface, InterOne, InterTwo { /*...*/}

我有一个包含这些MyObjectTypeXXX实例的列表

class ListHolder {

    public static List<GeneralInterface> list = new ArrayList<>();

    ListHolder() {

        list.add(new MyObjectTypeOne());
        list.add(new MyObjectTypeTwo());
        list.add(new MyObjectTypeTwo());
        // add any number of any of the types
    }
}

和20-40个功能接口。这里有 2 个例子:

@FunctionalInterface
public interface InterOne {

    boolean onInterOne();

    static void iterate() {

        for (GeneralInterface obj : ListHolder.list) {
            if (obj instanceof InterOne) {
                if (((InterOne) obj).onInterOne())
                    System.out.println("yes");
            }
        }
    }
}

@FunctionalInterface
public interface InterTwo {

    boolean onInterOne(String string);

    static void iterate(String string) {

        for (GeneralInterface obj : ListHolder.list) {
            if (obj instanceof InterTwo) {
                if (((InterTwo) obj).onInterTwo(string))
                    System.out.println("yes");
            }
        }
    }
}

在代码的不同位置,我需要调用不同的iterate方法:

InterTwo.iterate("S");
InterOne.iterate();

我的问题是,我需要维护iterate所有功能接口的方法,而它们实际上都在做同样的事情:检查对象是否实现了该接口(强制转换)并使用给定的参数调用其唯一的抽象方法。

有没有一种方法,通过语法或设计,只维护一个这样做的方法?我知道通过反思有一种不好的方式来做到这一点(我展示它只是为了表明我做了我的研究,我不想要它):

static void iterate(Class<?> clazz, Object arg) {

    for (GeneralInterface obj : ListHolder.list) {
        if (clazz.isAssignableFrom(obj.getClass())) {
            Method[] methods = clazz.getMethods();
            Method functional;
            for (Method m : methods) {
                if (m.getModifiers() == Modifier.ABSTRACT) {
                    functional = m;
                    break;
                }
            }
            if ((boolean) functional.invoke(obj, arg)) // cast arg or do some other trick
                System.out.println("yes");
        }
    }
}
4

1 回答 1

0

我认为您的代码有几点需要改进,但我只专注于为您的具体问题提供回应。我认为您需要将该通用方法传递给中间抽象类:

abstract class MyObjectTypeAbstract {
   abstract boolean onInter(String ... testString);
   abstract boolean onInter();
   void iterate(String string) {

       for (GeneralInterface obj : ListHolder.list) {
           if (obj instanceof InterTwo) {
               if (onInter(testString))
                   System.out.println("yes");
           }
       }
   }
   void iterate() {

       for (GeneralInterface obj : ListHolder.list) {
           if (obj instanceof InterTwo) {
               if (onInter())
                   System.out.println("yes");
           }
       }
   }
}

这样做是为了让您实现唯一似乎与您提供的每个FunctionalInterface不同的东西。那就是onInter方法。那好像不一样了。所以我做了这个摘要。其余的都是共享的。我还从 iterate 方法中取出了静态,以便它们可以访问 onInter 方法的不同实现。希望你不需要这个是静态的。所以我提出的解决方案是让你的类扩展这个抽象类并像现在一样实现接口。

于 2016-05-14T21:36:23.893 回答