缺少参数的访问者模式
我确信这一定是访问者模式的常见问题,所以我想看看是否有标准的解决方案。
如何重新编码树遍历,其中方法内置于树类本身中,例如
class Node {
void Traverse(SomeType& t) { ... }
};
使用访问者的代码中。我想到的两个解决方案是
class Visitor {
stack<SomeType> argumentStack;
void Visit() {
// do work, add new arguments onto stack
// call accept() on child objects
// pop stack
}
};
将参数添加到accept(Visitor&)和visit()方法本身。但这并不比类中内置的原始遍历更好。
另一个问题是,如果每个内置遍历方法采用不同的参数,或者某些返回值而其他方法不返回值,或者它们并不都返回相同的类型,会发生什么情况?
I'm sure this must be a common problem with the Visitor pattern, so thought I'd see if there is a standard solution.
How can you re-code a tree traversal where the methods are built into the tree classes themselves, say
class Node {
void Traverse(SomeType& t) { ... }
};
into code that uses a visitor. Two solutions that come to mind are either
class Visitor {
stack<SomeType> argumentStack;
void Visit() {
// do work, add new arguments onto stack
// call accept() on child objects
// pop stack
}
};
or adding the arguments to the accept(Visitor&) and visit() methods themselves. But then this is no better than the original traversal built into the classes.
As a further problem, what happens if each of the built in traversal methods take different arguments, or some return values and others don't, or they don't all return the same types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论