The Visitor
Pattern is an
Object Oriented Design Pattern which solves a few, nice OO design problems.
For more info on Design Patterns I would check out the book Design Patterns by the gang of four. Note that the
Visitor patten is defined on page 331.
The Visitor pattern is very simple to implement. Basically you have one class which is 'visitable' by a visitor.
Usually you would have it visited by a some class which you need to perform an operation or use as the basis for other operations.
So for example (this is Java code)
public class VisitablePrinter {
public void accept( Visitor visitor ) {
System.out.println( "Hello visitor: " visitor.toString() );
}
}
public class Visitor {
public String toString() {
return "Hello yourself!"
}
}
Now if you call the accept method on the VisitablePrinter you will see the following output
Hello visitor: Hello yourself!
This becomes really cool because now, in the future, you can call an accept method on ANY TYPE OF Visitable class.
This means you can do really crazy stuff without modifying Visitor class.