This article focuses on the main principles of using rxJava

rxJava hook

Hook: The AOP pattern, which I’ll cover later

RxJava is doing chain calls. If adding intermediate logic does not affect the main logic, adding auxiliary logic

All rxJava operators call one

Do the logic handling for onObservableAssembly, setOnObservableAssembly, to add the new logic code for Function.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //
        // I want to use how much to operate flatMap for Map
        // Hook before the listener
        RxJavaPlugins.setOnObservableAssembly(new Function<Observable, Observable>() {
            @Override
            public Observable apply(Observable observable) throws Exception {
                Log.d(Flag.TAG, "Apply: listen globally for how much of the project is using RxJava:" + observable);

                / / pseudo code
                /*if (observable === ObservableMap) return null; * /

                return observable; // Do not break the function of the home}}); testHook(); }/** * TODO RxJava Hook point */
    public static void testHook(a) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                e.onNext("A");
            }
        })
        .map(new Function<Object, Boolean>() {
            @Override
            public Boolean apply(Object o) throws Exception {
                return true;
            }
        })

        .subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {}}); }Copy the code

Subscriber model

Compared with traditional observer, the subscriber model has lower coupling and belongs to upstream and downstream of one line

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        New ObservableCreate() {source == custom source}
        // 2: Source code analysis of Observable creation
        Observable.create(

                // Customize source
                new ObservableOnSubscribe<String>() {
                    @Override
                    public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                        // Launcher. OnNext
                        emitter.onNext("A"); }})// 3: subscribe process source code analysis
        // ObservableCreate. subscribe
        .subscribe(
                // Customize the observer
                // 1: Observer source code
                new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {}@Override
                    public void onNext(String s) {}@Override
                    public void onError(Throwable e) {}@Override
                    public void onComplete(a) {}}); }Copy the code

Package mode

Take multiple map operators as an example

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("Derry"); e.onComplete(); }})// ↓ Observablecreate. map The innermost part of the wrap model
        .map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) throws Exception {
                return 45454; }})// ObservableMap.map
        .map(new Function<Integer, Boolean>() {
            @Override
            public Boolean apply(Integer integer) throws Exception {
                return true; }})// ↓ Strip the outer layer of the wrap model when going up ↑
        // ObservableMap.subscribe
        .subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {}@Override
            public void onNext(Boolean bool) {
                Log.d(Flag.TAG, "onNext bool:" + bool);
            }

            @Override
            public void onError(Throwable e) {}@Override
            public void onComplete(a) {}}); }Copy the code

The upper flows into the lower, the builder’s pattern layer by layer, the lower layer dissects the upper logic to influence the final result,

Parse the upper layer through subscribeActual, observer.onsubscribe (listener); Listen to upper-level logic and influence lower-level logic

Decorator pattern

Modify the original to make a new display, as opposed to pure builder mode, builder is composition