title: Java Enums - Switch statements vs Visitor Pattern on Enums - Performance benefits?
body: <p>I have been searching around for days to find an answer to this performance based issue.<br>
  After digging  the Internet so far I have learned that there are couple of ways to use the Enums in java, well documented in <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html" rel="nofollow noreferrer">here</a>.
  Well, definitely as a starter one would like use Enums in a <em>switch-case</em> statement, which provides clarity and better understanding of the code. But on the other hand we have a <em>Visitor pattern</em> style implementation of the Enums as well, which ensures type safety and extensibility, Discussed <a href="https://stackoverflow.com/questions/859563/java-enums-and-switch-statements-the-default-case">here</a>.</p>
  
  <p>Having said that, and coming back to original idea behind this question, so far I have learned that if a switch-case construct is properly designed using Enums, which ensures that case values are not sparse, and that the Enum declaration is in the same compilation unit as the switch-case statement, the java compiler performs some optimisation on the produced byte-code by implementing such construct as a <strong>Jump Table</strong> (discussed in <a href="http://forums.java.net/jive/thread.jspa?threadID=504" rel="nofollow noreferrer">here</a> and elsewhere as well on Sun's website, to which I lost the link). Now, this definitely gives a boost to the performance compared to multiple/nested if-else construct.  </p>
  
  <p>My question is, how does java implement the visitor pattern based implementation of Enums in the resulting byte-code and what is the performance boost while compared to <em>switch-case</em> based implementation, if any?</p>
  
  <p>And which type of implementation should I prefer, considering my Enums may grow in future and I am keen on performance as well. Currently, I have some 19 and odd constants in my Enum.</p>
  
  <hr>
  
  <p><strong>EDIT</strong><br>
  I have a class that stores some information about variables of a game. One of the variables being an <em>Enum</em> type.  </p>
  
  <pre><code>public class GameObject {
      private Shape mShape;
  
      public Shape getShape() {
          return mShape;
      }
      .
      .
      .
  
      public static enum Shape {
          SHAPE1, SHAPE2, SHAPE3, SHAPE4, ..., SHAPE20
      };
  
      public void drawShape() {
          switch (this.mShape) {
          case SHAPE1:
              drawShape1();
              break;
          case SHAPE2:
              drawShape2();
              break;
          case SHAPE3:
              drawShape3();
              break;
          case SHAPE4:
              drawShape4();
              break;
          .
          .
          .
          .
          .
          case SHAPE20:
              drawShape20();
              break;
          default:
              drawUnknown();
              break;
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Later I realised of separating the information from logic, and hence created another class and moved the <strong><em>Enum Shape</em></strong> from <strong><em>GameObject</em></strong> to this new class <strong><em>GraphicModel</em></strong>, and instead of having <em>switch-case</em> there, I implemented <em>constant-specific methods</em>. And yes I did put proper import statements in either class after this modification.  </p>
  
  <pre><code>public class GraphicModel {
      public void drawGraphicFromObject(GameObject gameObject) {
          gameObject.getShape().draw();
      }
  
      public static enum Shape {
          // This method is not public, instead is only called by GraphicModel
          abstract void draw();
  
          SHAPE1 {
              @Override
              void draw() {
                  // some transformations
              }
          },
          SHAPE2 {
              @Override
              void draw() {
                  // Some different transformation
              }
          },
          SHAPE3 {
              @Override
              void draw() {
                  // Yet another transform
              }
          },
          .
          .
          .
          .
          UNKNOWN {
              @Override
              void draw() {
                  // 
              }
          };
      }
  }
  </code></pre>
  
  <p>Later on I even implemented this based on <em>visitor pattern</em> as suggested <a href="https://stackoverflow.com/questions/859563/java-enums-and-switch-statements-the-default-case">here</a>  </p>
  
  <p>So, what I need to know is that which way of implementation is more efficient? Definitely, for <em>switch-case</em> to be converted to <strong>jump tables</strong> on compilation, java requires both <em>enum</em> declaration and <em>switch</em> statements in the same <em>compilation unit</em>.
  Should I use <em>switch</em> based implementation or <em>constant-specific method</em> implementation in my <strong>GraphicModel</strong> class?
  Rather, to be clear, what's the difference in performance?  </p>