Okay i've been strugling with this problem I'm having.
I have created a tree structure with logical nodes e.g. And, Or, Equals, Between. I don't want these nodes to have anything more than an accept method for the visitor, because there will be more visitors with different implementations.
So for example i'm visiting the and. It first visits itself so i know i'm dealing with an and, then it is visiting the left and the right nodes. Which can be any of earlier said nodes.
The problem is, that I need a way of checking that I am finished with visiting the childnodes. For example I want this output
"And(Equals()/Or(Equals()/Between())"
But since I have no way of knowing when childs have been visited it will be someting like this.
"And()Equals()/Or()Equals()/Between()
Any suggestions on how to overcome this problem?
Thanks for the tips jgauffin! But I got something smaller working the way I want it to.
Started working with generics.
public abstract class Node
{
public abstract T Accept<T>(IVisitor<T> visitor);
}
public interface IVisitor<T>
{
T Visit(And element);
T Visit(Or element);
T Visit(Equals element);
}
So my visitor can implement it without any concern of the objects like this:
public string Visit(Or element)
{
return "Or(" + element.Left.Accept(this) + "," + element.Right.Accept(this) + ")";
}
So my program just accepts the root node and will print out the string I mentioned before.