title: Ninject, the "Abstract Factory" pattern, and runtime conditional resolution
body: <h1>Introduction</h1>
  
  <p>I have been reading through the Ninject documentation, I reached the part where it talks about Factories (check <a href="http://www.ninject.org/wiki.html" rel="nofollow noreferrer">http://www.ninject.org/wiki.html</a> or <a href="http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-introduction/" rel="nofollow noreferrer">http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-introduction/</a>). There the <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern" rel="nofollow noreferrer">Abstract Factory</a> pattern is being referenced (Wikipedia).</p>
  
  <p>I keep finding a discrepancy between the way that the pattern is described in the Wikipedia article and the Ninject examples. I have also searched on SO and read a few answers relevant to this subject, and I still observe similarity in diverging from how it is described in Wikipedia.</p>
  
  <h1>Details</h1>
  
  <h2>In Wikipedia</h2>
  
  <p><img src="https://i.stack.imgur.com/8qW4p.png" alt="Class Diagram">
  You can notice:</p>
  
  <ul>
  <li>there are <strong>multiple</strong> implementations <em>*Concrete Factory*</em> for the <em>abstract factory</em>.</li>
  <li>there are <strong>multiple</strong> implementations <em>*Concrete Product*</em> of the <em>abstract product</em>.</li>
  <li>each <em>concrete factory</em> generates a <em>concrete product</em>. In the diagram <em>WinFactory</em> generates <em>WinButton</em> and <em>OSXFactory</em> generates <em>OSXButton</em></li>
  <li>if I am to write a program that determines types <em>conditionally</em> at runtime, it is obvious that I would have multiple implementations of a common abstraction (in the diagram multiple implementations of the Button interface)</li>
  <li>if I am to use the <em>Abstract Factory</em> pattern to achieve that, then according to the Wikipedia article I am deducing that at least one way _ <em>the article does not show another way to do it</em> _ would be to conditionally resolve to one of the <strong>multiple</strong> implementations for the factory which in turn would give me one of the <strong>multiple</strong> implementations of the <em>Product</em></li>
  </ul>
  
  <h2>In the Ninject documentation</h2>
  
  <p>We have:</p>
  
  <pre><code>public class Foo
  {
      readonly IBarFactory barFactory;
  
      public Foo(IBarFactory barFactory)
      {
          this.barFactory = barFactory;
      }
  
      public void Do()
      {
          var bar = this.barFactory.CreateBar();
          ...
      }
  }
  
  public interface IBarFactory
  {
      Bar CreateBar();
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>kernel.Bind&lt;IBarFactory&gt;().ToFactory();
  </code></pre>
  
  <ul>
  <li>I don't see multiple implementations of neither the factory nor the product</li>
  <li>Other than allowing for code like this <code>var bar = this.barFactory.CreateBar();</code> rather than have the dependency injected through the constructor I do not see the point. There might be a use for being able to use code like this (examples?) but is that all there is to it?</li>
  </ul>
  
  <h2>On SO</h2>
  
  <ul>
  <li>I saw <a href="https://stackoverflow.com/a/14024143">this</a>. Check the last comment which seems to indicate having multiple methods within the factory returning different implementations, but still we are using only <strong>one</strong> <em>concrete factory</em> and thus not following the Wikipedia article</li>
  <li><a href="https://stackoverflow.com/a/1945023">This</a> seems to resemble Ninject's examples </li>
  <li>In <a href="https://stackoverflow.com/a/1686887">this</a> a dependency is used, but still the type is not abstract </li>
  </ul>
  
  <h1>The question</h1>
  
  <p>Are the examples other than the Wikipedia one really following the <em>Abstract Factory</em> pattern?</p>