Observer 在 Java 9 中已被弃用.我们应该用什么来代替它? [英] Observer is deprecated in Java 9. What should we use instead of it?

查看:38
本文介绍了Observer 在 Java 9 中已被弃用.我们应该用什么来代替它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

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

先回答后半部分——

YES,这确实意味着你不应该再实现 ObserverObervables.

为什么不推荐使用 -

他们没有为应用程序提供足够丰富的事件模型.例如,他们只能支持某些事情发生了变化的概念,但没有传达任何关于发生了什么变化的信息.

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 有一个弱点:所有Observable 是一样的.您必须实现基于 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.

有什么更好的替代方法?

另一方面,Listeners 有很多类型,它们有回调方法,不需要强制转换.正如@Ravi 在他的 answer 中指出的那样,您可以使用 PropertyChangeListener 代替.

On the other hand Listeners have a lot of types and they have callback methods and don'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 或其他响应式流框架时出错.在在这种情况下,用户通常希望使用 jdk9java.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: It's 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 a few of the bug reports (linked above) which were raised to mark an improvement in its implementation in one or another way.

这篇关于Observer 在 Java 9 中已被弃用.我们应该用什么来代替它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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