Start course
4h 56m

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.


Basic knowledge of Java programming.



Hello dear friends. In this video, we will examine the visitor design pattern. 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 this separation is the ability to add new operations to existing object structures without modifying the structures. It's one way to follow the open-closed principal. The purpose of a visitor pattern is to define a new operation without introducing modifications to an existing object structure. This pattern comes under behavior pattern category. In visitor pattern, we use a visitor class which changes the executing algorithm of an element class. By this way, execution algorithm of elements can vary as and when visitor varies.

This pattern comes under behavior pattern category. As per the pattern, element object has to accept the visitor object, so that visitor object handles the operation on the element object. Let's examine the participants of visitor pattern on UML. Visitor is an interface or 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 abstract visitor must be implemented. Each visitor will be responsible for different operations. This will be our concrete visitor. Visitor is an interface which declares the accept operation. This is the entry point which enables an object to be visited by the visitor object. Concrete visitable classes implements the visitable interface or class and defines the accept operation.

The visitor object is parsed to this object using the accept 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 classes defined the object structure, you can use the visitor pattern. Now, if you'd 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 country visitor interface. Let's define countries in this interface; Italy, USA, and Canada. Now we need an interface named country. Let's add a method to call country visitor. This will take country visitor interface as 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 countries list as a class, name will be countries. 

First, we need an array. In constructor, let's add our countries, implements country. Now we will override method in country, done. Let's define concrete elements. We have to override countries method. Italy, USA, and Canada, done. Now the structure is ready. Let's use. First, define an instance from countries, define visitor interface on visitor. Now call countries method. Now, when we run project, we can see the countries that we have visited. Let's try. Look, 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. See you in the next video.


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.

Covered Topics