Starting the Reactive Journey

Starting the Reactive Journey

If you’re an Android developer, then odds are you’ve heard of RxJava.  It’s a popular library that utilizes the observer pattern.  Rx (ReactiveX) allows us to program reactively, meaning our code can be executed as events happen.  It’s tough to fully wrap your head around it at first, but once you get it your codebase can become more efficient and clean.

The Observer Pattern:

The Observer pattern (and RxJava) revolves around three key concepts.  The three O’s: observables, observers, and operators.  When combined these three allow us to emit information, modify it, and then react to it.  The best way to think about the observable pattern is to consider everything a stream. Data starts at the top of a stream and flows downhill.  On its way down things can happen to it to modify the data, and then at the bottom of the hill we use the data.

This is a high-level view of how the observer pattern works.  Think of observables as the top of the stream, operators changing the data on its way down, and observers as the bottom of the hill.  Ok, maybe this analogy will make a little more sense after you learn a little more about each of these O’s.

Observables:

An observable is something that can be observed.  It is something that emits data that we can react to.  A good example that already exists in Android is a button.  Whenever a button is pressed by the user it emits data. That data is basically just saying “I was pressed”, but it’s still data emitted from an observable!  What we do with that data is our option and none of the observable’s concern (think about the standard Android button; it just knows it was pressed, we write the logic for what it does).

So an observable is…literally something that is observable.  Real life examples could be a movie playing or a singer singing. These are things we could watch happen, but they don’t really impact us until we observe them.  Which brings us to the bottom of the stream.

Observers:

We’ll skip over operators right now and jump straight to observers because they are essential to this paradigm. So we have a movie playing already (the observable), and once we turn on the TV to watch it we have become the observer. The data (movie) is coming to us and we can react to it however we want.  Likewise, with an observer in Android we can write a function to react to the data an observable emits.

In the RxJava world observables don’t always have to emit just one piece of data, that’s the beauty of it.  They can emit thousands and thousands of pieces of data, and then observers can react to each and every piece of data if they see fit.  And just like multiple people can watch one tv, multiple observers can observe one observable.

Wrapping Up:

It’s a lot of “o” words I know, but the concept is pretty simple.  Observables emit data, observers react to it.  We’ll dive into plenty of code in part 2, so stay tuned for that as well as examples of how operators come into the mix!  RxJava takes a little while to get down, but once you do it’s too addictive to quit!

Samsung Leaks Unfold
Backing Away From The Back Button

Super Fans always leave a comment :-)

Leave a Comment

Loading Facebook Comments ...