javadesign-patternsvisitor-patterndouble-dispatch

How does double dispatch work in Visitor pattern?


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?


Solution

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