适配器模式

来源:互联网 发布:win7无法安装软件 编辑:程序博客网 时间:2024/06/08 19:10

一个适配使得因接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。

<?php   class Adaptee{   public function realRequest(){   echo "这是被适配者真正的调用方法";   }   }   interface Target{   public function request();   }   class Adapter implements Target{   protected  $adaptee;   function __construct(Adaptee $adaptee){   $this->adaptee=$adaptee;   }   public function request(){   echo "适配器转换:";   $this->adaptee->realRequest();   }   }      $adaptee=new Adaptee();   $target= new Adapter($adaptee);   $target->request();?>



原创粉丝点击