java 8 中如何使用 consumer 和 supplier 来代替 Reflection
How to use consumer and supplier instead Reflection in java 8
我有两个简单的 class 并使用反射模式调用方法。
我想写模块编程。
假设我们在数据库中有一个 table 保留模块名称:
Id moduleName methodName ClassName active
1 sample a com.examle.sample true
2 SMS sendSMS com.example.SMS false
3 Email sendEmail com.example.Email false
... ... ... ...
当 active 为真时,模块必须是 activated.So 当我编写程序并编译它时,我不喜欢再次编译整个 MyApp。所以我使用反射模式来调用模块。请查看代码。
public class Sample {
public void a() {
System.out.println("Call Method a");
}
}
public class SMS {
public void sendSMS(String str) {
System.out.println("send SMS ok");
}
}
public class Email {
public void sendEmail(String str) {
System.out.println("send Email ok");
}
}
public class SampleMainClass {
public static void main(String[] args) {
//coonect to database and fetch all record in tables
while(record.next){
if (record.getActive()){
Object o = Class.forName(record.getClssName()).newInstance() ;
Method method = o.getClass().getDeclaredMethod(record.getMethodName());
method.invoke(o);
}
}
}
产出
Call Method a
所以我在 java 8 中听说,反射模式已被弃用,我们可以使用消费者和供应商。
java8 中如何使用消费者和供应商代替反射?
谢谢。
Consumer
和Supplier
是函数式接口,没有引用反射。
这是不同的东西。
So i heard in the java 8, reflection pattern is deprecate and instead that we can use consumer and supplier.
错误信息。
public class Q42339586 {
static class Sample {
void a() { System.out.println("a() called"); }
void b() { System.out.println("b() called"); }
}
static <T> void createInstanceAndCallMethod(
Supplier<T> instanceSupplier, Consumer<T> methodCaller) {
T o = instanceSupplier.get();
methodCaller.accept(o);
}
public static void main(String[] args) {
createInstanceAndCallMethodJava8(Sample::new, Sample::a);
}
}
在这里,createInstanceAndCallMethod
执行您的 main() 方法中执行的操作,但它接受参数。
A Supplier
用于创建新实例,而 Consumer
用于调用该实例的特定方法。在示例中,两个方法引用都作为两个参数传递。相反,您也可以使用 lambda 表达式并改为编写 () -> new Sample()
和 o -> o.a()
。详情请参考this official tutorial part。
相对于反射的优势显而易见:
- 您不能要求
createInstanceAndCallMethod
创建一个 class 不存在的实例。
- 您不能要求
createInstanceAndCallMethod
调用特定 class 中不存在的方法。
- 作为两者的结果,您不必处理任何已检查的异常。
当然,这只有在代码中的某个地方实际 class 和方法已知时才有效,例如无法从属性文件中读取 class 和方法名称,然后使用 Java 8 种机制来安全地创建实例并调用特定方法。
我有两个简单的 class 并使用反射模式调用方法。 我想写模块编程。 假设我们在数据库中有一个 table 保留模块名称:
Id moduleName methodName ClassName active
1 sample a com.examle.sample true
2 SMS sendSMS com.example.SMS false
3 Email sendEmail com.example.Email false
... ... ... ...
当 active 为真时,模块必须是 activated.So 当我编写程序并编译它时,我不喜欢再次编译整个 MyApp。所以我使用反射模式来调用模块。请查看代码。
public class Sample {
public void a() {
System.out.println("Call Method a");
}
}
public class SMS {
public void sendSMS(String str) {
System.out.println("send SMS ok");
}
}
public class Email {
public void sendEmail(String str) {
System.out.println("send Email ok");
}
}
public class SampleMainClass {
public static void main(String[] args) {
//coonect to database and fetch all record in tables
while(record.next){
if (record.getActive()){
Object o = Class.forName(record.getClssName()).newInstance() ;
Method method = o.getClass().getDeclaredMethod(record.getMethodName());
method.invoke(o);
}
}
}
产出
Call Method a
所以我在 java 8 中听说,反射模式已被弃用,我们可以使用消费者和供应商。
java8 中如何使用消费者和供应商代替反射?
谢谢。
Consumer
和Supplier
是函数式接口,没有引用反射。
这是不同的东西。
So i heard in the java 8, reflection pattern is deprecate and instead that we can use consumer and supplier.
错误信息。
public class Q42339586 {
static class Sample {
void a() { System.out.println("a() called"); }
void b() { System.out.println("b() called"); }
}
static <T> void createInstanceAndCallMethod(
Supplier<T> instanceSupplier, Consumer<T> methodCaller) {
T o = instanceSupplier.get();
methodCaller.accept(o);
}
public static void main(String[] args) {
createInstanceAndCallMethodJava8(Sample::new, Sample::a);
}
}
在这里,createInstanceAndCallMethod
执行您的 main() 方法中执行的操作,但它接受参数。
A Supplier
用于创建新实例,而 Consumer
用于调用该实例的特定方法。在示例中,两个方法引用都作为两个参数传递。相反,您也可以使用 lambda 表达式并改为编写 () -> new Sample()
和 o -> o.a()
。详情请参考this official tutorial part。
相对于反射的优势显而易见:
- 您不能要求
createInstanceAndCallMethod
创建一个 class 不存在的实例。 - 您不能要求
createInstanceAndCallMethod
调用特定 class 中不存在的方法。 - 作为两者的结果,您不必处理任何已检查的异常。
当然,这只有在代码中的某个地方实际 class 和方法已知时才有效,例如无法从属性文件中读取 class 和方法名称,然后使用 Java 8 种机制来安全地创建实例并调用特定方法。