Visitor Design Pattern
Start course
3h 59m

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello, dear friends. In this video, we will examine the Visitor Design Pattern. So, let's begin. In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of the separation is the ability to add new operations to existing object structures without modifying the structures. It is one way to follow the open-closed principle. The purpose of a visitor pattern is to define a new operation without introducing modifications to an existing object structure. This pattern comes under the behavior pattern category. In the visitor pattern, we use a visitor class which changes the executing algorithm of an element class. This way, the execution algorithm of an element can vary as and when the visitor varies. This pattern comes under the behavior pattern category.

As per the pattern, the element object has to accept the visitor object so that the visitor object handles the operation on the element object. Let's examine the participants of the visitor pattern on UML. Visitor is an interface or an abstract class used to declare the visit operations for all the types of visitable classes. For each type of visitor, all the visit methods declared in an abstract visitor must be implemented. Each visitor will be responsible for different operations. This will be our concrete visitor. Visitable is an interface which declares the except operation. This is the entry point which enables an object to be visited by the visitor object. Concrete visitable classes implement the visitable interface or class and defines the except operation. The visitor object is passed to this object using the except operation.

Visitor pattern simplifies the addition of new operations and also it gathers related operations while separating unrelated ones. We should know the return type of visit methods at the time of designing, otherwise, we will have to change the interface and all of its implementations. If there are too many implementations of visitor interface, it makes it hard to extend. When an object structure contains many objects with different interfaces and there is a need to perform operations on these objects depending on their concrete class or many different and unrelated operations must be performed on objects in a structure or when class is defined the object structure, you can use the visitor pattern. Now, if you like, let's create a project and apply the visitor pattern. Create a new project and name it Visitor. The name is Visitor. So, let's make a little world tour. First we need an interface.

Let's name it CountryVisitorInterface. Let's define counties in this interface. Italy, USA, and Canada. Now we need an interface named country. Let's add a method to callCountryVisit. This will make the CountryVisitorInterface as a parameter. Let's define countries one by one. First, start with Italy. Implements country. Now override method. Go on with USA. Last one is Canada. Let's define counties list as a class. Name will be countries implements country. First we need an array. In constructor, let's add our countries. Now we will override method in country. Done. Let's define concrete elements. We have to override countries methods; Italy, USA, and Canada. Done.

Now the structure is ready, let's use. First to find an instance from countries. Define visitor interface on visitor. Now call the country's method. Now when we run the project, we can see the countries that we visited. Let's try. Look at the result. Italy, USA, and Canada. Done. We have now finished looking at the visitor pattern. In the next video, we will examine the mediator design pattern. So, I'll see you in the next video, my friends.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.