Visitor pattern

From Academic Kids

In software engineering, the visitor design pattern is a way of separating an algorithm from an object structure.

The idea is to use an object structure of element classes, each of which has 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 , Smalltalk, and C++. For an explanation of how double dispatch differs from function overloading, see the Double dispatch is more than function overloading in the double dispatch article.)

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.

Contents

Examples

Python

The following example uses the Python programming language:

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

The following example uses the Java programming language:

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);
    }
}

C++

The following example uses the C++ programming language:


class Visitor {
public:
  virtual void visit(Wheel *wheel) = 0;
  virtual void visit(Engine *engine) = 0;
  virtual void visit(Body *body) = 0;
  virtual void visit(Car *car) = 0;
};

class Wheel {
public:
  std::string name;

  void accept(Visitor *visitor) {
    visitor->visit(this);
  }

  Wheel(std::string const& name_) : name(name_) {
  }
};

class Engine {
public:
  void accept(Visitor *visitor) {
    visitor->visit(this);
  }
};

class Body {
public:
  void accept(Visitor *visitor) {
    visitor->visit(this);
  }
};

class Car {
private:
  Engine *engine;
  Body *body;
  Wheel *wheels[4];

public:
  void accept(Visitor *visitor) {
    visitor->visit(this);
    engine->accept(visitor);
    body->accept(visitor);
    for (int i = 0; i < 4; i++) {
      wheels[i]->accept(visitor);
    }
  }

  Car() : engine(new Engine()), body(new Body()) {
    wheels[0] = new Wheel("front left");
    wheels[1] = new Wheel("front right");
    wheels[2] = new Wheel("back left");
    wheels[3] = new Wheel("back right");
  }

  ~Car() {
    delete engine;
    delete body;
    for (int i = 0; i < 4; i++) {
      delete wheels[i];
    }
  }
};

class PrintVisitor : public Visitor {
public:
  void visit(Wheel *wheel) {
    std::cout << "Visiting " << wheel->name << " wheel" << std::endl;
  }

  void visit(Engine *engine) {
    std::cout << "Visiting engine" << std::endl;
  }

  void visit(Body *body) {
    std::cout << "Visiting body" << std::endl;
  }

  void visit(Car *car) {
    std::cout << "Visiting car" << std::endl;
  }
};

int main() {
  Car car;
  PrintVisitor visitor;
  car.accept(&visitor);
  return 0;
}

See also

External links

zh-cn:访问者模式 es:Visitor (patrn de diseo)

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools