Visitor


Definition

Define a new operation to deal with the classes of the elements without changing their structures.

Where to use & benefits

Example

The following is a dummy program. Two interfaces involved: Visitor and Pizza. The Pizza system is completely independent. "How to get it" tries to add new operations to the Pizza system. It is done by adding another interface Visitor and parameterizing Pizza interface in the abstract method visit(composite pattern). The "how to get" classes implement Visitor interface and make a connection with Pizza system.

import java.util.*;
interface Visitor {
    public void visit(Pizza p);
}
interface Pizza {
    public String order();
}
class PopJohn implements Pizza {
    final String name = "PopJohn";
    public String order() {
        return name;
    }
}
class PizzaHut implements Pizza {
    final String name = "PizzaHut";
    public String order() {
        return name;
    }
}
class GodFather implements Pizza {
    final String name = "GodFather";
    public String order() {
        return name;
    }
}
class ByPickup implements Visitor {
    private String name;
    private final String method = "By pick up";
    public void visit(Pizza p) {
        name = p.order();
    }
    
    public String toString() {
         return name + " " + method;
    }
}
class ByEatin implements Visitor {
    private String name;
    private final String method = "By eat in";
    
    public void visit(Pizza p) {
        name = p.order();
    }
    
    public String toString() {
        return name + " " + method;
    }
}
class ByDelivery implements Visitor {
    private String name;
    private final String method = "By delivery";
    
    public void visit(Pizza p) {
        name = p.order();
    }
    
    public String toString() {
         return name + " " + method;
    }
}
class Dinner {
    public Pizza getDinner() {
        switch ((int)(Math.random()*3)){
            case 0: return new PopJohn(); 
            case 1: return new PizzaHut(); 
            case 2: return new GodFather(); 
            default: return null;
        }
    }
    public Visitor howto() {
        switch ((int)(Math.random()*3)){
            case 0: return new ByPickup(); 
            case 1: return new ByEatin(); 
            case 2: return new ByDelivery(); 
            default: return null;
        }
    }    
}
class Test {
    
    public static void main(String[] args) {
        List pizzaList = new ArrayList();
            pizzaList.add(new PopJohn());
            pizzaList.add(new PizzaHut());
            pizzaList.add(new GodFather());
        Iterator it = pizzaList.iterator();
        System.out.println("How many pizza restaurants in this area?");
        while (it.hasNext()) {
           System.out.println(((Pizza)it.next()).order());
        }
        Dinner d = new Dinner();
        Pizza pza = d.getDinner();
        Visitor v = d.howto();
        v.visit(pza);
        System.out.println("\nWhich store for dinner?");
        System.out.println(v);
    }
}
//run it several times.

 C:\ Command Prompt
 
C:\> java Test
How many pizza restaurants in this area?
PopJohn
PizzaHut
GodFather

Which restaurant for dinner?
GodFather By delivery

C:\> java Test
How many pizza restaurants in this area?
PopJohn
PizzaHut
GodFather

Which restaurant for dinner?
PizzaHut By pick up

C:\> java Test
How many pizza restaurants in this area?
PopJohn
PizzaHut
GodFather

Which restaurant for dinner?
PizzaHut By delivery

Return to top