How does double dispatch work in Visitor pattern?

末鹿安然 提交于 2019-11-27 20:36:22

问题


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?


回答1:


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).




回答2:


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

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).




回答3:


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).




回答4:


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


来源:https://stackoverflow.com/questions/6762256/how-does-double-dispatch-work-in-visitor-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!