de.dante.extex.interpreter.type.math
Interface MathClassVisitor


public interface MathClassVisitor

This interface implements part of the visitor pattern for the math class.

In object-oriented design a pattern has been established to let one class react on the type of some object. Consider a tree composed of different types of nodes all implementing a same base interface and some of them having extensions of the interface. This is the situation for Mathclasses for which several types exist.

The original idea was to attach the desired functionality to the sunb-types and let it have one common method to invoke it. This turns out not to be practical for modular components where the algorithm might be exchanged. Any new implementation would have the need to extend the MathClass classes.

The other simplistic solution is to use a large switch, or cascaded if-then-else, to differntiate the cases. This kind of code is cumbersome to maintain. Whenever a new type of node is added you have to remember all places which need adaption.

This problem is solved in the visitor pattern. Each sub-class of MathClass has to provide one method called visit() here. This method has as one argument the visitor which should be called back. The visitor is defined by the current interface. It has to provide a set of methods which allow him to differntiate the types of the math class.

Each math class has to implement the method visit() in a way that the appropriate method from the visitor interface is invoked. Thus as a result the math class and the algorithm are decoupled.

Example Source Code

Consider you have a class with a method which needs to react differently on different mode classes. The first approximation looks as follows:


 public class MyClass {

     public void myMethod(final MathClass mc) {
         // Do something with node depending on its type
     }
 }
 

Now we can add the MathClassVisitor interface. Thus we are forced to define a bunch of methods declared in this interface:


 public class MyClass implements NodeVisitor {

     public void myMethod(final MathClass mc) {
         // Do something with mc depending on its type
     }
 
     public Object visitBinary(final Object arg, Object arg2) {
         // do something for binary class
     }

     public Object visitClosing(final Object arg, Object arg2) {
         // do something for closing delimiters
     }

     // and many others..
 
 }
 

Now we just have to make sure that those methods are invoked. This is done with the method visit() of the MathClass. The signature allows us to provide one additional argument and receive a return value. Since we want to do something with a Noad, we use the argument for the noad.

The definition of the parameter and the return value are rather general. Thus it is possible to use the visitor pattern in several different situations.

The visitor is not necessarily the class Myclass itself. If this class contains several methods which need to distinguish the types of the math class it is possible to use another class as visitor, e.g. an inner class.

Version:
$Revision: 1.1 $
Author:
Gerd Neugebauer

Method Summary
 java.lang.Object visitBinary(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for a binary operator.
 java.lang.Object visitClosing(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for a closing delimiter.
 java.lang.Object visitLarge(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for a large operator.
 java.lang.Object visitOpening(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for a opening delimiter.
 java.lang.Object visitOrdinary(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for an ordinary symbol .
 java.lang.Object visitPunctation(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for a punctation symbol.
 java.lang.Object visitRelation(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for a relation operator.
 java.lang.Object visitVariable(java.lang.Object arg, java.lang.Object arg2)
          Invoke the visitor method for a variable width symbol.
 

Method Detail

visitBinary

public java.lang.Object visitBinary(java.lang.Object arg,
                                    java.lang.Object arg2)
Invoke the visitor method for a binary operator.

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result

visitClosing

public java.lang.Object visitClosing(java.lang.Object arg,
                                     java.lang.Object arg2)
Invoke the visitor method for a closing delimiter.

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result

visitLarge

public java.lang.Object visitLarge(java.lang.Object arg,
                                   java.lang.Object arg2)
Invoke the visitor method for a large operator.

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result

visitOpening

public java.lang.Object visitOpening(java.lang.Object arg,
                                     java.lang.Object arg2)
Invoke the visitor method for a opening delimiter.

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result

visitOrdinary

public java.lang.Object visitOrdinary(java.lang.Object arg,
                                      java.lang.Object arg2)
Invoke the visitor method for an ordinary symbol .

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result

visitPunctation

public java.lang.Object visitPunctation(java.lang.Object arg,
                                        java.lang.Object arg2)
Invoke the visitor method for a punctation symbol.

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result

visitRelation

public java.lang.Object visitRelation(java.lang.Object arg,
                                      java.lang.Object arg2)
Invoke the visitor method for a relation operator.

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result

visitVariable

public java.lang.Object visitVariable(java.lang.Object arg,
                                      java.lang.Object arg2)
Invoke the visitor method for a variable width symbol.

Parameters:
arg - the argument
arg2 - the second argument
Returns:
the result