Visitor パターン
例

下記の例は、Java プログラミング言語のものである。
interface Visitor {
    void visit(Wheel wheel);
    void visit(Engine engine);
    void visit(Body body);
    void visitCar(Car car);
    void visitVehicle(Vehicle vehicle);
}
 
class Wheel {
    private String name;
    Wheel(String name) {
        this.name = name;
    }
    String getName() {
        return this.name;
    }
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
 
class Engine {
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
 
class Body {
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
 
 
abstract class Vehicle {
    protected Engine  engine = new Engine();
    protected Body    body   = new Body();
    protected Wheel[] wheels;
 
    public Engine getEngine() {
        return this.engine;
    }
    public Body getBody() {
        return this.body;
    }
    public Wheel[] getWheels() {
        return this.wheels;
    }
    public void accept(Visitor visitor) {
        visitor.visitVehicle(this);
    }
}
 
class Car extends Vehicle {
    public Car() {
        super();
        this.wheels = new Wheel[]
          { new Wheel("front left"), new Wheel("front right"),
            new Wheel("back left") , new Wheel("back right")  };
    }
 
    public void accept(Visitor visitor) {
         visitor.visitCar(this);
    }
}
 
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 visitVehicle(Vehicle vehicle) {
        System.out.println("Visiting vehicle");
    }
    public void visitCar(Car car) {
        System.out.println("Visiting car");
        car.getEngine().accept(this);
        car.getBody().accept(this);
        for(Wheel wheel : car.getWheels()) {
            wheel.accept(this);
        }
    }
 
}
 
class DoVisitor implements Visitor {
    public void visit(Wheel wheel) {
        System.out.println("Steering my wheel");
    }
    public void visit(Engine engine) {
        System.out.println("Starting my engine");
    }
    public void visit(Body body) {
        System.out.println("Moving my body");
    }
    public void visitCar(Car car) {
        System.out.println("Starting my car");
        car.getEngine().accept(this);
        car.getBody().accept(this);
        for(Wheel wheel : car.getWheels()) {
            wheel.accept(this);
        }
    }
    public void visitVehicle(Vehicle vehicle) {
        System.out.println("Starting my vehicle");
    }
}
 
public class VisitorDemo {
    static public void main(String[] args){
        Car car = new Car();
        Visitor printVisitor = new PrintVisitor();
        Visitor doVisitor = new DoVisitor();
        car.accept(printVisitor);
        car.accept(doVisitor);
    }
}
下記の例は、C++ プログラミング言語のものである。
#include <iostream>
#include <ostream>
#include <vector>

class Wheel;
class Engine;
class Body;
class Car;

class Visitor {
public:
	Visitor() { }
	virtual ~Visitor() { }

	virtual void visit(Wheel &) = 0;
	virtual void visit(Engine &) = 0;
	virtual void visit(Body &) = 0;
	virtual void visit(Car &) = 0;
};

class TestVisitor : public Visitor {
public:
	TestVisitor() : Visitor() { }
	virtual ~TestVisitor() { }

	virtual void visit(Wheel &) {
		std::cout << "Visiting Wheel" << std::endl;
	}
	virtual void visit(Engine &) {
		std::cout << "Visiting Engine" << std::endl;
	}
	virtual void visit(Body &) {
		std::cout << "Visiting Body" << std::endl;
	}
	virtual void visit(Car &) {
		std::cout << "Visiting Car" << std::endl;
	}
};

class CarElement {
public:
	CarElement() { }
	virtual ~CarElement() { }

	virtual void accept(Visitor &) = 0;
};

class Wheel : public CarElement {
public:
	virtual void accept(Visitor & visitor) {
		visitor.visit(*this);
	}
};

class Engine : public CarElement {
public:
	virtual void accept(Visitor & visitor) {
		visitor.visit(*this);
	}
};

class Body : public CarElement {
public:
	virtual void accept(Visitor & visitor) {
		visitor.visit(*this);

		for (std::vector<Wheel>::iterator i = _wheels.begin(); i != _wheels.end(); ++i) {
			i->accept(visitor);
		}
	}

private:
	std::vector<Wheel> _wheels;
};

class Car : public CarElement {
public:
	virtual void accept(Visitor & visitor) {
		visitor.visit(*this);

		_engine.accept(visitor);
		_body.accept(visitor);
	}

private:
	Engine _engine;
	Body _body;
};

int main() {
	Car car;
	TestVisitor visitor;

	car.accept(visitor);
}