Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Flow API replacing Observer and Observable

In Java 9 does Flow API replace Observer and Observable? If not, what does?

like image 234
Denis Murphy Avatar asked Mar 01 '17 13:03

Denis Murphy


People also ask

Is observer pattern deprecated?

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.

Can an Observer be an observable?

This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed. An observable object can have one or more observers. An observer may be any object that implements interface Observer.

What is Flow API in Java?

Flow API is official support for reactive streams specification since Java 9. It is a combination of both Iterator and Observer patterns. The Flow API is an interoperation specification and not an end-user API like RxJava.

Is RxJava observer pattern?

RxJava is an open source library for Java and Android that helps you create reactive code. It's heavily inspired by functional programming. RxJava implements the Observer pattern with two main interfaces: Observable and Observer .


1 Answers

The new Flow API is designed as a common denominator for reactive stream libraries like RxJava and Reactive X. Building on Java 9, they can have their types extend the new interfaces (or so the thought goes). While it is of course charming to use the API inside the JDK, that is not the case in Java 9 and there are no concrete plans to introduce it (to the best of my knowledge).

Regarding Observer and Observable the issue which triggered the deprecation states:

Application developers should consider using java.beans for a richer change notification model. Or they should consider constructs in java.util.concurrent such as queues or semaphores to pass messages among threads, with reliable ordering and synchronization properties.

These are recommendations for application developers for writing new code. It gives no advice on updating existing code or what to do inside the JDK. I guess the reason for that is that both cases are supposed to stay as they are.

Note that Java does not use @Deprecated to necessarily mean "will be removed". Instead it can also mean "use better alternatives" and I think that is the case here. So to answer your question in a few words:

In Java 9 does Flow API replace Observer and Observable

No.

and if it doesn't what does.

Nothing.

like image 109
Nicolai Parlog Avatar answered Sep 23 '22 07:09

Nicolai Parlog