方法数组:适配器模式?

2022-09-04 21:09:32

问题描述:我希望能够将方法列表传递给仅在一个类中定义了方法的其他类。如果这些方法(其中一些具有输入参数和非 void 返回类型)在一个类中定义,我希望能够将其中一些方法的列表(可能具有重复项)作为参数传递给其他类的构造函数。

代码说明:下面的代码是一个粗略的示例,如果它偏离了主要目标,则可以忽略它。除了下面的例子之外,另一个例子是方法为int Add(int n1,int n2),int Subtract(int n1,int n2),乘法等。并且该接口有一个名为int MathOperation(int n1,int n2)的方法。

尝试解决问题:适配器模式似乎具有我正在寻找的功能,但我只看到接口中的方法没有输入或输出参数的示例。我为这个问题编写的一个示例实现发布在下面。

问题类比:您有一个随机图片生成器 Web 服务。有30个突变可以应用于图像。客户端连接并单击“生成”按钮,其中一些函数的随机列表被传递给Web服务中的某个其他类,然后该类继续使用自己的数据运行这些函数,同时还收集并可能重用返回值以生成一些突变的cat图像。它不能只是显式调用另一个类中的方法,因为该过程需要在运行时随机完成。这就是为什么我倾向于生成一个随机方法列表的想法,这些方法在单击“生成”按钮时按顺序执行。

我希望我已经说清楚了。

public class SomeClass {
    ...
    public double UseWrench(double torque, boolean clockwise) { ... }
    public double UsePliers(double torque, boolean clockwise) { ... }
    public double UseScrewDriver(double torque, boolean clockwise) { ... }
    public boolean UseWireCutters(double torque) { ... }

    interface IToolActions {
        double TurnFastener(double torque, boolean clockwise);
        boolean CutWire(double torque);
    }

    private IToolActions[] toolActions = new IToolActions[] {
        new IToolActions() { public double TurnFastener(double torque, boolean clockwise) { double UseWrench(double torque, boolean clockwise); } },
        new IToolActions() { public double TurnFastener(double torque, boolean clockwise) { double UsePliers(double torque, boolean clockwise); } },
        new IToolActions() { public double TurnFastener(double torque, boolean clockwise) { double UseScrewDriver(double torque, boolean clockwise); } },
        new IToolActions() { public boolean CutWire(double torque) { boolean UseWireCutters(double torque); } },
    };
}

public class Worker<T> {

    public List<? extends IToolActions> toolActions;

    public Worker(List<? extends IToolActions> initialToolSet){
        toolActions = initialToolActions;
    }
}

答案 1

虽然@alainlompo有一般的想法,但Java 8通过使用诸如(对于双精度)甚至只是一个类对象之类的东西来大大简化了这一点。事实上,你可以变得非常疯狂,并有一个接受varargs lambdas的方法:BiConsumerConsumer

public class SomeClass

    public double useWrench(double torque, boolean clockwise) { ... }
    public double usePliers(double torque, boolean clockwise) { ... }
    public double useScrewDriver(double torque, boolean clockwise) { ... }
    public boolean useWireCutters(double torque) { ... }

}

public class Worker {

    @SafeVarargs
    public Worker(SomeClass example, Consumer<? extends SomeClass>... operations) {
        for (Consumer bc : operations) {
            bc.accept(example);
        }
    }
}

然后,这很容易简化:

SomeClass c = new SomeClass();
new Worker(c, SomeClass::useWrench, SomeClass:usePliers, SomeClass::useScrewDriver, SomeClass::useWireCutters);

虽然像这样应用它似乎有点尴尬(因为它是一个适配器模式),但你可以很容易地看到它如何应用于类体:

public class SomeClass

    public double useWrench(double torque, boolean clockwise) { ... }
    public double usePliers(double torque, boolean clockwise) { ... }
    public double useScrewDriver(double torque, boolean clockwise) { ... }
    public boolean useWireCutters(double torque) { ... }

    @SafeVarargs
    public void operate(Consumer<? extends SomeClass>... operations) {
        for (Consumer<? extends SomeClass> bc : operations) {
            bc.accept(example);
        }
    }

}

//Elsewheres
SomeClass c = new SomeClass();
c.operate(SomeClass::useWrench, SomeClass:usePliers, SomeClass::useScrewDriver, SomeClass::useWireCutters);

当然,你不需要varargs,它也可以简单地传递一个Collection

但是等等,还有更多!!!

如果你想要一个结果,你甚至可以通过一个使用自返回的方法,例如:Function

public class SomeClass {

    public double chanceOfSuccess(Function<? super SomeClass, ? extends Double> modifier) {
        double back = /* some pre-determined result */;
        return modifier.apply(back); //apply our external modifier
    }

}

//With our old 'c'
double odds = c.chanceOfSuccess(d -> d * 2); //twice as likely!

Java 8 中的 Function API 提供了更大的灵活性,使得像这样的复杂问题变得非常容易编写。


答案 2

@John这就是我如何解决您的问题。

我使用MathOperations的情况来简化它。首先,我认为最好在SomeClass之外拥有这样的界面:

public interface MathOperable {

    public int mathOperation(int n1, int n2);

}

我创建了两个实现此接口的类示例和一个在SomeClass内部实现的匿名实现(我做了一个Add,Multiply和一个匿名的“Substract”)

public class Add implements MathOperable {

    public int mathOperation(int n1, int n2) {

        return n1 + n2;
    }

    public String toString() {
        return "<addition>";
    }

}

覆盖 toString() 只是为了提高我将在文章末尾显示的示例的可读性。

public class Multiply implements MathOperable {

    public int mathOperation(int n1, int n2) {
        // TODO Auto-generated method stub
        return n1 * n2;
    }

    public String toString() {
        return "<multiplication>";
    }

}

这是我的SomeClass类,它包含一个getRandomListOfOperations,我模拟当点击按钮完成时会发生什么

public class SomeClass {

    private static MathOperable addition = new Add();
    private static MathOperable multiplication = new Multiply();

    // Anonymous substraction  
    private static MathOperable substraction = new MathOperable() {

        public int mathOperation(int n1, int n2) {
            // TODO Auto-generated method stub
            return n1-n2;
        }

        public String toString() {
            return "<substraction>";
        }

    };


    public List<MathOperable> getRandomListOfOperations() {

        // We put the methods in an array so that we can pick them up later     randomly
        MathOperable[] methods = new MathOperable[] {addition,     multiplication, substraction};
        Random r = new Random();

        // Since duplication is possible whe randomly generate the number of     methods to send
        // among three so if numberOfMethods > 3 we are sure there will be     duplicates
        int numberOfMethods = r.nextInt(10);
        List<MathOperable> methodsList = new ArrayList<MathOperable>();

        // We pick randomly the methods with duplicates
        for (int i = 0; i < numberOfMethods; i++) {
            methodsList.add(methods[r.nextInt(3)]);

        }

        return methodsList;     
    }

    public void contactSomeOtherClass() {
        new SomeOtherClass(getRandomListOfOperations());
    }
}

现在这是我的SomeOtherClass(可能对应于你的Worker类)

public class SomeOtherClass<T extends MathOperable> {

    Random r = new Random();

    List<T> operations;

    public SomeOtherClass(List<T> operations) {
        this.operations = operations;

        runIt();
    }

    public void runIt() {

        if (null == operations) {
            return;
        }

        // Let's imagine for example that the new result is taken as     operand1 for the next operation
        int result = 0;

        // Here are examples of the web service own datas
        int n10 = r.nextInt(100);
        int n20 = r.nextInt(100);

        for (int i = 0; i < operations.size(); i++) {

            if (i == 0) {
                result = operations.get(i).mathOperation(n10, n20);
                System.out.println("Result for operation N "  + i + " = " +     result);
            } else {

                // Now let's imagine another data from the web service     operated with the previous result
                int n2 = r.nextInt(100);
                result = operations.get(i).mathOperation(result, n2);
                System.out.println("Current result for operation N " + i + "     which is " + operations.get(i) +" = " + result);

            }
        }
    }

}

我有一个简单的测试类,其中包含一个连接两个类的main

public class SomeTestClass {

    public static void main(String[] args) {
        SomeClass classe = new SomeClass();
        classe.contactSomeOtherClass();
    }

}

现在举几个执行的例子:

example1

还有另一个例子!

example 2

我希望这可以有所帮助!


推荐