title: Wrapping a library using interfaces without needing downcasts
body: <p>Let's say my project uses a library, <code>LibFoo</code>, which provides its functionality through a number of classes, say, <code>FooA</code> and <code>FooB</code>. Now, there are a number of similar libraries (e.g., <code>LibBar</code> which provides <code>BarA</code> and <code>BarB</code>) that provide the same functionality as <code>LibFoo</code> and I want users of my project to be able to chose which library to use, preferably at runtime.</p>
  
  <p>For this to work, I've created a "wrapper layer" that defines the interfaces I expect from the libraries. In my example, this layer contains two interfaces: <code>IfaceA</code> and <code>IfaceB</code>. Then, for every library I want to support, I create an "implementation layer" that implements the interfaces using one of the libraries.</p>
  
  <p>My problem now lies in how to implement the implementation layer nicely. To demonstrate my problem, consider we have the following interfaces (shown in C++ but should be applicable to similar languages):</p>
  
  <pre><code>class IfaceA
  {
  public:
      virtual void doSomethingWith(IfaceB&amp; b) = 0;
      ...
  };
  
  class IfaceB
  {
      ...
  };
  </code></pre>
  
  <p>The classes in the implementation layer of <code>LibFoo</code> will hold objects from the corresponding classes of <code>LibFoo</code>. The operations in the interfaces should be implemented using these objects. Hence (excuse me for the horrible names):</p>
  
  <pre><code>class ConcreteAForFoo : public IfaceA
  {
  public:
      void doSomethingWith(IfaceB&amp; b) override
      {
          // This should be implemented using FooA::doSomethingWith(FooB&amp;)
      }
  
  private:
      FooA a;
  };
  
  class ConcreteBForFoo : public IfaceB
  {
  public:
      FooB&amp; getFooB() const
      {
          return b;
      }
  
  private:
      FooB b;
  };
  </code></pre>
  
  <p>The problem is in implementing <code>ConcreteAForFoo::doSomethingWith</code>: its parameter has type <code>IfaceB&amp;</code> but I need access to the implementation details of <code>ConcreteBForFoo</code> to be able to implement the method correctly. The only way I've found to do this is to use ugly downcasts all over the place:</p>
  
  <pre><code>void doSomethingWith(IfaceB&amp; b) override
  {
      assert(dynamic_cast&lt;ConcreteBForFoo*&gt;(&amp;b) != nullptr);
      a.doSomethingWith(static_cast&lt;ConcreteBForFoo&amp;&gt;(b).getFooB());
  }
  </code></pre>
  
  <p>Since having to downcast is generally considered to be a code smell, I can't help to think there should be a better way to do this. Or am I designing this wrongly to begin with?</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>Given a layer of interdependent interfaces (in that methods in one interface receive references to other interfaces). How can the implementations of these interfaces share implementation details without downcasting or exposing those details in the interfaces?</p>