I have parsed XML file into objects, in which each object has a 1:1 relationship with XML node in the file. This object tree implements Composite and Visitor design patterns as per Jeremy Miller's following blog entry:
When I iterate object tree, I do some processing in AcceptVisitor() method before double dispatching it. As a part of processing on certain objects, I call certain methods on leafs/children objects. Now, when I traverse child object (as a part of ongoing iteration), I don't want AcceptVisitor() to double dispatch anything. (Double dispatch calls method to write text to the output file. I want to avoid duplicate text in the file. )I achieved this functionality with a flag but I am not satisfied with the flag design. Is there any modified version of Visitor pattern to handle this particular situation? Any other ideas are also welcome.
//Iterate object tree
public void Iterate(Root root)
{
foreach (ILeaf child in root.ChildLeaves)
{
child.AcceptVisitor(this);
}
}
//This method in child object of type ILeaf gets called when Iterate() executes.
public void AcceptVisitor(IVisitor visitor)
{
//Some child object implement ILeaf1 along with ILeaf
//I want to avoid any processing in AcceptVisitor() for objects of type ILeaf1
//once following loop executes.
//This means Iterate() should not process anyting in AcceptVisitor() for
//children of type ILeaf1.
IEnumerable<ILeaf1> children = this.ChildLeaves.OfType<ILeaf1>();
//Collection of action statements from child objects.
List<ActionStatement> statements = new List<ActionStatement>();
foreach (ILeaf1 s in children)
{
ActionStatement statement = s.Generate();
statements.Add(statement);
}
//Output action statements to TextWriter
visitor.WriteStatements(statements);
}
//ILeaf1 object method
public void AcceptVisitor(IVisitor visitor)
{
ActionStatement statement = new ActionStatement("Some Text");
//Output action statement to TextWriter
visitor.WriteStatement(statement);
}