RxJava’s Essential Characteristics
Get introduced to some key components of RxJava.
Observer pattern
RxJava is a classic example of the observer pattern. We start with the Observable
, which is the source of our data. Then we have one or more Observer
elements, which subscribe to the Observable
and are notified when there is a new event. This allows for a push-based mechanism, which is usually far more ideal than continually polling for new events.
RxJava adds to the traditional observer pattern by having the Observable
signal completion and errors along with regular events, which we’ll see in RxJava Core Components.
Iterator pattern
With a traditional iterator pattern, the iterator pulls data from the underlying collection, which would implement some sort of Iterable interface. What Erik Meijer essentially did in creating Rx was flip the iterator pattern on its head, turning it into a push-based pattern.
The Observable
was designed to be the dual of the Iterable
. Instead of pulling data out of an Iterable
with .next()
, the Observable
pushes data to an Observer
using .onNext()
. So, where the iterator pattern uses synchronous pulling of data, RxJava allows for asynchronous pushing of data, allowing code to be truly reactive.
Functional programming
One of the most important aspects of RxJava is that it uses functional programming, in particular, with its Operators
. Functional programming is programming with pure functions.
Pure functions
A pure function is one that satisfies the following two conditions:
- The function always returns the same value given the same input (that is, it’s deterministic). This implies