# Visitor

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Visitor (which is a Structural Pattern).

# Visitor Pattern - theory

Visitor Design Pattern in Java is used when we want to call the same method of the same object (visit() method of the CountryVisitor class, in my example ) and that method to behave differently function of the object that call that method. The Visitor design pattern simplifies the addition of new operations. For instance, in my example is not very difficult to add more countries in my Java project.

Take a look at the following UML diagram representing the Visitor design pattern (for my example):

# Visitor Pattern - example

Here is an example of using the Visitor Design Pattern in Java:

package visitor.java.pattern.example;
 
public interface Country {
    public void callCountryVisit(CountryVisitorInterface countryVisitorInterface);
}
package visitor.java.pattern.example;
 
public class Canada implements Country {
    public void callCountryVisit(CountryVisitorInterface countryVisitorInterface){
        countryVisitorInterface.visit(this);
    };
}
package visitor.java.pattern.example;
 
public class France implements Country {
    public void callCountryVisit(CountryVisitorInterface countryVisitorInterface){
        countryVisitorInterface.visit(this);
    };
}
package visitor.java.pattern.example;
 
public class Germany implements Country {
    public void callCountryVisit(CountryVisitorInterface countryVisitorInterface){
        countryVisitorInterface.visit(this);
    };
}
package visitor.java.pattern.example;
 
public class Countries implements Country {
     
     private Country[] countries;
 
     public Countries(){
         countries = new Country[] {new Canada(), new France(), new Germany()};     
     } 
     
     public void callCountryVisit(CountryVisitorInterface countryVisitorInterface){
 
         for (int i = 0; i < countries.length; i++) {
             countries[i].callCountryVisit(countryVisitorInterface);  
          }
     };
}
package visitor.java.pattern.example;
 
public interface CountryVisitorInterface {
    public void visit(Canada canada);
    public void visit(France france);
    public void visit(Germany germany);
}
package visitor.java.pattern.example;
 
public class CountryVisitor implements CountryVisitorInterface{
     
    public void visit(Canada canada){
        // Do something when you visit Canada
        System.out.println("You are visiting Canada now !");
    };
     
    public void visit(France france){
        // Do something when you visit France
        System.out.println("You are visiting France now !");
    };
     
    public void visit(Germany germany){
        // Do something when you visit Germany
        System.out.println("You are visiting Germany now !");
    };
     
}
package visitor.java.pattern.example;
 
 
public class VisitorJavaPatternExample {
    public static void main(String[] args) {
 
         Country countries = new Countries();
         CountryVisitorInterface countryVisitor = new CountryVisitor();
          
         countries.callCountryVisit(countryVisitor);
           
    }
}