Observer 在 Java 中被弃用 9. 我们应该用什么来代替它?

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

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

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

考虑到 Observable class 和 Observer 接口已从 Java 9 开始弃用。根据 post Java's Observer and Observable Are Deprecated in 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.beans package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrent package. For reactive streams style programming, see the Flow API.

还有更多原因:

Not Serializable - 因为,Observable 没有实现 Serializable。因此,您不能序列化 Observable 及其子类。

No 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

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

我还建议阅读这个答案 Why should the observer pattern be deprecated?,@Jeff 解释了弃用的其他原因。


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

您可以使用 PropertyChangeEvent and PropertyChangeListener from java.beans 包。

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

先回答后半部分 -

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

Why were they deprecated -

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

Alex's answer 很好地预先指出 Observer 有一个弱点:所有 Observable 都是相同的 。您必须实现基于 instanceof 的逻辑并将对象转换为具体类型到 Observable.update() 方法中。

除此之外还有像 could not serialize the Observable class 这样的错误,因为它没有实现 Serializable 接口并且它的所有成员都是私有的。

有什么更好的选择?

另一方面,Listeners 有很多类型,它们有回调方法,不需要转换。正如@Ravi 在他的 you can make use of PropertyChangeListener 中指出的那样。

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


请注意,弃用还标有 this mail -

中所述的分析

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 的弃用主要不只是因为上述原因,还有无法维护一些错误报告(上面链接)的评论中提到的遗留代码,这些错误报告的提出是为了以某种方式改进其实施。

Why Observer is deprecated in Java 9?

回答: Observable class 和 Observer 接口已在 Java 9 中弃用,因为事件模型ObserverObservable支持的非常有限,Observable传递通知的顺序不明确,状态变化与通知不是一一对应的。

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

Alternate of Observer pattern?

Observer设计模式有很多选择,Reactive Streams就是其中之一。

反应流或流 API:

Flow 是在 Java 9 中引入的 class,有 4 个相互关联的接口:ProcessorPublisherSubscriberSubscription.

Flow.Processor : 一个既充当订阅者又充当发布者的组件。

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

Flow.Subscriber : 消息接收者。

Flow.Subscription:链接 Flow.PublisherFlow.Subscriber 的消息控件。

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

问题出在 Java class/interface 名为 Observer、Observable 等的有缺陷的 Java 实现上 - 但不是 GoF 观察者模式。

也可以使用 CDi 2.0,Event Handling