Answer the question
In order to leave comments, you need to log in
What is the essence of the "visitor" pattern?
1) Transferring some function of the subclass tree to a separate class - when it is impossible to implement the method directly in the subclass tree, the method is moved to a separate class.
2) Double dispatching - the element itself calls the desired method in the visitor. (Replacing the conditional statements ̶p̶o̶l̶i̶m̶o̶r̶f̶i̶z̶m̶o̶m̶ with calling the desired method)
They say that double dispatch is not the main idea .
So what's the point - we just move the function out of the subclass tree into a separate class? Is this the essence of the pattern? Or is it still about replacing conditional operators with a method call in the visitor?
Are we violating the OCP?
Answer the question
In order to leave comments, you need to log in
Its essence is that it allows you to be sure that you have processed all possible subtypes of some type. Well, let's say if there is `class Node`, `class Leaf1 : Node`, `class Leaf2 : Node`, then the client code could be written like this
if (node instanceof Node) {
...
} else of (node instanceof Leaf1) {
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question