Visitor Design Pattern Examples
Learn about the visitor design pattern with some coding exampels.
We'll cover the following
C++ example
Let’s look at an example where we have a base class of File
, which is implemented by three classes named ArchivedFile
, SplitFile
, and ExtractedFile
. Let’s assume that we want to apply an operation Dispatch
to all the files without affecting the object’s structure. Adding this method to the File
class hierarchy doesn’t sound like a good idea because Dispatch
doesn’t really belong in that object structure.
We’ll create a visitor class called AbstracttDispatcher
and a concrete visitor class called Dispatcher
. Depending on the object type, it will add an operation to the file objects. Let’s look at its code.
Get hands-on with 1400+ tech skills courses.