title: RXJava 2 /RxAndroid 2 vs Observable vs Listener to Observe from different classes
body: <p>since i would like to start my new app with a clean base im looking for a good way sharing Informations around different classes. For an example i would like to subscribe to an interface which may be used/shared by different classes. </p>
  
  <p>Example for the interface/Observable way:</p>
  
  <pre><code>class SingleTonHolder {
      private static _instance = null;  // and initalize it using a static method.
      private List&lt;MyListener&gt; myListeners new ArrayList&lt;&gt;();
      public SingleTonHolder getInstance(){ return _instance }
  
      public void registerListener(MyListener listener) { myListeners.add(listener); }
      public void removeListener(MyListener listener) { myListeners.remove(listener); }
  
      public void doSomethingToTheListener(...) { for(MyListener l : myListeners) l.doSomethingToTheListener(..); }
  }
  
  ClassOne extends MyListener {
    public void onCreate() { 
        SingleTonHolder.getInstance().registerListener(this); 
    }
    public vond onDestroy() { 
        SingleTonHolder.getInstance().removeListener(this); 
    }
  }
  </code></pre>
  
  <p>and another class to listen for changes.</p>
  
  <pre><code>ClassTwo {
    MyListener listener = null;
    public void onCreate() { 
        listener = new MyListener( () =&gt; { .... });
        SingleTonHolder.getInstance().registerListener(listener); 
    }
    public vond onDestroy() { 
        SingleTonHolder.getInstance().removeListener(listener); 
    }
  }
  </code></pre>
  
  <p>This does work and looks like the default solution. Everytime another Object calls SingleTonHolder.doSomethingToTheListener() it notify all registered listeners for changes.</p>
  
  <p>Since i would like to try the same solution using RxJava2 and RxAndroid2 which lacks documentation i have tried the following way doing it. </p>
  
  <pre><code>Class CallFactory{
      public Obvservable&lt;List&lt;Data&gt;&gt; getDummyData() { return anDummyObservable; }
  }
  </code></pre>
  
  <p>Then ive created a Singleton Class which has a function to modify/observ onec a client subscribes.</p>
  
  <pre><code>public Observable&lt;List&lt;Data&gt;&gt; getData() {
      return CallFactory.with(context)
             .getDummyData(...)
             .map(modifyList -&gt; { /** do some processing **/return modifyList;          
      }) 
  }
  </code></pre>
  
  <p>which doesnt work as excepted since every time a client subscribes it "recall" it and the client keeps connected until onCompleted() is called.</p>
  
  <p>My first try to share the informations to all subscribed clients ive created a PublishSubject in my Singleton Class.</p>
  
  <pre><code>private PublishSubject&lt;List&lt;Data&gt;&gt; sharedSubject = PublishSubject.create(); 
  </code></pre>
  
  <p>Now i let my clients subscribe to the subjects using a method like</p>
  
  <pre><code>public PublishSubject getSharedSubject() { return this.sharedSubject; }
  </code></pre>
  
  <p>If i would like to send a Message which should be received by all listening clients then i have created something like</p>
  
  <pre><code>public void setNewData(List&lt;Data&gt; data) {
      sharedSubject.onNext(data);
  }
  </code></pre>
  
  <p>I am pretty sure that is not the way it should be, but is rxjava designed to serve such a solution? If i want to share events different then onNext, onError, onComplete, do i need to wrap an Interface in the onNext? </p>
  
  <p>The codes are not tested and just to show how i understood it. Any help would be appreciate.</p>