到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.javaresearch.org无关,不对该信息负责.通过http://www.javaresearch.org/article//showarticle.jsp?column=31&thread=442访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
[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:
  1. interface Element {
  2.    public void accept( Visitor v ); // first dispatch     // 1. accept(Visitor)
  3. }                                                         //    interface
  4. class This implements Element {
  5.    public void   accept( Visitor v ) { v.visit( this ); } // 1. accept(Visitor)
  6.    public String thiss()             { return "This"; }   //    implementation
  7. }
  8. class That implements Element {
  9.    public void   accept( Visitor v ) { v.visit( this ); }
  10.    public String that()              { return "That"; }
  11. }
  12. class TheOther implements Element {
  13.    public void   accept( Visitor v ) { v.visit( this ); }
  14.    public String theOther()          { return "TheOther"; }
  15. }
  16. interface Visitor {                                    // 2. Create a "visitor"
  17.    public void visit( This e ); ////// second dispatch //    base class with a
  18.    public void visit( That e );                        //    visit() method for
  19.    public void visit( TheOther e );                    //    every "element"
  20. }                                                      //    type
  21. class UpVisitor implements Visitor {                   // 3. Create a "visitor"
  22.    public void visit( This e ) {                       //    derived class for
  23.       System.out.println( "do Up on " + e.thiss() ); } //    each "operation"
  24.    public void visit( That e ) {                       //    to perform on
  25.       System.out.println( "do Up on " + e.that() ); }  //    "elements"
  26.    public void visit( TheOther e ) {
  27.       System.out.println( "do Up on " + e.theOther() ); }
  28. }
  29. class DownVisitor implements Visitor {
  30.    public void visit( This e ) {
  31.       System.out.println( "do Down on " + e.thiss() ); }
  32.    public void visit( That e ) {
  33.       System.out.println( "do Down on " + e.that() ); }
  34.    public void visit( TheOther e ) {
  35.       System.out.println( "do Down on " + e.theOther() ); }
  36. }
  37. class VisitorDemo {
  38.    public static Element[] list = { new This(), new That(), new TheOther() };
  39.    public static void main( String[] args ) {
  40.       UpVisitor    up   = new UpVisitor();             // 4. Client creates
  41.       DownVisitor  down = new DownVisitor();           //    "visitor" objects
  42.       for (int i=0; i < list.length; i++)              //    and passes each
  43.          list[i].accept( up );                         //    to accept() calls
  44.       for (int i=0; i < list.length; i++)
  45.          list[i].accept( down );
  46. }  }

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


 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读