Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code.
Read why Visitors can’t be simply replaced with method overloading in our article Visitor and Double Dispatch.
A real-world example of the Visitor pattern is serde serialization framework and its deserialization model (see Serde data model).
Visitor should be implemented for a deserializable type.
Visitor is passed to a
Deserializer (an “Element” in terms of the Visitor Pattern), which accepts and drives the
Visitor in order to construct a desired type.
Let’s reproduce this deserializing model in our example.