如何将方法或构造函数绑定到功能接口


要将方法或构造函数绑定到功能接口,我们将使用Burningwave Core库的FunctionalInterfaceFactory。FunctionalInterfaceFactory组件用于缓存所有生成的功能接口,以加快访问速度。在开始编码之前,必须将以下依赖项添加到pom.xm l中:

<dependency>
    <groupId>org.burningwave</groupId>
    <artifactId>core</artifactId>
    <version>8.12.6</version>
</dependency>

构造函数绑定

要将构造函数绑定到功能接口,我们将使用以下构造函数:

private Service(String name, String... items) {
    this.id = UUID.randomUUID().toString();
    this.name = name;
    this.items = items;
    logInfo("\nMultiparameter constructor:\n\tid: {}\n\tname: {} \n\titems: {}",
            this.id, this.name, String.join(", ", this.items));
}

...现在,让我们看一下绑定和调用生成的功能接口所需的代码:

ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
  componentContainer.getFunctionalInterfaceFactory();
BiFunction<String, String[], Service> serviceInstantiator =
  functionalInterfaceFactory.getOrCreate(Service.class, String.class, String[].class);
Service service = serviceInstantiator.apply("Service name", new String[] {"item 1", "item 2"});

方法绑定

要将方法绑定到功能接口,我们将使用以下方法:

private Long reset(String name, String... items) {
    this.id = UUID.randomUUID().toString();
    this.name = name;
    this.items = items;
    logInfo("\nMultiparameter method:\n\tid: {}\n\tname: {} \n\titems: {}", this.id,
            this.name, String.join(", ", this.items));
    return System.currentTimeMillis();
}

...现在,让我们看一下绑定和调用生成的功能接口所需的代码:

ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
  componentContainer.getFunctionalInterfaceFactory();
MultiParamsFunction<Long> methodInvoker =
  functionalInterfaceFactory.getOrCreate(Service.class, "reset", String.class, String[].class);
Long currentTimeMillis = methodInvoker.apply(service, "Service One", new String[] {"item 1", "item 2"});

无效方法绑定

要将void方法绑定到功能接口,我们将使用以下方法:

private void voidReset(String... name) {
    this.id = UUID.randomUUID().toString();
    this.name = name[0];
    this.items = null;
    logInfo("\nSingle parameter void varargs method:\n\tname: {}", this.name);
}

...现在,让我们看一下绑定和调用生成的功能接口所需的代码:

ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
  componentContainer.getFunctionalInterfaceFactory();
BiConsumer<Service, String[]> methodInvoker =
  functionalInterfaceFactory.getOrCreate(Service.class, "voidReset", String[].class);
methodInvoker.accept(service, new String[] {"Service Two"});

绑定到具有布尔返回值的方法

要将方法与布尔返回值绑定到功能接口,我们将使用以下方法:

private boolean resetWithBooleanReturn(String... name) {
    this.id = UUID.randomUUID().toString();
    this.name = name[0];
    this.items = null;
    logInfo("\nSingle parameter varargs method with boolean return:\n\tname: {}", this.name);
    return true;
}

...现在,让我们看一下绑定和调用生成的功能接口所需的代码:

ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
  componentContainer.getFunctionalInterfaceFactory();
BiPredicate<Service, String[]> methodInvoker =
  functionalInterfaceFactory.getOrCreate(Service.class, "resetWithBooleanReturn", String[].class);
boolean executed = methodInvoker.test(service, new String[] {"Service Two"});


原文链接:http://codingdict.com