假设我有一个名为Server
的课程,我想允许其他人为此编写Plugins
。说Plugin
是一个扩展Runnable
的接口,并添加了一个方法:void init(...)
。收集数据并将其发送到服务器是一个插件的工作。但是,到了将数据发送到服务器的时候,它是如何做到的?来自C和C ++我正在寻找一个函数指针的思路。虽然我没有在Java标准类库之外找到示例,但似乎可以在Java中使用。
如何将方法引用传递给init
方法,使其可以由Plugin
存储,然后在插件想要发送数据时如何调用方法?现在说所需的Server方法是: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
// ...
}
答案 0 :(得分:3)
使用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);
}
答案 1 :(得分:2)
如果方法是void sendData(Integer data)
,它对应于采用Integer并返回内置Consumer<Integer>
接口所涵盖的void的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);
答案 2 :(得分: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。
我希望,这有助于
答案 3 :(得分: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(...);
...
}
}
答案 4 :(得分:0)
interface Server{
...
void sendData(String message);
}
插件不需要函数引用,您可以使用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
// ...
} }