Visitor Pattern and Double Dispatch Explained

Easy to understand guide to help you understand the visitor pattern and double dispatch with concrete examples.

Jacob Toftgaard Rasmussen
Geek Culture

--

Photo by Maria Lupan on Unsplash

You will be presented with clear explanations of both topics as well as intuitive examples and code snippets. As always, I have included screenshots from the IDE as well as made a public git repository where you can access all the code referenced in the article. The code is also included in the article as Github Gists.

Overview of the article

  • Introduction
  • Explanation of double dispatch
  • Explanation of the visitor pattern
  • Important note — what are good use cases of the visitor pattern?
  • Summary

Explanation of double dispatch

Double dispatch is a language feature that makes it possible to call different functions dynamically depending on the runtime type of two objects. Both the object from which the method is called and also the object given to the method as an argument. As an example, imagine that you have some movie characters that can interact with each other, and you want to implement different outcomes depending on which two characters interact…

--

--

Jacob Toftgaard Rasmussen
Geek Culture

I explore the wonderful world of software engineering. I write guides, informative articles and silly projects ;) — Keep learning!