在 Java 9 中不推荐使用 Observer。我们应该使用什么来代替它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46380073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:19:58  来源:igfitidea点击:

Observer is deprecated in Java 9. What should we use instead of it?

javajava-8deprecatedobserver-patternjava-9

提问by curious95

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

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

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

最好知道什么是更好的选择?

采纳答案by Naman

Why is that? Does it mean that we shouldn't implement observer pattern anymore?

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

Answering the latter part first -

先回答后半部分——

YES, it does mean you shouldn't implement Observerand Obervables anymore.

是的,这确实意味着你不应该再实现ObserverandObervables 了。

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's answerputs it nicely upfront that Observerhas a weakness: all Observables are the same. You have to implement the logic that is based on instanceofand cast object to concrete type into Observable.update()method.

亚历克斯的回答很好地预先提出Observer了一个弱点:所有Observables 都是相同的。您必须实现基于instanceof并将对象转换为具体类型的逻辑到Observable.update()方法中。

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

添加到它有像一个不能序列化Observable类的错误因为它没有实现Serializable接口,它的所有成员都是私有的。

What is a better alternative to that?

什么是更好的替代方案?

On the other hand Listenershave a lot of types and they have callback methods and don't require casting. As pointed by @Ravi in his answeryou can make use of PropertyChangeListenerinstead.

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

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

对于其余部分,@Deprecation已标记有适当的文档,以探索其他答案中链接的其他软件包。



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

请注意,弃用还标有此邮件中所述的分析 -

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

如今,遇到这些问题的任何人都可能在使用RxJava或其他反应式流框架时误触它们。在这种情况下,用户通常希望改用 jdk9 java.util.concurrent.FlowAPI,所有反应流框架都应该在他们计划的即将推出的与 jdk9 兼容的版本中兼容/互操作。

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.

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

回答by Ousmane D.

Considering that the Observableclass and the Observerinterface have been deprecated as of Java 9. As per the post Java's Observer and Observable Are Deprecated in JDK 9

考虑到Observable类和Observer接口已从 Java 9 开始弃用。根据帖子Java 的观察者和观察者在 JDK 9 中已弃用

The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the java.beanspackage. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrentpackage. For reactive streams style programming, see the Flow API.

Observer 和 Observable 支持的事件模型非常有限,Observable 传递通知的顺序是不确定的,状态变化与通知不是一一对应的。对于更丰富的事件模型,请考虑使用java.beans包。对于线程之间可靠且有序的消息传递,请考虑使用java.util.concurrent包中的并发数据结构之一 。对于反应式流风格的编程,请参阅 Flow API。

回答by Ravi

Yes, it is deprecated in Java 9. And, we can't implement observer pattern anymore.

是的,它在Java 9 中已被弃用。而且,我们不能再实现观察者模式了。



Why is that?

这是为什么?

There are more reasons :

还有更多原因:

Not Serializable- Since, Observable doesn't implements Serializable. So, you can't Serialize Observable neither its subclass.

不可序列化- 因为 Observable 不实现可序列化。所以,你不能序列化 Observable 也不能序列化它的子类。

No Thread Safety- The methods can be overridden by its subclasses, and event notification can occur in different orders and possibly on different threads, which is enough to disrupt any "thread safety".

没有线程安全——这些方法可以被它的子类覆盖,事件通知可以以不同的顺序发生,也可能发生在不同的线程上,这足以破坏任何“线程安全”。

Less to offer-

少提供-

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

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

Open Issues- As mentioned, there were lot of major issues raised (thread safety, Serializable) and most of them had complexities to fix and still "not fixed" or No Active Development, and that is the reason why it has been deprecated.

未解决的问题- 如前所述,提出了许多主要问题(线程安全、可序列化),其中大多数问题都需要修复,但仍然“未修复”或“没有积极开发”,这就是它被弃用的原因。

I would also recommend to read this answer Why should the observer pattern be deprecated?, @Jeff has explained other reasons for deprecation.

我还建议阅读这个答案为什么不推荐使用观察者模式?,@Jeff 解释了弃用的其他原因。



So, what's the alternative we have ?

那么,我们有什么选择呢?

You can use PropertyChangeEventand PropertyChangeListenerfrom java.beanspackage.

您可以使用PropertyChangeEventPropertyChangeListenerjava.beans包。

回答by Mohit Tyagi

Why Observer is deprecated in Java 9?

为什么在 Java 9 中不推荐使用 Observer?

Ans:The Observableclass and the Observerinterface have been deprecated in Java 9 because the event model supported by Observerand Observableis quite limited, the order of notifications delivered by Observableis unspecified, and state changes are not in one-for-one correspondence with notifications.

答:Observable类和Observer接口都在Java中9遭到反对,因为该事件模型的支持Observer,并Observable是非常有限的,通知被传递的顺序Observable是不确定的,并且状态变化不是一个换一个对应于通知。

See Java doc https://docs.oracle.com/javase/9/docs/api/java/util/Observable.html

请参阅 Java 文档https://docs.oracle.com/javase/9​​/docs/api/java/util/Observable.html

Alternate of Observer pattern?

观察者模式的替代方案?

There are many alternatives of Observer design pattern and Reactive Streams is one of them.

Observer 设计模式有很多替代方案,Reactive Streams 就是其中之一。

Reactive Streams or Flow API:

反应式流或流 API

Flowis a class introduced in Java 9 and has 4 interrelated interfaces : Processor, Publisher, Subscriberand Subscription.

Flow是一类在Java中9中引入并具有4个相互关联的接口:ProcessorPublisherSubscriberSubscription

Flow.Processor: A component that acts as both a Subscriber and Publisher.

Flow.Processor:同时充当订阅者和发布者的组件。

Flow.Publisher: A producer of items received by Subscribers.

Flow.Publisher:订阅者收到的项目的生产者。

Flow.Subscriber: A receiver of messages.

Flow.Subscriber: 消息的接收者。

Flow.Subscription: Message control linking a Flow.Publisherand Flow.Subscriber.

Flow.Subscription: 链接 aFlow.Publisher和 的消息控制Flow.Subscriber

See Java doc https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html

请参阅 Java 文档https://docs.oracle.com/javase/9​​/docs/api/java/util/concurrent/Flow.html