title: Using RX to communicate between two components through a mediator
body: <p>C1 and C2 are components that don't know about each other. C1 does things that C2 cares about. I use a "Manager/Mediator" to allow communication between them. </p>
  
  <p>Both C1 and C2 reference this mediator. C1 calls method <code>mMediator.notifyItemProduced()</code> and <code>Mediator</code> forwards an 'onItemProduced' event to any listener it has. C2 implements this listener interface and does what it wants with it.</p>
  
  <pre><code>class Mediator {
     List&lt;Listener&gt; mListeners;
  
     public void notifyItemProduced() {
          for each listener in mListeners
              listener.onItemProduced();
     }
  
     public void addListener(Listener listener) { ... }
  
     public interface Listener { 
         void onItemProduced();
     }
  }
  
  class C1 {
      public void onClick() { mMediator.notifyItemProduced(); }
  }
  
  class C2 {
      public C2 { mMediator.addListener(this); }
  
      public void onItemProduced() { 
          // do something here!
      }
  }
  </code></pre>
  
  <p>I'm evaluating replacing this pattern with an RX implementation. I'm thinking that the <code>Mediator</code> should hold on to a <code>Subject</code> that C1 publishes to and C2 subscribes to.</p>
  
  <pre><code>class Mediator {
     Subject&lt;Boolean, Boolean&gt; mItems;
  
     public Subject&lt;Boolean, Boolean&gt; getItemsSubject() {
          return mItems;
     }
  
     public Observable&lt;Booelan&gt; getItemsStream() {
         return mItems.asObservable();
     }
  }
  
  class C1 {
      public void onClick() { mMediator.getItemsSubject().onNext(true); }
  }
  
  class C2 {
      public C2 { mMediator.getItems().subscribe(b =&gt; doSomething(b)); }
  }
  </code></pre>
  
  <p>I don't like that the <code>Mediator</code> exposes to everyone the ability to publish events. Though I know this ability existed in the listener-based implementation, I'd like to know if there's a pattern that would allow:</p>
  
  <ul>
  <li>C1 &amp; C2 not knowing about each other</li>
  <li>Only C1 can publish events</li>
  <li>Anyone can subscribe to these events</li>
  </ul>
  
  <p>Or is what I have a reasonable/best solution?</p>