title: How to write solid Pure Aggregation (composition) Game Objects in Java?
body: <p>So I am just at the beginning of writing a game in Java and I am writing my game objects. Now I have read here in <a href="http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/" rel="noreferrer">Evolve Your Hierarchy</a> that you should build your games as compositions and not as a big class hierarchy. As this image from the previous link shows:</p>
  
  <p><img src="https://i.stack.imgur.com/YJ0t8.gif" alt="enter image description here"></p>
  
  <p>However, when actually getting down to the implementation I have one small question about where to apply the interfaces.</p>
  
  <p>Lets say you have a class called Player and the interfaces Moveable and Renderable. Do you implement this using public interface variables:</p>
  
  <pre><code>class Player {
      public Moveable moveable;
      public Renderable renderable;
  }
  
  class GenericMoveable implements Moveable {
      // Function implementations
  }
  
  class PlayerRenderable implements Renderable {
      // Function implementations
  }   
  </code></pre>
  
  <p>Or do you try and do this by applying the interfaces directly to the object:</p>
  
  <pre><code>class Player implements Moveable, Renderable {
      private GenericMoveable genericMoveable;
  
      // Non-direct Implementation of Moveable
      void someMoveFunc(double x, double y) {
          genericMoveable.someMoveFunc(x, y);
      }
  
      // Direct implementation of Renderable
      void someRenderableFunction() {
          // Player class specific code
      }
  }
  
  class GenericMoveable implements Moveable {
      // Function implementations
  }
  </code></pre>
  
  <p>Now currently I am feeling that the second method is better. The main reason for that is because I can create the following lists:</p>
  
  <pre><code>List&lt;Renderable&gt; renderObjects; // use this to draw all of the objects to the screen
  List&lt;Moveable&gt; moveObjects; // use this to move all objects at once
  </code></pre>
  
  <p>I really just want confirmation that I am on the right track. I am pretty sure that when creating game objects by composition for games in Java you want to apply the interfaces directly to the Game Object in question and then use private interfaces if you need them and direct implementations of those functions for the rest. Is this correct? And what are the positives and negatives of what I am doing?</p>
  
  <p>Though, while we are here, what else do I need to look out for in the implementation of Compositional Game Objects in Java? What should I do differently? Thanks for any responses in advance, I appreciate any and all help.</p>