在Java 8 引入了函数式接口后,Supplier 接口变得非常常用,它代表一个无参数的函数,返回一个结果。在某些场景下,我们可能需要将当前对象 this 传递给一个 Supplier。本文将探讨几种实现方式,并分析它们的优劣。
问题背景与需求
假设我们有一个类 Thing
import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; class Thing{ final T value; final CompletableFuture future; Thing(T value) { this.value = value; this.future = new CompletableFuture<>(); } Thing self() { return this; } void reject() { future.cancel(false); } void complete() { // 方式一:使用 Lambda 表达式 future.completeAsync(() -> this); // 方式二:使用方法引用 future.completeAsync(this::self); } }
在上述代码中,我们使用了两种方式将 this 传递给 Supplier:
Lambda 表达式 vs. 方法引用
这两种方式都能达到将 this 传递给 Supplier 的目的,但它们之间存在细微的差异。
lf() 方法的实现发生变化(例如,添加了额外的逻辑),使用方法引用的代码会自动反映这些变化。而使用 Lambda 表达式的代码则需要手动更新。最佳实践与注意事项
虽然上述两种方式都能实现将 this 传递给 Supplier 的目的,但在大多数情况下,直接在需要 this 的地方使用 this 即可。
例如,如果 CompletableFuture 完成后需要访问 Thing 对象的其他字段,可以直接在 Lambda 表达式或 self() 方法中访问 this.value 或调用 this.otherMethod()。
class Thing{ final T value; final CompletableFuture future; Thing(T value) { this.value = value; this.future = new CompletableFuture<>(); } void complete() { future.completeAsync(() -> { // 直接访问 this.value System.out.println("Value: " + this.value); return this; }); } }
总结
将 this 传递给 Supplier 可以通过 Lambda 表达式或方法引用实现。方法引用通常更简洁、可读性更强,并且可能略微高效。然而,在大多数情况下,直接在需要 this 的地方使用 this 即可,避免不必要的复杂性。选择哪种方式取决于具体的场景和个人偏好。 关键在于保持代码的清晰性和可维护性。