我已经将XML文件解析为对象,其中每个对象与文件中的XML节点具有1:1的关系。这个对象树实现复合和Visitor设计模式为每杰里米米勒的以下博客条目:.NET C#访问者模式
当我遍历对象树,我双调度它之前做一些处理AcceptVisitor()方法。作为处理某些对象的一部分,我在叶子/子对象上调用某些方法。现在,当我遍历子对象(作为正在进行的迭代的一部分)时,我不希望AcceptVisitor()加倍派遣任何东西。 (双派遣调用方法写文本到输出文件,我想避免文件中的重复文本。)我用一个标志实现了这个功能,但我对标志设计不满意。是否有修改版的Visitor模式来处理这种特殊情况?任何其他想法也是受欢迎的。
//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);
}
为什么创建AcceptVisitor2()当然;) – zvolkov 2009-07-09 21:14:01
添加一些代码来说明第二段,它的含义不清楚(至少对我来说)你的Accept()在做什么以及你希望它的行为如何。 – 2009-07-09 21:22:56