score:3

Accepted answer

The Visitor pattern helps you to avoid creating if or switch statements that will test the type of the visited element. The avoidance of these statements is generally considered as a good practice. If the operation you implement does not differentiate between various possible types of elements that can be visited, then, yes, the visitor pattern does not bring you any advantage over your alternative approach any other approach.

The problem is maybe in the understanding of the Visitor pattern: it is mainly about implementing the double-dispatch in languages that does not offer this feature, or (this is the case of C#) where the usage double-dispatch brings considerable performance issues. It is not about tree traversing. It can be even used for classes that does not form a hierarchical structure. The GoF book says that the traversing algorithm may be implemented either by the Visitor itself, either by the visited elements, or even either by the client.

Edit: I read you question again carefully, I think that your approach is, let's say, an alternative implementation of the Visitor pattern, where the Visitor is not a class, but set of lambda functions.


Related Articles