title: Inheritance vs Composition for canvas shapes model?
body: <p>For the following model, would you prefer inheritance or composition:</p>
  
  <ul>
  <li>I want to draw objects on a <code>canvas</code> that each represent a data object</li>
  <li>think of it like a state machine diagram: <code>Ellipses</code> represent <code>States</code>, <code>Lines</code> represent <code>connections/transitions</code> between them. The object representation itself will never change, ie a <code>State</code> will always be shown by a <code>ellipse</code>. But the way an <code>ellipse</code> is drawn should differ, eg for selection it should have a different color, while dragging it should maybe have an alpha channel, etc.</li>
  </ul>
  
  <p>From the design point of view, an <code>ellipse</code> is not a <code>state</code>, and a <code>line</code> is not a <code>transition</code>. Anyhow it would be suitable to combine both objects for being able to collect them in a <code>List&lt;Shape&gt;</code> and execute <code>shape.draw()</code> on every object.</p>
  
  <p>Now 2 design models are possible, whereas I would consider 2 classes to be always the same:</p>
  
  <pre><code>interface Shape {
      void draw();
  }
  abstract class Figure implements Shape {
      //basic vars like start and end coordinates
      int x0, y0, x1, y1;
  }
  </code></pre>
  
  <p>Inheritance:</p>
  
  <pre><code>abstract class State extends Figure {
      String name;
  }
  class Rectangle extends State {
      @Override void draw();
  }
  
  class Line extends Figure;
  class Transition extends Line
  </code></pre>
  
  <p>Although from design point of view a <code>rectangle</code> is not a <code>State</code>, and a <code>state</code> is not a <code>figure</code>, regarding the drawing context this might be viable. Because I could inherit most of the stuff needed for handling the <code>shapes</code>, drawing etc.</p>
  
  <p>Or Composition:</p>
  
  <pre><code>abstract class State {
      String name;
  }
  
  class Rectangle extends Figure {
      private State state;
      @Override void draw();
  }
  class Line extends Figure {
      private Transition transition;
      @Override void draw();
  }
  </code></pre>
  
  <p>So <code>Rectangle</code> + <code>Line</code> would be wrappers for my objects.</p>
  
  <p>Should a Rectangle and Line rather extends State and Transition, or contain it?</p>
  
  <p>Or, maybe there is also a 3rd design option that I did not see. Looking forward to your ideas.</p>