可以使用子编号列表指定树中的位置。您的计算结果如下(快速编写;未编译/测试):
public class Context {
private Context parent; // set in constructor
private int child; // with get/set
}
public ReturnType evaluate(Context context)
context.setChild(context.getChild() + 1);
context = new Context(context); // push a new context for calls
// do evaluation - when calling kids, pass context
}
上面应该以数字列表的形式跟踪上下文,告诉您当前正在处理哪个孩子。
与其在每种节点类型中都实现这一点,我建议您编写一个decorator(或使用模板方法的超类)来完成这项工作,例如:
// decorator
public class ContextTrackerEvaluator<T> implements Evaluator<T> {
private Evaluator realEvaluator;
public ContextTrackerEvaluator(Evaluator realEvaluator) {
this.realEvaluator = realEvaluator;
}
public T evaluate(Context context) {
context.setChild(context.getChild() + 1);
context = new Context(context); // push a new context for calls
realEvaluator.evaluate(context);
}
}
// OR superclass w/ template method
public class EvaluatorBase<T> {
public final T evaluate(Context context) {
context.setChild(context.getChild() + 1);
context = new Context(context); // push a new context for calls
doEvaluate(context);
}
// subclasses override doEvaluate to do their real work
protected abstract T doEvaluate(Context context);
}
这样您就可以访问上下文列表。
然后,您可以添加一个“stop context”参数进行比较(这样您就可以传入两个上下文,您可以比较它们是否匹配)。
希望这有帮助!
--斯科特