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

前端 未结 5 1153
再見小時候
再見小時候 2020-11-27 11:24

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

5条回答
  •  自闭症患者
    2020-11-27 12:00

    Why Observer is deprecated in Java 9?

    Ans: The Observable class and the Observer interface have been deprecated in Java 9 because 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.

    See Java doc 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.

    Reactive Streams or Flow API:

    Flow is a class introduced in Java 9 and has 4 interrelated interfaces : Processor, Publisher, Subscriber and Subscription.

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

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

    Flow.Subscriber : A receiver of messages.

    Flow.Subscription: Message control linking a Flow.Publisher and Flow.Subscriber.

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

提交回复
热议问题