title: How to implement a Factory with Dagger2
body: <p>I'm trying to implement a factory with Dagger2 but I don't know how?</p>
  
  <p>In a simplified example:</p>
  
  <pre><code>public class Foo{
  
      public interface Factory{
          Foo createNew();
      }
  
      private Bar bar;
  
      public Foo(Bar bar){
          this.bar= bar;
      }
  }
  </code></pre>
  
  <p>I'd like to set a method that provides Foo.Factory instances</p>
  
  <pre><code>@Module
  public class TestModule{
      @Provides
      Bar provideBar(){
          return new Bar();
      }
  
      @Provides
      Foo.Factory provideFooFactory(){
          // ??????
      }
  }
  </code></pre>
  
  <p>The result should be something like (getting the Bar instance from the module of course):</p>
  
  <pre><code>new Foo.Factory(){
        @Override
        public Foo createNew() {
           return new Foo(new Bar());
        }
  };
  </code></pre>
  
  <p>What is the correct approach to achieve this?</p>