views:

632

answers:

2

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:

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

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);
}
+1  A: 

I don't even see where visitor is being used in your AcceptVisitor(IVisitor). That being said, if you want a different implementation for exception cases, you could pass two delegates into your fuction, one to test for the special case and one to describe what you want done differently for that special case. Alternatively make the special case implement another interface in addition to your normal ones and test for that (if your implementation allows, your description/code are very unclear).

Blindy
I have updated sample code.
rxm0203
A: 

there is no need for implementing this complex structure in .NET C#. we can implement visitor pattern as an extension method. As You many know, by using extension method, you can add new functionality to classed without accessing or changing the class.

Thanks BEHTASH

Behtash Moradi