title: Using Enum for factory in Java, a best practice?
body: <p>Java allow us to embed data and behaviour on Enum.
  I don't want to implement a factory directly on an Enum, because I think this is not its role. </p>
  
  <p>But I can put class reference on the enum, and contruct object on an external factory. Comparing to a traditionnal factory pattern, what is the best implementation for you ? Which solution is better to use in which case ?</p>
  
  <p>Now, the code.</p>
  
  <p>Function used in both solutions to construct objects.  Usefull to implement fly-weight pattern with a Map if required.</p>
  
  <pre><code>private Action getAction(Class&lt;? extends Action&gt; actionClazz) {
      // logger + error handling
      return actionClazz.newInstance();
  }
  </code></pre>
  
  <p>1) With a traditionnal factory:</p>
  
  <pre><code>public enum ActionEnum {
      LOAD_DATA,
      LOAD_CONFIG;
  }
  
  public Action getAction(ActionEnum action) {
      switch (action) {
      case LOAD_CONFIG:
          return getAction(ActionLoadConfig.class);
      case LOAD_DATA:
          return getAction(ActionLoadData.class);
      }
  }
  </code></pre>
  
  <p>2) With Enum-styled factory :</p>
  
  <pre><code>public enum ActionEnum {
      LOAD_DATA(ActionLoadConfig.class),
      LOAD_CONFIG(ActionLoadData.class);
  
      public ActionEnum(Class&lt;? extends Action&gt; clazz){...}
      public Class&lt;? extends Action&gt; getClazz() {return this.clazz}
  }
  
  public Action getAction(ActionEnum action) {
      return getAction(action.getClazz());
  }
  </code></pre>