zoukankan      html  css  js  c++  java
  • php适配器模式

       适配器的使用将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作,主要应用在第三方api的适配和多个类库的选择适配到我们的应用的接口规则。

    例子 

    /**
     * 普通媒体接口
     */
    
    interface MediaInterface
    {
      public function play($file='');
    }
    
    
    /**
     * 音频设备实体
     */
    class AudioPlayer implements MediaInterface
    {
      public function play($file='', $type='')
      {
        switch ($type) {
          case 'mp3':
            echo 'playing file: ' . $file . ".mp3
    ";
            break;
          case 'mp4':
            $adapter = new Adapter($type);
            $adapter->play($file);
            break;
          case 'wma':
            $adapter = new Adapter($type);
            $adapter->play($file);
            break;
    
          default:
            throw new Exception("$type is not supported", 400);
            break;
        }
    
      }
    }
    
    
    
    /**
     * 高级媒体接口
     */
    interface MediaAdvanceInterface
    {
      public function playMp4($file='');
      public function playWma($file='');
    }
    
    
    
    /**
     * mp4高级播放器实体
     */
    class AdvanceMp4Player implements MediaAdvanceInterface
    {
      public function playMp4($file='')
      {
          echo 'AdvanceMp4Player driver playing file: ' . $file . ".mp4
    ";
      }
    
      public function playWma($file='')
      {
          //do nothing
      }
    }
    
    /**
     * wma高级播放器实体
     */
    class AdvanceWmaPlayer implements MediaAdvanceInterface
    {
      public function playMp4($file='')
      {
          //do nothing
      }
    
      public function playWma($file='')
      {
          echo 'AdvanceWmaPlayer driver playing file: ' . $file . ".wma
    ";
      }
    }
    
    /**
     * 高级播放器适配器
     */
    class Adapter
    {
      private $_advancePlayerInstance;
    
      private $_type = '';
    
      public function __construct($type='')
      {
        switch ($type) {
          case 'mp4':
            $this->_advancePlayerInstance = new AdvanceMp4Player();
            break;
          case 'wma':
            $this->_advancePlayerInstance = new AdvanceWmaPlayer();
            break;
    
          default:
            throw new Exception("$type is not supported", 400);
            break;
        }
        $this->_type = $type;
      }
    
      public function play($file='')
      {
        switch ($this->_type) {
          case 'mp4':
            $this->_advancePlayerInstance->playMp4($file);
            break;
          case 'wma':
            $this->_advancePlayerInstance->playWma($file);
            break;
          default:
            break;
        }
      }
    }

       

  • 相关阅读:
    《算法竞赛入门经典》《算法竞赛入门经典——训练之南》:勘误、讨论及代码
    codeforces 340B Maximal Area Quadrilateral(叉积)
    codeforces 340C Tourist Problem(简单数学题)
    codeforces 340A The Wall(简单数学题)
    UVALive 4043 Ants(二分图完美匹配)
    UVA 11865 Stream My Contest(最小树形图)
    UVA 11354 Bond(最小瓶颈路+倍增)
    UVALive 5713 Qin Shi Huang's National Road System(次小生成树)
    UVALive 3661 Animal Run(最短路解最小割)
    卡尔曼滤波器
  • 原文地址:https://www.cnblogs.com/hellohell/p/9092776.html
Copyright © 2011-2022 走看看