Visitor: Implementation and Example

Learn the Visitor design pattern by implementing an example program.

Problem statement

Let’s imagine that we have a library that’s capable of converting between some basic HTML and plain text. However, we realize it doesn’t deal with all HTML element types. Then we realize that we don’t want to just deal with plain text. We want to convert between HTML and Markdown.

But the problem is that we can’t change the original functionality because this will break some components of the system already using it. So, we call the Visitor design pattern to our rescue.

Creating a console application

We’ll create a console application project. Then, we’ll add the following interface to enable our existing classes to accept a Visitor object.

Get hands-on with 1200+ tech skills courses.