Encyclopedia > Visitor pattern

  Article Content

Visitor pattern

The visitor design pattern is a way of separating an algorithm from an object structure.

The basic idea is that you have a set of element classes that form an object structure. Each of these element classes has an accept method that takes a Visitor object as an argument. The Visitor is an interface that has a different visit method for each element class. The accept method of an element class calls back the visit method for its class. Separate concrete Visitor classes can then be written that perform some particular operations.

(One of these visit methods of a concrete Visitor can be thought of as methods not of a single class, but rather methods of a pair of classes: the concrete visitor and the particular element class. Thus the visitor pattern simulates double dispatch[?] in a conventional single dispatch object-oriented language such as Java or C++.)

The visitor pattern also specifies how iteration occurs over the object structure. In the simplest version, where each algorithm needs to iterate in the same way, the accept method of a container element, in addition to calling back the visit method of the Visitor, also passes the Visitor object to the accept method of all its constituent child elements.

Python Example

 class Wheel:
     def __init__(self,name):
         self.name = name
     def accept(self,visitor):
         visitor.visitWheel(self)
 
 class Engine:
     def accept(self,visitor):
         visitor.visitEngine(self)
 
 class Body:
     def accept(self,visitor):
         visitor.visitBody(self)
 
 class Car:
     def __init__(self):
         self.engine = Engine()
         self.body   = Body()
         self.wheels = [ Wheel("front left"), Wheel("front right"),
                         Wheel("back left") , Wheel("back right") ]
     def accept(self,visitor):
         visitor.visitCar(self)
         self.engine.accept( visitor )
         self.body.accept( visitor )
         for wheel in self.wheels:
             wheel.accept( visitor )
 
 class PrintVisitor:
     def visitWheel(self,wheel):
         print "Visiting "+wheel.name+" wheel"
     def visitEngine(self,engine):
         print "Visiting engine"
     def visitBody(self,body):
         print "Visiting body"
     def visitCar(self,car):
         print "Visiting car"
 
 car = Car()
 visitor = PrintVisitor()
 car.accept(visitor)
 
Java Example

 interface Visitor{
     void visit(Wheel wheel);
     void visit(Engine engine);
     void visit(Body body);
     void visit(Car car);
 }

 class Wheel{
     private String name;
     Wheel(String name){
         this.name = name;
     }
     String getName(){
         return this.name;
     }
     void accept(Visitor visitor){
         visitor.visit(this);
     }
 }
  
 class Engine{
     void accept(Visitor visitor){
         visitor.visit(this);
     }
 }

 class Body{
     void accept(Visitor visitor){
         visitor.visit(this);
     }
 }

 class Car{
     private Engine  engine = new Engine();
     private Body    body   = new Body();
     private Wheel[] wheels 
         = { new Wheel("front left"), new Wheel("front right"),
             new Wheel("back left") , new Wheel("back right")  };
     void accept(Visitor visitor){
         visitor.visit(this);
         engine.accept( visitor );
         body.accept( visitor );
         for(int i=0; i<wheels.length; ++i)
             wheels[i].accept( visitor );
     }
 }

 class PrintVisitor implements Visitor{
     public void visit(Wheel wheel){
         System.out.println("Visiting "+wheel.getName()
                             +" wheel");
     }
     public void visit(Engine engine){
         System.out.println("Visiting engine");
     }
     public void visit(Body body){
         System.out.println("Visiting body");
     }
     public void visit(Car car){
         System.out.println("Visiting car");
     }
 }

 public class VisitorDemo{
     static public void main(String[] args){
         Car car = new Car();
         Visitor visitor = new PrintVisitor();
         car.accept(visitor);
     }
 }


See also: design pattern, Composite pattern, Hierarchical visitor pattern



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Holtsville, New York

... of 18.0 km² (7.0 mi²). 18.0 km² (7.0 mi²) of it is land and none of the area is covered with water. Demographics As of the census of 2000, th ...

 
 
 
This page was created in 23.5 ms