Main Page | See live article | Alphabetical index

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): = name
    def accept(self,visitor):

class Engine:
    def accept(self,visitor):

class Body:
    def accept(self,visitor):

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):
        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"
    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()

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){ = name; } String getName(){ return; } 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){
        engine.accept( visitor );
        body.accept( visitor );
        for(int i=0; i
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