RxJava2 in Android Part1

RxJava is getting popular to used in the android application. Many of the developers they are supporting to use RxJava in building an android app. They are getting the good response to find that it taking the android app up to next level. 

Rx java is using the Observer design pattern.I followed many article and tutorial for understanding what is observable and observer in technical term and every time got confused. Every article have different-2 explanation So I got more confused How to use RxJava, But most important when and Where to use.

Now Its create the confusion to you also right because to understand the technical thing required to understand first what is usage and 
existence in the real world.Yes sounds Great:).
So Let’s first take some real time example or daily basis usages to understand this technical concept.

Suppose Android point is the tutorial hub for android. Many lists of developers have subscribed for this tutorial. Once any new tutorial added into Android Point then listed subscribed developers will get notified for same that new article or tutorial added in Android point. So here developers are Observer And Android Tutorial point is Observable. So once Observable Object get updated then Observers will get notified.
Still confused. Let me give another example.

Everybody has its own permanent address and this address is registered on your passport and pan card etc. So once your Permanent address gets updated then your card authority get 
notify that something has changed in the permanent address. So here Your authority card are Observer and permanent address is the Observable. Once changes in Observable your observer get notify.
 
I hope now it’s clear about Observable and Observer in real time. Let’s check how is the same relation with technology term.
Any Object has its own property and behaviors. The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state (means the change in his property or behavior ), all of its dependents are notified and updated automatically. Again confusion Ohh !! Let’s connect above example with Java Program.

CardObserver.java

package com.sunil.rxjavaexample;

import java.util.Objects;

/**
 * Created by sunil on 11-02-2017.
 */
   
public abstract class CardObserver {

    protected PermanentAddressObservable permanentAddressObservable;
    public abstract void update();
}

PermanentAddressObservable.java

    
package com.sunil.rxjavaexample;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunil on 11-02-2017.
 */

public class PermanentAddressObservable {

    private List observers = new ArrayList();
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
        notifyAllObservers();
    }

    public void attach(CardObserver observer){
        observers.add(observer);
    }

    public void notifyAllObservers(){
        for (CardObserver observer : observers) {
            observer.update();
        }
    }
}

AuthorityCardAddressObserver.java

    
package com.sunil.rxjavaexample;

/**
 * Created by sunil on 11-02-2017.
 */

public class AuthorityCardAddressObserver extends CardObserver {

    public AuthorityCardAddressObserver(PermanentAddressObservable permanentAddressObservable){
        this.permanentAddressObservable = permanentAddressObservable;
        this.permanentAddressObservable.attach(this);
    }

    @Override
    public void update() {
        System.out.println( "Address Change: "+permanentAddressObservable.getAddress());
    }
}

MainActivity.java

    
package com.sunil.rxjavaexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PermanentAddressObservable permanentAddressObservable = new PermanentAddressObservable();
        new AuthorityCardAddressObserver(permanentAddressObservable);
        System.out.println("Initiated Address Change");
        permanentAddressObservable.setAddress("This is my new Address");
    }
}

Result will be :
02-11 11:44:14.661 9430-9430/com.sunil.rxjavaexample I/System.out: Initiated Address Change 02-11 11:44:14.661 9430-9430/com.sunil.rxjavaexample I/System.out: Address Change: This is my new Address

I hope this is understandable. Now let's focus How Observable and Observer following this pattern.Observable is the class and Observer is the interface. Observable maintain the list of Observers. When Observable Object gets changes then its notify to all Observers to get the update. Again Confused Oh No!!!. Let me again explained by example.

AndroidPointTutorial.java

package com.sunil.rxjavaexample.tutorial;

import java.util.Observable;

/**
 * Created by sunil on 11-02-2017.
 */

public class AndroidPointTutorial extends Observable{

    private String article;

    public AndroidPointTutorial(String value) {
        article = value;
    }

    public String getArticle() {
        return article;
    }

    public void setArticle(String newArticle) {
        if (!article.equals(newArticle)) {
            System.out.println("Article changed to new article: " + article);
            article = newArticle;
            setChanged();
            notifyObservers(article);
        }
    }
}    

Developers.java

package com.sunil.rxjavaexample.tutorial;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by sunil on 11-02-2017.
 */

public class Developers implements Observer{

    private AndroidPointTutorial androidPointTutorial = null;

    public Developers(AndroidPointTutorial androidPointTutorial) {
        this.androidPointTutorial = androidPointTutorial;
    }
    public void update(Observable observable, Object o) {
        System.out.println("Updated :"+androidPointTutorial.getArticle());
    }

}    

MainActivity.java

package com.sunil.rxjavaexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.sunil.rxjavaexample.tutorial.AndroidPointTutorial;
import com.sunil.rxjavaexample.tutorial.Developers;

import java.util.Observable;
import java.util.Observer;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AndroidPointTutorial androidPointTutorial = new AndroidPointTutorial("Original Article");
        Developers developers = new Developers(androidPointTutorial);
        androidPointTutorial.addObserver(developers);

        androidPointTutorial.setArticle("New Article");  // this will get update and notify to developer
    }

}

Result will be:


02-11 12:54:55.661 20994-20994/com.sunil.rxjavaexample I/System.out: Article changed to new article: Original Article

02-11 12:54:55.661 20994-20994/com.sunil.rxjavaexample I/System.out: Updated :New Article

This all about the Observable pattern.Now Rx is following this pattern and used this in android development. I heard that now many android developers are using this pattern and it gives the good result.

It avoids boilerplate code I mean it avoids the unnecessary duplicate code. Many of the library that implemented on this pattern. For example, Retrofit is using RX Pattern and Realm also using this concept and many more good available library that working with Rx Team. I will explain in detail related Rx(Reactive Programming) Java2 to use in Android application development. Here are the details of my next tutorial link Rxjava2 in android part2.

Thanks for reading this post.

Comments

Popular posts from this blog

NavigationView Drawer Expandable menu Item in ANdroid

Service LifeCycle

Custom SeekBar (Discrete SeekBar) in android