Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does double dispatch work in Visitor pattern?

Tags:

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?

like image 834
BOSS Avatar asked Jul 20 '11 12:07

BOSS


People also ask

Is double dispatch a design pattern?

Double dispatch is a technical term to describe the process of choosing the method to invoke based both on receiver and argument types. A lot of developers often confuse double dispatch with Strategy Pattern. Java doesn't support double dispatch, but there are techniques we can employ to overcome this limitation.

Why is double dispatch useful?

Use cases. Double dispatch is useful in situations where the choice of computation depends on the runtime types of its arguments. For example, a programmer could use double dispatch in the following situations: Sorting a mixed set of objects: algorithms require that a list of objects be sorted into some canonical order ...

How does the visitor pattern work?

The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates. This pattern can be observed in the operation of a taxi company. When a person calls a taxi company (accepting a visitor), the company dispatches a cab to the customer.

What is dispatcher pattern?

The dispatcher pattern is a method for scheduling (or dispatching) code for execution from a worker-thread to the main-thread. Actually... this pattern can be used to schedule code to any single thread, however it is most commonly used with the main-thread. It is quite simple to use.


2 Answers

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

NodesVisitors

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

like image 188
Kaushalya Avatar answered Oct 06 '22 23:10

Kaushalya


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

like image 37
salman.mirghasemi Avatar answered Oct 06 '22 23:10

salman.mirghasemi