You are on page 1of 7

Visitor

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

Where to use & benefits


Add operations on a bunch of classes which have different interfaces. Traverse the object structure to gather related operations Easy to add new operations. Crossing class hierarchies may break encapsulation. Related patterns include o Composite, which may be applied in a visitor pattern. o Interpreter, which may be used to go through structure and define new operation in a visitor pattern.

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

In Visitor pattern, we use a visitor class which changes the executing algorithm of an element class. By this way, execution algorithm of element can varies as 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.

Implementation
We're going to create a ComputerPart interface defining accept opearation.Keyboard, Mouse, Monitor and Computer are concrete classes implementing ComputerPart interface. We'll define another interface ComputerPartVisitor which will define a visitor class operations. Computer uses concrete visitor to do corresponding action. VisitorPatternDemo, our demo class will use Computer, ComputerPartVisitor classes to demonstrate use of visitor pattern.

Step 1
Define an interface to represent element. ComputerPart.java public interface class ComputerPart { public void accept(ComputerPartVisitor computerPartVisitor); }

Step 2
Create concrete classes extending the above class.

Keyboard.java public class Keyboard implements ComputerPart {

@Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } } Monitor.java public class Monitor implements ComputerPart {

@Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } } Mouse.java public class Mouse implements ComputerPart {

@Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } } Computer.java public class Computer implements ComputerPart {

ComputerPart[] parts;

public Computer(){ parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()}; }

@Override

public void accept(ComputerPartVisitor computerPartVisitor) { for (int i = 0; i < parts.length; i++) { parts[i].accept(computerPartVisitor); } computerPartVisitor.visit(this); } }

Step 3
Define an interface to represent visitor. ComputerPartVisitor.java public interface ComputerPartVisitor { public void visit(Computer computer); public void visit(Mouse mouse); public void visit(Keyboard keyboard); public void visit(Monitor monitor); }

Step 4
Create concrete visitor implementing the above class. ComputerPartDisplayVisitor.java public class ComputerPartDisplayVisitor implements ComputerPartVisitor {

@Override public void visit(Computer computer) { System.out.println("Displaying Computer."); }

@Override public void visit(Mouse mouse) { System.out.println("Displaying Mouse."); }

@Override public void visit(Keyboard keyboard) { System.out.println("Displaying Keyboard.");

@Override public void visit(Monitor monitor) { System.out.println("Displaying Monitor."); } }

Step 5
Use the ComputerPartDisplayVisitor to display parts of Computer. VisitorPatternDemo.java public class VisitorPatternDemo { public static void main(String[] args) {

ComputerPart computer = new Computer(); computer.accept(new ComputerPartDisplayVisitor()); } }

Step 6
Verify the output. Displaying Displaying Displaying Displaying Mouse. Keyboard. Monitor. Computer.

You might also like