title: Dynamically choose method at runtime; alternatives to Visitor Pattern or Reflection
body: <p>I'm working on a small game template, with a world comprised of nodes like so:</p>
  
  <pre><code>World
  |--Zone
  |----Cell
  |------Actor
  |------Actor
  |--------Item
  </code></pre>
  
  <p>Where a <code>World</code> can contain multiple <code>Zone</code> objects, a <code>Zone</code> can contain multiple <code>Cell</code> objects, and so on.</p>
  
  <p>Each of these implements the <code>Node</code> interface, which has a few methods like <code>getParent</code>, <code>getChildren</code>, <code>update</code>, <code>reset</code> and so on.</p>
  
  <p>I want to be able to execute a given <code>Task</code> on a single node or recursively down the tree from a node (as specified by the <code>Task</code>).  </p>
  
  <p>To compound this issue, I would like this to be a "pluggable" system, meaning I want players/developers to be able to add new types to the tree on the fly.  I had also considered casting from the base types:</p>
  
  <pre><code>public void doTask(Actor node)
  {
      if(!(node instanceof Goblin)) { return; }
      Goblin goblin = (Goblin) node;
  }
  </code></pre>
  
  <p>Initially I was drawn to use the <a href="https://en.wikipedia.org/wiki/Visitor_pattern" rel="nofollow noreferrer">Visitor Pattern</a> to take advantage of double dispatch, allowing each routine (Visitor) to act according to the type of <code>Node</code> being visited.  However, this caused a few complications, specifically when I want to add a new <code>Node</code> type to the tree.</p>
  
  <p>As an alternative, I wrote a <a href="https://github.com/floralvikings/jenjin/blob/node-overhaul/jenjin-world-core/src/main/java/com/jenjinstudios/world/reflection/DynamicMethodSelector.java" rel="nofollow noreferrer">utility class</a> that uses reflection to find the most specific method applicable to the <code>Node</code>.</p>
  
  <p><a href="https://stackoverflow.com/questions/435553/java-reflection-performance">My concern now is performance</a>; since there will be a fairly large number of reflective lookups and calls, I'm worried that the performance of my game (which could have hundreds or thousands of these calls per second) will suffer.</p>
  
  <p>Which seems to solve the problem of both patterns, but makes the code for each new <code>Task</code> uglier.</p>
  
  <p>The way I see it, I have three options for allowing this dynamic dispatch (unless I'm missing something obvious/obscure, which is why I'm here):</p>
  
  <ol>
  <li>Visitor Pattern
  
  <ul>
  <li>Pros
  
  <ul>
  <li>Double Dispatch</li>
  <li>Performance</li>
  <li>Clean Code in tasks</li>
  </ul></li>
  <li>Cons
  
  <ul>
  <li>Difficult to add new <code>Node</code> types (impossible without modifying original code)</li>
  <li>Ugly code during invocation of tasks </li>
  </ul></li>
  </ul></li>
  <li>Dynamic Invocation using Reflection
  
  <ul>
  <li>Pros
  
  <ul>
  <li>Can add new <code>Node</code> types with abandon</li>
  <li>Very customizable tasks</li>
  <li>Clean Code in tasks</li>
  </ul></li>
  <li>Cons
  
  <ul>
  <li>Poor performance</li>
  <li>Ugly code during invocation of tasks </li>
  </ul></li>
  </ul></li>
  <li>Casting 
  
  <ul>
  <li>Pros
  
  <ul>
  <li>More performant than reflection</li>
  <li>Potentially more dynamic than Visitor</li>
  <li>Clean code during invocation of tasks</li>
  </ul></li>
  <li>Cons
  
  <ul>
  <li>Code smell</li>
  <li>Less performant than Visitor (no double dispatch, casting in each invocation)</li>
  <li>Ugly code in tasks</li>
  </ul></li>
  </ul></li>
  </ol>
  
  <p>Have I missed something obvious here?  I'm familiar with many of the Gang of Four patterns, as well as the ones in <a href="http://gameprogrammingpatterns.com/" rel="nofollow noreferrer">Game Programming Patterns</a>.  Any help would be appreciated here.</p>
  
  <p><strong>To be clear, I'm not asking which of these is the "best"</strong>.  I'm looking for an alternative to these approaches.</p>