title: .NET C# Visitor Pattern
body: <p>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:</p>
  
  <p><a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/10/31/be-not-afraid-of-the-visitor-the-big-bad-composite-or-their-little-friend-double-dispatch.aspx" rel="nofollow noreferrer">http://codebetter.com/blogs/jeremy.miller/archive/2007/10/31/be-not-afraid-of-the-visitor-the-big-bad-composite-or-their-little-friend-double-dispatch.aspx</a></p>
  
  <p>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. </p>
  
  <pre><code>//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&lt;ILeaf1&gt; children = this.ChildLeaves.OfType&lt;ILeaf1&gt;();           
         //Collection of action statements from child objects.           
         List&lt;ActionStatement&gt; statements = new  List&lt;ActionStatement&gt;();           
         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);
  }
  </code></pre>