Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>设计模式>>[Behavioralnbsp;Patterns]nbsp;Thenbsp;Visitornbsp;Pattern
[Behavioralnbsp;Patterns]nbsp;Thenbsp;Visitornbsp;Pattern
2007-04-13   来源:www.javaresearch.org  作者:未知

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


  --相关文章--
· 面向对象编程,我的思想 (2007-04-13)
· 面向对象的思维方式 (2007-04-13)
· 通过Javanbsp;Swing看透MVC设计模式 (2007-04-13)
· 适配器模式(Adapternbsp;Pattern) (2007-04-13)
· 追MM与Java的23种设计模式 (2007-04-13)
· 责任链模式(Chainnbsp;ofnbsp;Responsibility) (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接