在Java 9中不推荐使用Observer。我们应该使用什么而不是它? [英] Observer is deprecated in Java 9. What should we use instead of it?

查看:349
本文介绍了在Java 9中不推荐使用Observer。我们应该使用什么而不是它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 9问世了, Observer 已被弃用。
为什么?这是否意味着我们不应该再实施观察者模式了?

Java 9 came out, and Observer has been deprecated. Why is that? Does it mean that we shouldn't implement observer pattern anymore?

知道什么是更好的选择会很好吗?

It would be good to know what is a better alternative?

推荐答案

为什么?这是否意味着我们不应再实施观察者模式了?

首先回答后一部分 -

Answering the latter part first -

,这确实意味着您不应该实施 Observer Obervable s。

为什么要弃用它们? -

Why were they deprecated -

它们没有为应用程序提供足够丰富的事件模型。例如,他们只支持某些内容发生变化的概念,但没有传达任何有关更改内容的信息。

They didn't provide a rich enough event model for applications. For example, they could support only the notion that something has changed, but didn't convey any information about what has changed.

Alex的回答很好地预先提出 Observer 有一个弱点:所有可观察的是相同的。您必须实现基于 instanceof 的逻辑,并将对象转换为具体类型为 Observable.update()方法。

Alex's answer puts it nicely upfront that Observer has a weakness: all Observables are the same. You have to implement the logic that is based on instanceof and cast object to concrete type into Observable.update() method.

要添加它,有一个像无法序列化 Observable 类,因为它没有实现 Serializable 界面和所有其成员是私人的。

To add to it there were bugs like one could not serialize the Observable class because as it didn't implement Serializable interface and all of its members were private.

有什么更好的替代方案?

另一方面监听器有很多类型,他们有回调方法,不需要强制转换。正如@Ravi在其答案中指出的那样,您可以使用 PropertyChangeListener

On the other hand Listeners have a lot of types and they have callback methods and doesn't require casting. As pointed by @Ravi in his answer you can make use of PropertyChangeListener instead.

对于其余部分, @Deprecation 已经标记了适当的文档,以探索其他链接在其他包中答案也是如此。

For the rest of it the @Deprecation has been marked with proper documentation to explore other packages as linked in other answers as well.

请注意,弃用也标有分析,如此邮件 -

Note that the deprecation was also marked with an analysis as stated in this mail -


现在,遇到这些问题的任何人在使用 RxJava 或其他反应流框架时可能会因为
错误而遇到它们。在
这种情况​​下,用户通常会想要使用jdk9
java.util.concurrent.Flow API,所有的反应流框架
应该在他们计划即将推出的
jdk9兼容版本中兼容/互操作。

These days, anyone encountering these is probably hitting them by mistake while using RxJava or other reactive-stream frameworks. In which case, users will normally want to instead use the jdk9 java.util.concurrent.Flow APIs that all reactive-streams frameworks should be compatible/interoperable within their planned upcoming jdk9-compatible versions.

编辑 :还值得一提的是,API的弃用主要不仅仅是因为上述原因,而且还无法维护一些错误报告的评论中提到的遗留代码(上面链接) )这是为了在一种或另一种方式上改进其实施而提出的。

Edit: Its also worth mentioning that the deprecation of the APIs is not primarily just because of the above reason but also being unable to maintain such legacy code as mentioned in comments of few of the bug reports(linked above) which were raised to mark an improvement in its implementaion in one or the other way.

这篇关于在Java 9中不推荐使用Observer。我们应该使用什么而不是它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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