Visitor Pattern

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 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.

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 = 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; ii.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

External links

 

<< PreviousWord BrowserNext >>
1672
1563
1562
1558
1559
1556
1553
1552
1551
1547
1548
1549
1545
1544
1541
2002 winter olympics
1538
1539
1533
1532
1530
1529
1527
1526
1525
1524
1518
1517
1516
1515
1514
1513
1511
1510
1507
1508
1509
1505
1504
1502
1501
perpignan
1250
1251