6

假设我有一个名为 的类Server,我想允许其他Plugins人为它编写。SayPlugin是一个扩展Runnable并添加了一个方法的接口:void init(...). 插件的工作是收集数据并将其发送到服务器。然而,当需要向服务器发送数据时,它是如何做到的呢?来自 C 和 C++,我正在寻找沿着函数指针的思路。尽管我没有在 Java 标准类库之外找到示例,但在 Java 中似乎是可能的。

如何将方法引用传递给init方法,以便它可以由 存储Plugin,然后在插件想要发送数据时如何调用该方法?现在说所需的服务器方法是:void sendData(Integer data).

例如:

// Inside Server
Plugin p = new PluginImplementation();
p.init(this::sendData);    

// Plugin init
public void init(?? sendMethod) {
    storedSendMethod = sendMethod;
    // ...
}

// Plugin run
public void run() {
    // ...
    storedSendMethod(x) // Sends data to server
    // ...
}
4

5 回答 5

5

使用java.util.function.Function我们可以将函数作为参数传递给方法,然后将apply()其应用于相关参数。这是一个例子:

import java.util.function.Function;

public class FunctionDemo {

    // we will pass a reference to this method
    public static Integer square(Integer x) {
        return x * x;
    }

    // this method accepts the function as an argument and applies it to the input: 5
    public static Integer doSomething(Function<Integer, Integer> func) {
        return func.apply(5);
    }

    public static void main(String[] args) {
        // and here's how to use it
        System.out.println(doSomething(FunctionDemo::square)); // prints 25
    }   
}

具有多个参数的附加版本(作为数组传递):

public static Integer sum(Integer[] x) {
    Integer result = 0;
    for(int i = 0; i < x.length; i++)
        result += x[i];
    return result;
}

public static void main(String[] args) {
    Integer[] arr = {1,2,3,4,5};
    System.out.println(doSomething(Play::sum, arr));
}

public static Integer doSomething(Function<Integer[], Integer> func,
                                  Integer[] arr) {        
    return func.apply(arr);
}
于 2015-08-27T03:11:03.493 回答
2

如果该方法void sendData(Integer data)对应于一个消费者,该消费者接受一个整数并返回一个被内置Consumer<Integer>接口覆盖的 void,该接口具有一个accept(Integer)在调用时将调用您的函数的方法。

因此,您的代码将如下所示:

public void init(Consumer<Integer> sendMethod) {
    storedSendMethod = sendMethod;
    // ...
}

// Plugin run
 void run() {
    // ...
    storedSendMethod.accept(x) // Sends data to server
   // ...
}

作为旁注,拥有一个init方法可能是一个糟糕的 Java 设计。如果可能的话,你最好将初始化移动到构造函数

Plugin p = new PluginImplementation( this::sendData);
于 2015-08-27T03:12:33.157 回答
1

在java中,你用回调来做,这是你的回调接口,

public interface SendCallback {
    public void doSend(Object toSend);
}

这是插件接口,所有插件都必须实现这个接口

public interface Plugin extends Runnable {
    public void init(SendCallback callback);
}

这是服务器的代码。

public class Server {

    Plugin plugin;

    SendCallback callback = new SendCallback() {
        public void doSend(Object toSend) {
                // logic to send object 'toSend'
        }
    }

    public Server() {
        plugin = new MyPlugin();
        plugin.init(callback);
    }

}

这是您的插件实现。

public class MyPlugin implements Plugin {
    SendCallback callback = null;
    Object x = null;

    public void init(SendCallback callback) {
        this.callback = callback;
    }
    public void run() {
        x = "Somthing"; // initialize the x object
        callback.doSend(x);
    }
} 

您会注意到,服务器定义了回调实现。该插件将调用回调的方法 doSend。

我希望这有帮助

于 2015-08-27T03:22:39.820 回答
0

Java 8 中有方法引用,但是您可以只传递整个对象并调用其 sendData() 方法。在“插件”的情况下,使用每个接口都有助于插件和服务器的“松散”耦合。

public interface Server {
    void setData(...);
}

public class MyPlugin implements plugin {

   private Server server;   

   void init(Server s )  {
       this.server = s;
   }

   void run() {
      ...
      this.server.setData(...);
      ...
   }

}
于 2015-08-27T03:25:44.510 回答
0
interface Server{
  ...
  void sendData(String message);
}

Plugin 不需要函数引用,您可以使用 Server 接口通知 Plugin 了解该方法。

class PluginX implements Plugin{
    ...
    private Server server;
    void init(Server server) {
        this.server = server;
    }

    public void run() {
    // ...
        server.sendData(x) // Sends data to server
    // ...

} }

于 2015-08-27T03:25:50.820 回答