PHP 适配器模式

| 选择喜欢的代码风格  

PHP 适配器模式场景描述


比如在 DSP 广告投放过程中,对百度 BES AdX 渠道返回广告,调用 $Baidu->sendResponse($material); 就可以给 AdX 提供物料的参与竞价响应,但过一阵子,又要对接其他渠道,或者这个方法名变了…… 显然将每个调用这个代码的地方都改一次是不现实也不优雅的,这个时候就可以考虑使用适配器模式封装。

PHP 原始代码


class BaiduBES{
    public function sendResponse($material){
        echo "...".$material;
    }
}
class GoogleAdX{
    public function doRes($material){
        echo "...".$material;
    }
}

PHP 调用方式:

$baidu = new BaiduBES();
$baidu->sendResponse('xxx');//返回物料响应给百度 BES ADX

$google = new GoogleAdX();
$google->doRes('xxx');//返回物料响应给 Google AdX

PHP 适配器模式


class Adapter implements AdResponseMethod{
    private $adapter;

    function __construct($adaptee){
        $this->adapter = $adaptee;
    }

    public function response($material){
        //判断是哪个类
        $className = get_class($this->adapter);
        if($className == "BaiduBES"){
            $this->adapter->sendResponse($material);
        }elseif ($className == "GoogleAdX") {
            $this->adapter->doRes($material);
        }
    }
}

PHP 调用方式:

$baidu = new BaiduBES();
$adapter = new Adapter($baidu);
$adapter->response('xxx');//通过百度 BES 响应广告请求

$google = new GoogleAdX();
$adapter = new Adapter($google);
$adapter->response('xxx');//通过 GoogleAdX 响应广告

PHP 适配器模式改造


或者直接将类的声明封装在适配器的内部:

class Adapter implements AdResponseMethod{
    private $adapter;
    
    function __construct($className){
        if(strtolower($className) == "baidubes"){
            $this->adapter = new BaiduBES();
        }elseif (strtolower($className) == "googleadx") {
            $this->adapter = new GoogleAdX();
        }
    }

    public function response($material){
        //判断是哪个类
        $className = get_class($this->adapter);
        if($className == "BaiduBES"){
            $this->adapter->sendResponse($material);
        }elseif ($className == "GoogleAdX") {
            $this->adapter->doRes($material);
        }
    }
}

PHP 调用方式:

$adapter = new Adapter('baidubes');
$adapter->response('xxx');//通过百度 BES 响应广告请求

$adapter = new Adapter('googleadx');
$adapter->response('xxx');//通过 GoogleAdX 响应广告

PHP 适配器完整例子


<?php
//目标
interface AdResponse{
  public function bidRequest();
  public function bidResponse();
}

//适配器
class AdManager implements AdResponse {
  private $adProxy;

  function __construct($adx_name){    
    $cls = strtolower($adx_name);

    switch($cls){
      case 'baidu':
        $this->adProxy = new Baidu();
        break;
      case 'google':
        $this->adProxy = new Google();
        break;
      default:
        break;
    }
    // print_r($this->adProxy); 
    
  }

  public function bidRequest(){
    echo "BidRequest";
  }

  public function bidResponse(){
    
    switch (strtolower(get_class($this->adProxy))){
      case 'baidu':
        $this->adProxy->sendBaidu();
        break;
      case 'google':
        $this->adProxy->sendGoogle();
        break;
      default:
        break;
    }

  }

}

class Baidu {
  public function sendBaidu(){
    echo "Baidu Bid Response";
  }
}

class Google {
  public function sendGoogle(){
    echo "Google Bid Response!";
  }

}

PHP 调用方式:

<?php
$adx = new AdManager('baidu');
$adx->bidResponse();

echo "<hr>";

$adx = new AdManager('google');
$adx->bidResponse();

设计模式扩展阅读:




发表评论