Realize RXJAVA from 0

tags: Android learning

refer to:
First make a simplest RXJAVA:

/**
   * Observer
 */
public interface MyObserver<T> {

    void onSubscribe();
    void onNext(T t);
    void onError(Throwable e);
    void onComplete();
}
// Real observer
public interface MyObservableOnSubscrible<T> {

         Void Setobserver (Myobserver <T> OBServer); // Set downstream
}
/**
   * Is observed
 */
public class MyObservable<T> {

         // upstream object
    private MyObservableOnSubscrible<T> source;

         // Structure method for receiving upstream objects
    public MyObservable(MyObservableOnSubscrible<T> source) {
        this.source = source;
    }

         // Static method creates a real observer
    public static <T> MyObservable create(MyObservableOnSubscrible<T> source) {
        return new MyObservable(source);
    }

         / / Receive a downstream object here,
    public void setObserver(MyObserver<T> observer) {
        source.setObserver(observer);
         } // Set downstream
}

The three classes above are the most basic RXJAVA class, below is a simple use.

        MyObservable.create(new MyObservableOnSubscrible<Integer>() {
            @Override
            public void setObserver(MyObserver<Integer> observer) {
                Log.i (TAG, "Upstream Data: 10");
                observer.onNext(10);
            }
        }).setObserver(new MyObserver<Integer>() {
            @Override
            public void onSubscribe() {
                Log.i(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer o) {
                                 Log.i (tag, "downstream received data:" + O);
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError");
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete");
            }
        });

Output result:

COM.Example.test send data: 10
 COM.EXAMPLE.TEST downstream receives data: 10

Is it very similar to the chain of RxJava, but the variable name is a bit ugly, people are the subscribe method name, I am the SETOBSERVER method name, but it does not affect the use. We will change it in the later period, now in this way in order to better understand.
Analysis, finding the decorator mode here, Myobserver is an observer, MyobservableOnSubscribe is an actual observer, and MyObservable is a decorative class, which holds a MyobServableOnSubscribe object.

Custom MAP operator
It is obvious that the MAP operator is between the upstream and downstream, that is, their middle. In fact, this is also a thing that the decorator model is to do, that is, enhanced objects.

Intelligent Recommendation

Realize the target detection from 0 -based on YOLOV5

1. Background Last time we used YOLOV3 to do the task of target detection (Reference article: Starting from 0 to achieve target testing -Practice), We have some problems. One of them is to compare wit...

3 Realize SoftMax training from 0, and realize the Fashion-Mnist classification

I introduction Fashion-Mnist consists of 10 images of 10 categories, and each category is composed of 6,000 images and test data sets in Train Dataset. Therefore, the training sets and test sets inclu...

Realize two threads to print alternately from 0-100

Sharing a multi-threaded interview question is just for practicing. Here are two ways to print 0-100 alternately. If you are bored, you can look at it. 1. Synchronized realization: Start two threads, ...

Realize the distributed task scheduling system from 0--etcd basic operation

Download: etcd-v3.3.18-linux-amd64.tar.gz; unzip:tar -zxvf etcd-v3.3.18-linux-amd64.tar.gz The situation after decompression is as follows: Among them etcd is the server program, etcdctl is the client...

More Recommendation

Native JS game: Realize a minesweeper game from 0

I have been idle for the past two days and wrote a few Web games to pass the time. Among them, the author feels that the knowledge points involved in the realization of the minesweeper game are more c...

Realize React series from 0 (1): React architecture design

Editor's note: Su Chang, the author of this article, is a front-end development engineer at Qi Dance Group. Why write this series? At the beginning of 2020, set a goal for yourself. This year, you mus...

Realize React series from 0 (two): component update

Editor's note: The author of this article is Su Chang, the front-end development engineer of Qi Dance Group. Code refer to React 16.13.1 How is it different from other React tutorials? Assuming that R...

Realize distributed transactions from 0 to 1 - recognize distributed transactions

The main content of this lesson is to first introduce some theories related to distributed transactions ACID isolation level spring transaction propagation behavior optimistic lock pessimistic lock BA...

Android custom control from 0 to 1 easily realize the sliding button

Don't say anything, let's get an effect picture first Source code portal:https://github.com/anzaizai/EasySwipeMenuLayout I. Introduction The main knowledge points used this time are View measurement, ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top