访客模式:遍历客户或访客中的树元素 [英] Visitor Pattern: Traversing tree elements in client or visitor

查看:123
本文介绍了访客模式:遍历客户或访客中的树元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,stackoverflow,

Good morning stackoverflow,

我目前正在实现类似AST这样的访问者模式. 现在我的问题是,如何遍历元素?

I'm currently implemeting a visitor pattern on something like an AST. Now my question is, how do I iterate through the elements ?

我认为将对象返回给访客并让访客从那里来回遍历更具逻辑性.因为要保持灵活性,所以当您想以不同的方式遍历对象时.

I think its somewhat more logical to just return the object to the visitor and let the visitor traverse from there on. Because you're keeping up flexibility , when you would like to traverse the object in different ways.

另一方面,访问者可以不必关心对象的结构.因此,如果对象发生更改,您也不必更改访问者.

On the other side one could say, the visitor shouldn't concern about the structure of the object. So in case the object changes, you don't have to change the visitor too.

有什么一般的建议如何解决这个问题?我有两本关于访客模式的书,但都没有解决如何处理更复杂的节点的问题.

Are there any general recommadations how to solve this? I've got two books about Visitor Patterns but both are not dealing with the question how to deal with more complex nodes.

Regads 脚趾

推荐答案

对于树结构而言,这似乎非常简单.节点中的accept方法如下所示:

It seems pretty straightforward for a tree structure. The accept method in a node could look like this:

void accept(Visitor visitor) {
    visitor.visitMyTypeOfNode(this);
    for each child {
        child.accept(visitor);
    }
}

显然,您需要考虑这对于您的应用程序的整体体系结构是否有意义.

Obviously you need to consider if this makes sense in the overall architecture of your application.

这篇关于访客模式:遍历客户或访客中的树元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆