开发者

Visitor pattern lacking parameters

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&l开发者_StackOverflowt;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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜