Difference betwen Visitor pattern & Double Dispatch

In short they come from to different conceptualizations that, in some languages where double dispatch is not natively supported, lead to the visitor pattern as a way to concatenate two (or more) single dispatch in order to have a multi-dispatch surrogate. In long The idea of multiple dispatch is – essentially – allow a call … Read more

Alternative to the visitor pattern?

You might want to have a look at the Strategy pattern. This still gives you a separation of concerns while still being able to add new functionality without having to change each class in your hierarchy. class AbstractShape { IXmlWriter _xmlWriter = null; IShapeDrawer _shapeDrawer = null; public AbstractShape(IXmlWriter xmlWriter, IShapeDrawer drawer) { _xmlWriter = … Read more

Visitor pattern’s purpose with examples [duplicate]

So you’ve probably read a bajillion different explanations of the visitor pattern, and you’re probably still saying “but when would you use it!” Traditionally, visitors are used to implement type-testing without sacrificing type-safety, so long as your types are well-defined up front and known in advance. Let’s say we have a few classes as follows: … Read more

What is the point of accept() method in Visitor pattern?

The visitor pattern’s visit/accept constructs are a necessary evil due to C-like languages’ (C#, Java, etc.) semantics. The goal of the visitor pattern is to use double-dispatch to route your call as you’d expect from reading the code. Normally when the visitor pattern is used, an object hierarchy is involved where all the nodes are … Read more