访客模式中的双重调度如何工作?

发布于 2024-11-25 00:16:11 字数 160 浏览 4 评论 0原文

我正在研究与访问者模式相关的其他问题,但无法理解访问者模式中双重调度的实现。

请参考链接 访问者模式

访问者模式中的双重调度如何工作?

I was looking into other questions related to the visitor pattern but couldn't understand the implementation of double dispatch in visitor pattern.

Please refer to the link
Visitor Pattern

How does double dispatch work in the Visitor pattern?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘虞初梦 2024-12-02 00:16:11

单次调度

单次调度

假设 Node 是一个接口类,两个子类是具体实现界面的。

如果您在节点实例上调用 GenerateCode() 方法,则实际执行的操作取决于节点的类型。它可以是 VariableRefNodeAssignmentNode 中的方法。如果调用 PrettyPrint() 也是一样的。因此,实际执行的操作取决于您正在调用的方法名称和节点的类型

双重调度

Nodes
Visitors

这次 Node 允许您传递 NodeVisitor< 类型的参数/code> 到其名为 Accept 的方法。在您的程序中,如果您在节点实例上调用 Accept,则现在执行的实际操作取决于节点的类型VariableRefNode 或 < code>AssignmentNode) AND 您传递给 Accept 的访问者实例的类型(TypeCheckingVisitorCodeGenerateVisitor >)。

Single-dispatch

Single dispatch

Assume Node is an interface class and the two sub classes are concrete implementations of the interface.

If you call GenerateCode() method on a node instance, the actual operation getting executed depends on the type of the node. It could be the method either in VariableRefNode or AssignmentNode. It's the same if you call PrettyPrint(). So the actual operation getting executed depends on name of the method you are calling and the type of the node.

Double-dispatch

Nodes
Visitors

This time the Node is allowing you to pass a parameter of type NodeVisitor to its method called Accept. In your program if you call Accept on a node instance, the actual operation getting executed now depends on the type of the node (VariableRefNode or AssignmentNode) AND the type of the visitor instance you passed into Accept (TypeCheckingVisitor or CodeGeneratingVisitor).

瞎闹 2024-12-02 00:16:11

元素对象的 accept 方法接收访问者对象,并调用该访问者对象的 visit 方法。由于访问者对象有多个 visit 方法,因此根据元素类型调用适当的 visit 方法。这里我们有两个调用(双调度),它们指定元素和元素的正确操作(基于其类型)。

The element object's accept method receives a visitor object and it calls the visit method on the visitor object. As the visitor object has several visit methods, based on the element type the appropriate visit method is called. Here we have two calls (double dispatch) that specify the element and the right operation for the element (based on its type).

苹果你个爱泡泡 2024-12-02 00:16:11

好吧,这是该文章的相关引用:

访客实现“双重调度”。 OO 消息通常表现为“单一调度”——所执行的操作取决于:请求的名称和接收者的类型。在“双重调度”中,执行的操作取决于:请求的名称和两个接收者的类型(访问者的类型及其访问的元素的类型)。

这本质上意味着不同的访问者可以访问相同的类型,并且同一访问者可以访问不同的类型。使用访问者模式执行的命名操作的效果可能取决于访问者被访问者(双重调度)。

Well, here's the relevant quote from that article:

Visitor implements “double dispatch”. OO messages routinely manifest “single dispatch” - the operation that is executed depends on: the name of the request, and the type of the receiver. In “double dispatch”, the operation executed depends on: the name of the request, and the type of TWO receivers (the type of the Visitor and the type of the element it visits).

This essentially means different visitors can visit the same type and different types can be visited by the same visitor. The effect of a named operation that is performed using the visitor pattern may depend on the visitor and the visited (double dispatch).

耀眼的星火 2024-12-02 00:16:11

显示双重调度的示例代码:

import java.util.Arrays;
import java.util.List;

class Client {
    public static void main(String[] args) {
        List<Node> nodes = Arrays.asList(new NodeA(), new NodeB());
        List<NodeVisitor> visitors = Arrays.asList(new NodeVisitor1(), new NodeVisitor2());

        for (Node node : nodes) {
            for (NodeVisitor visitor : visitors) {
                node.accept(visitor);
            }
        }
    }
}

interface Node {
    void accept(NodeVisitor visitor);
}

interface NodeVisitor {
    void visit(Node node);
}

class NodeA implements Node {

    @Override
    public void accept(NodeVisitor visitor) {
        visitor.visit(this);
    }

    @Override
    public String toString() {
        return "Node A";
    }
}

class NodeB implements Node {

    @Override
    public void accept(NodeVisitor visitor) {
        visitor.visit(this);
    }

    @Override
    public String toString() {
        return "Node B";
    }
}

class NodeVisitor1 implements NodeVisitor {

    @Override
    public void visit(Node node) {
        System.out.println("Node visitor 1, node " + node);
    }
}

class NodeVisitor2 implements NodeVisitor {

    @Override
    public void visit(Node node) {
        System.out.println("Node visitor 2, node " + node);
    }
}

输出为:

Node visitor 1, node Node A
Node visitor 2, node Node A
Node visitor 1, node Node B
Node visitor 2, node Node B

An example code, that shows double dispatch:

import java.util.Arrays;
import java.util.List;

class Client {
    public static void main(String[] args) {
        List<Node> nodes = Arrays.asList(new NodeA(), new NodeB());
        List<NodeVisitor> visitors = Arrays.asList(new NodeVisitor1(), new NodeVisitor2());

        for (Node node : nodes) {
            for (NodeVisitor visitor : visitors) {
                node.accept(visitor);
            }
        }
    }
}

interface Node {
    void accept(NodeVisitor visitor);
}

interface NodeVisitor {
    void visit(Node node);
}

class NodeA implements Node {

    @Override
    public void accept(NodeVisitor visitor) {
        visitor.visit(this);
    }

    @Override
    public String toString() {
        return "Node A";
    }
}

class NodeB implements Node {

    @Override
    public void accept(NodeVisitor visitor) {
        visitor.visit(this);
    }

    @Override
    public String toString() {
        return "Node B";
    }
}

class NodeVisitor1 implements NodeVisitor {

    @Override
    public void visit(Node node) {
        System.out.println("Node visitor 1, node " + node);
    }
}

class NodeVisitor2 implements NodeVisitor {

    @Override
    public void visit(Node node) {
        System.out.println("Node visitor 2, node " + node);
    }
}

The output is:

Node visitor 1, node Node A
Node visitor 2, node Node A
Node visitor 1, node Node B
Node visitor 2, node Node B
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文