In Java 8, is it better for interface or abstract class to define APIs returning CompletableFuture
instead of returning Future
? Considering it is ugly converting Future
to CompletableFuture
and the fact that CompletableFuture
will give the caller more flexibility of using functional style directly, what could be a good reason for an API to just return Future
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Thought I would come back to this and provide some updates on my final decisions:
For my own code/design, I went with using
CompletableFuture
as the return type, becauseprotected abstract
method of an internal part that I want to make extensible;CompletableFuture
is an added benefit/reminder/encouragement of using functional style to the future devs.With that being said, I would definitely use the
CompletableStage
interface as the return type, had I been designing a public API, because:CompletableStage
has a methodCompletableFuture<T> toCompletableFuture()
.My 2 cts:
So you should probably assess the likelihood that you will want to return something other than a CompletableFuture in the future (haha) and decide accordingly.