The core concept of the visitor design pattern is based on two interfaces: the visitor– and visitable interface.
The visitor interface declares one method, the visit method, which is then overloaded for each type of object that it has to add functionality to. In our case that would be the Circle-, Triangle-, and Square objects.
Whichever additional functionality we want to add to the shape objects, then must implement the interface and in the implementation of the overloaded method, and state how the functionality should work in that specific scenario.
The visitable interface only declares one method, the accept method, which takes in a visitor object as its only argument.
All shape objects have to implement the visitable interface and override the interface method. In the method, we use the visitor argument to call the visitor method and provide the shape object (this) as the argument.
The advantages of the visitor design pattern is similar to any other design pattern which utilize composition over inheritance, no class explosions, because we do not have to create a class for each variation of algorithm and shape object, and flexibility at runtime, since the association between algorithm and shape object have not been created in the code. Except for the interfaces, which enables the decoupled code structure.