在定义API时我应该返回CompletableFuture还是Future? [英] Should I return CompletableFuture or Future when defining API?

查看:145
本文介绍了在定义API时我应该返回CompletableFuture还是Future?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 8中,接口或抽象类更好地定义返回 CompletableFuture 的API,而不是返回 Future ?考虑到 Future 转换为丑陋 CompletableFuture 以及 CompletableFuture 这一事实将使调用者更灵活地直接使用功能样式, API只是返回 Future 的一个很好的理由?

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?

推荐答案

我的2 cts:


  • 通过返回Future,您可以保持选项打开并返回Future或CompletableFuture - 它与调用者的观点没有区别。

  • 通过返回CompletableFuture,您给调用者更多选项(他们获得更多方法)但你也承诺返回那种类型的Future - 如果在两年内你意识到返回BetterFuture会更有意义,你将不得不改变API,这是不好的。

所以你应该评估一下你以后想要返回除CompletableFuture以外的东西的可能性(哈哈)并做出相应的决定。

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.

这篇关于在定义API时我应该返回CompletableFuture还是Future?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆