当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP设计模式-对象行为型模式-VISITOR

PHP
php sprintf()函数让你的sql操作更安全
php SQLite学习笔记与常见问题分析
使用PHP socke 向指定页面提交数据
jq的get传参数在utf-8中乱码问题的解决php版
PHP 得到根目录的 __FILE__ 常量
PHP 表单提交给自己
PHP4中session登录页面的应用
简单示例AJAX结合PHP代码实现登录效果代码
php+mysql写的简单留言本实例代码
php在线打包程序源码
php intval的测试代码发现问题
php5编程中的异常处理详细方法介绍
php include的妙用,实现路径加密
PHP中$_SERVER的详细参数与说明
php 全文搜索和替换的实现代码
PHP一些常用的正则表达式字符的一些转换
php自动跳转中英文页面
MySql中正则表达式的使用方法描述
说明的比较细的php 正则学习实例
新安装的MySQL数据库需要注意的安全知识

PHP设计模式-对象行为型模式-VISITOR


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 175 ::
收藏到网摘: n/a

    个人认为在23个经典模式中VISITOR是比较难理解的一个,所以决定先讲讲自己对VISITOR的理解。因为马哲认为对事物的认识是从具体到抽象的一个过程,所以在谈理论之前先说一个例子是必要的。这个例子不是实际应用,但我想它还算生动,是个记忆VISITOR模式的好例子吧。

    英国、美国都有自己的核武机构,每个国家的核武机构,都使用不同的接口来进行通讯:

CODE:
    class Country
    {
         // ...
    }
   

1,对于英国,核武密码被分成三个部分,需要用三个接口取得:
CODE:
   interface I1{function get1();};
   interface I2{function get2();};
   interface I3{function get3();};
   class English extends Country implements I1,I2,I3
   {
               function get1(){return '123';}
               function get2(){return '456';}
               function get3(){return '789';}
   }
   

2,对于美国,保管核武密码的方式不一样,使用了5个接口来分别取得密码的部分:
CODE:
   interface Ia{function getA();};
   interface Ib{function getB();};
   interface Ic{function getC();};
   interface Id{function getD();};
   interface Ie{function getE();};
   class America extends Country implements Ia,Ib,Ic,Id,Ie
   {
               function getA(){return 'a';}
               function getB(){return 'b';}
               function getC(){return 'c';}
               function getD(){return 'd';}
               function getE(){return 'e';}
   }
   

如果我们中国欲取得此二国的核武密码,则必须先熟知这二国的核武密码接口。实际行动(runtime)时,
用if..else来判断现在具体是哪个国家,然后调用该国相应的核武密码接口。
CODE:
      class Client
      {
            private function getRealPwd(Country $country)
            {
                  if($country instanceof English)
                  {
                      return $country->get1() mod ( $country->get2() + $country->get3() );
                  }elseif($country instanceof American){
                        return $country->getA() . $country->getB() . $country->getC() . $country->getD() . $country->getE();
                  }elseif(...){
                        ...
                  }
            }
      }