|
| [Behavioral Patterns] The Visitor Pattern
|
|
| 作者:Bryan Lau 来源:www.javaresearch.org 发布时间:2006-02-28 12:44:47.353 |
|
|
Intent It builds on the double dispatching scheme, in other words add functionality to a collection of classes and encapsulate the methods it uses.
Motivation Many distinct and unrelated operations need to be performed on node objects in a heterogeneous aggregate structure. You want to avoid "polluting" the node classes with these operations. And, you don't want to have to query the type of each node and cast the pointer to the correct type before performing the desired operation. 1. Add functions to class libraries for which I either do not have the source or cannot change the source; 2. Obtain data from a disparate collection of unrelated classes and use it to present the results of a global calculation to the user program; 3. Gather related operations into a single class rather than force you to change or derive classes to add these operations; 4. Collaborate with the Composite pattern.
The steps are as follow: 1. Add an accept(Visitor) method to the "element" hierarchy; 2. Create a "visitor" base class w/ a visit() method for every "element" type; 3. Create a "visitor" derived class for each "operation" to do on "elements"; 4. Client creates "visitor" objects and passes each to accept() calls.
Code's snapshot:
-
- interface Element {
- public void accept( Visitor v ); // first dispatch // 1. accept(Visitor)
- } // interface
-
- class This implements Element {
- public void accept( Visitor v ) { v.visit( this ); } // 1. accept(Visitor)
- public String thiss() { return "This"; } // implementation
- }
-
- class That implements Element {
- public void accept( Visitor v ) { v.visit( this ); }
- public String that() { return "That"; }
- }
-
- class TheOther implements Element {
- public void accept( Visitor v ) { v.visit( this ); }
- public String theOther() { return "TheOther"; }
- }
-
- interface Visitor { // 2. Create a "visitor"
- public void visit( This e ); ////// second dispatch // base class with a
- public void visit( That e ); // visit() method for
- public void visit( TheOther e ); // every "element"
- } // type
-
- class UpVisitor implements Visitor { // 3. Create a "visitor"
- public void visit( This e ) { // derived class for
- System.out.println( "do Up on " + e.thiss() ); } // each "operation"
- public void visit( That e ) { // to perform on
- System.out.println( "do Up on " + e.that() ); } // "elements"
- public void visit( TheOther e ) {
- System.out.println( "do Up on " + e.theOther() ); }
- }
-
- class DownVisitor implements Visitor {
- public void visit( This e ) {
- System.out.println( "do Down on " + e.thiss() ); }
- public void visit( That e ) {
- System.out.println( "do Down on " + e.that() ); }
- public void visit( TheOther e ) {
- System.out.println( "do Down on " + e.theOther() ); }
- }
-
- class VisitorDemo {
- public static Element[] list = { new This(), new That(), new TheOther() };
- public static void main( String[] args ) {
- UpVisitor up = new UpVisitor(); // 4. Client creates
- DownVisitor down = new DownVisitor(); // "visitor" objects
- for (int i=0; i < list.length; i++) // and passes each
- list[i].accept( up ); // to accept() calls
- for (int i=0; i < list.length; i++)
- list[i].accept( down );
- } }
// do Up on This // do Down on This // do Up on That // do Down on That // do Up on TheOther // do Down on TheOther
When to use It is useful when you want to perform the right algorithm, based on the type of two (or more) objects (aka "double dispatch"). Example algorithms might be: process a "collision" between different kinds of SpaceGame objects, compute the distance between different kinds of shapes, or compute the intersection between different kinds of shapes. Here I want to appeal to people to apply them fequestly. Practice! Practice! Practice! In the real world. I rather advocate the method of case-driven stuty, so post your cases or thoughts no matter in any language(chinese, english...). *********** * Problem *********** "If you want to add a new Visitable object, you have to change the Visitor interface, and then implement that method in each of your Visitors."
|
|
|