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
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