org.extex.typesetter.type.math
Interface MathClassVisitor<RET_TYPE,A1_TYPE,A2_TYPE>

Type Parameters:
RET_TYPE - the return type
A1_TYPE - the type of argument 1
A2_TYPE - the type of argument 2

public interface MathClassVisitor<RET_TYPE,A1_TYPE,A2_TYPE>

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 sub-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 differentiate 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 differentiate 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(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(MathClass mc) {
         // Do something with mc depending on its type
     }
 
     public Object visitBinary(Object arg, Object arg2) {
         // do something for binary class
     }

     public Object visitClosing(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: 5563 $
Author:
Gerd Neugebauer

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

Method Detail

visitBinary

RET_TYPE visitBinary(A1_TYPE arg,
                     A2_TYPE arg2)
Invoke the visitor method for a binary operator.

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

visitClosing

RET_TYPE visitClosing(A1_TYPE arg,
                      A2_TYPE arg2)
Invoke the visitor method for a closing delimiter.

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

visitLarge

RET_TYPE visitLarge(A1_TYPE arg,
                    A2_TYPE arg2)
Invoke the visitor method for a large operator.

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

visitOpening

RET_TYPE visitOpening(A1_TYPE arg,
                      A2_TYPE arg2)
Invoke the visitor method for a opening delimiter.

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

visitOrdinary

RET_TYPE visitOrdinary(A1_TYPE arg,
                       A2_TYPE arg2)
Invoke the visitor method for an ordinary symbol .

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

visitPunctation

RET_TYPE visitPunctation(A1_TYPE arg,
                         A2_TYPE arg2)
Invoke the visitor method for a punctation symbol.

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

visitRelation

RET_TYPE visitRelation(A1_TYPE arg,
                       A2_TYPE arg2)
Invoke the visitor method for a relation operator.

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

visitVariable

RET_TYPE visitVariable(A1_TYPE arg,
                       A2_TYPE arg2)
Invoke the visitor method for a variable width symbol.

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