How to create a NullObject template using RxJava

Yes, this may seem a little ambitious, but here is what I want to achieve:

I have a class that contains my hot Observable, it takes some time to prepare it to prepare it correctly (create it elsewhere and get the link), so when another class requests it, the link may remain empty (calls NullPointer). What I'm trying to achieve is similar to the NullObject pattern: it returns an empty observable (instead of zero) that can be safely signed, and when the Observable is created correctly, start issuing the elements.

The only way to solve this is to create PublishSubjectone that acts as a bridge between the client classes and the ObservableHolder class. The PublishSubject link is always returned to the client, and when Observableready, it simply redirects all events to the object. This is a good decision? Or can it be done better?

@edit

I decided to stay with my decision, I think that after packing it in class it is a good enough solution. I supported the answer below, however, it does not correspond to my problem, because it is not directly related to the implementation rx-java, however, the suggestion about the proxy server template was useful.

+4
source share
1 answer

, , , -.

- ( ) , , .

, java.util.Observable

public class ObservableProxy extends Observable {


    private ObservableProxyAdapter observableProxyAdapter = new ObservableProxyAdapter();

    private Observable target;

    public void setTarget(Observable target) {
        if(this.target != null){
            // if this proxy was connected to an observer... disconnect
            this.target.deleteObserver(observableProxyAdapter);
        }

        this.target = target;

        if(this.target != null){
            // If a new target is set... connect to it
            this.target.addObserver(observableProxyAdapter);
        }
    }


    private class ObservableProxyAdapter implements Observer {

        public void update(Observable o, Object arg) {
            // forward notifications from the target
            setChanged();
            notifyObservers(arg);

        }

    }

}

ObservableProxy . ObservableProxy. , "" Observable, ObservableProxy. Observer .

+-----------------+  notify  +---------------+   notify   +--------------+
| real observable |  ------> | proxy adapter |   ------>  | observables  |
+-----------------+          +---------------+            |  of proxy    |
                                                          +--------------+

public class ObservableProxyTest {

    public static void main(String[] args) {
        ObservableProxy observableProxy = new ObservableProxy();

        Observer someObserver = new Observer() {

            public void update(Observable o, Object arg) {
                System.out.println(arg);
            }
        };
        observableProxy.addObserver(someObserver);


        // Later the real observer becomes available
        RealObservable realProxy = new RealObservable();
        observableProxy.setTarget(realProxy);


        // notifications will be forwarded
        realProxy.notifyHello();
    }

    private static class RealObservable extends Observable {

        public void notifyHello(){
            setChanged();
            super.notifyObservers("Hello World");
        }
    }
}
+1

Source: https://habr.com/ru/post/1615226/


All Articles