Let's say we have the following data hierarchy in C# to evaluate mathematic (prefix) expressions in int
arithmetics:
abstract class Expression {
public abstract int Evaluate();
}
class ValueExpression : Expression {
public int Value {
get;
}
public sealed override int Evaluate() {
return Value;
}
}
abstract class OperatorExpression : Expression {
// ...
}
abstract class BinaryExpression : OperatorExpression {
protected Expression op0, op1;
public Expression Op0 {
get { return op0; }
set { op0 = value; }
}
public Expression Op1 {
get { return op1; }
set { op1 = value; }
}
public sealed override int Evaluate() {
return Evaluate(op0.Evaluate(), op1.Evaluate());
}
protected abstract int Evaluate(int op0Value, int op1Value);
}
sealed class PlusExpression : BinaryExpression {
protected override int Evaluate(int op0Value, int op1Value) {
return checked(op0Value + op1Value);
}
} // and more operators...
How could I use the visitor pattern to evaluate an expression and write out a result as int
or double
based on the user input? I thought I might write a Visitor class that holds double result;
and each Visit(...)
would evaluate the (sub)expression using double
arithmetics and then I would convert the result to int
if needed. But is this the best solution to this using visitor pattern? What if I wanted to use long
later on? It would be necessary to modify the classes by non-trivial amount of code, or? (Note: I would like to use visitor pattern (and not the dynamic one) and not any generic code.)
Aucun commentaire:
Enregistrer un commentaire