title: How to define common operations between different objects in java?
body: <p>I have a number of operands of different types and want to define how
  these operands can operate on each other. This seems like a generic
  problem but I fail to find a good approach for implementing it.</p>
  
  <p>Say for a start that I have scalars (i.e. numbers), matrix, and sets
  of matrix or scalars and that I want to define the plus operation on these:</p>
  
  <ul>
  <li><p>scalar1.plus(scalar2) returns a scalar equal to scalar1+scalar2,</p></li>
  <li><p>matrix.plus(scalar1) or scalar.plus(matrix) both return a matrix
  where 'scalar1' each element has been added to each element of the
  matrix,</p></li>
  <li><p>matrixSet.plus(matrix1) returns a set where each matrix has been
  added to matrix1</p></li>
  </ul>
  
  <p>And so on... Note that some operations may throw exceptions, such as
  adding matrix of different sizes or adding sets that do not have the
  same number of elements.</p>
  
  <p>My goals are the following:</p>
  
  <p>1- reuse as much code as possible (important because I will have many
       more operations and data types),</p>
  
  <p>2- do as much validation at compile time as possible</p>
  
  <p>3- make it easy to add new data types or operations in the future (for
       example, I may want to add a vector data type later on)</p>
  
  <p>I initially thought I would define an Operand interface, which my
  scalar, matrix, and set of these elements would implement, and which
  would include methods like:</p>
  
  <pre><code>public Operand plus(Operand op);
  </code></pre>
  
  <p>This would allow be to define the plus operation from the bottom up,
  starting with the simplest element (scalar):</p>
  
  <pre><code>public Operand plus(Operand operand) {
    if (Scalar.class.equals(operand.getClass())) {
      return new Scalar(this.value + ((Scalar) operand).getValue());
    } else {
     // assume the other operand will define the addition for me
     // since add is commutative
      return operand.plus(this);
    }
  }
  </code></pre>
  
  <p>And then for matrix:</p>
  
  <pre><code>public Operand plus(Operand operand) {
    if (Scalar.class.equals(operand.getClass())) {
      // return a matrix where we add scalar value to all matrix elements
    } else if (Matrix.class.equals(operand.getClass())) {
      // return a matrix where we perform a scalar addition between each element
    } else {
      // assume the other operand will define the addition for me
      // since add is commutative
      return operand.plus(this);
    }
  }
  </code></pre>
  
  <p>The idea being that whenever I introduce a new class implementing the
  Operand type (say a vector), I will only have to define the plus
  operation betwen that new operand and all the other existing
  operands. I.e. I wouldn't have to rewrite the plus method of the
  Scalar and Matrix classes.</p>
  
  <p>This approach meets goals 1 and 3 but not 2: all operations return
  Operand objects, and forces me to do a lot of casting and class
  checking at run time, which doesn't seem like a good practice.</p>
  
  <p>Any better way of approach my problem?</p>