php适配器模式

把实现了不同接口的对象通过适配器的方式组合起来放在一个新的环境

<?php
namespace adapter;

/**
 * 高级媒体接口
 */
interface MediaAdvanceInterface
{
  public function playMp4($file='');
  public function playWma($file='');
}
<?php
namespace adapter;

/**
 * mp4高级播放器实体
 */
class AdvanceMp4Player implements MediaAdvanceInterface
{
  public function playMp4($file='')
  {
      echo 'AdvanceMp4Player driver playing file: ' . $file . ".mp4\n";
  }

  public function playWma($file='')
  {
      //do nothing
  }
}
<?php
namespace adapter;

/**
 * wma高级播放器实体
 */
class AdvanceWmaPlayer implements MediaAdvanceInterface
{
  public function playMp4($file='')
  {
      //do nothing
  }

  public function playWma($file='')
  {
      echo 'AdvanceWmaPlayer driver playing file: ' . $file . ".wma\n";
  }
}
<?php
namespace adapter;

use Exception;

/**
 * 高级播放器适配器
 */
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;
    }
  }
}
<?php
namespace adapter;

/**
 * 普通媒体接口
 */

interface MediaInterface
{
  public function play($file='');
}
<?php
namespace adapter;

use Exception;

/**
 * 音频设备实体
 */
class AudioPlayer implements MediaInterface
{
  public function play($file='', $type='')
  {
    switch ($type) {
      case 'mp3':
        echo 'playing file: ' . $file . ".mp3\n";
        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;
    }

  }
}
<?php
/**
 * 结构型模式
 *
 * php适配器模式
 * 把实现了不同接口的对象通过适配器的方式组合起来放在一个新的环境
 *
 *   
 * @example 运行 php test.php
 */


// 注册自加载
spl_autoload_register('autoload');

function autoload($class)
{
  require dirname($_SERVER['SCRIPT_FILENAME']) . '//..//' . str_replace('\\', '/', $class) . '.php';
}

/************************************* test *************************************/

use adapter\AudioPlayer;

try {
  //生产一台设备
  $mp4 = new AudioPlayer();
  // 播放一个mp3
  $mp4->play('忍者', 'mp3');
  // 播放一个wma
  $mp4->play('彩虹', 'wma');
  // 播放一个mp4
  $mp4->play('龙卷风mv', 'mp4');
} catch (\Exception $e) {
  echo $e->getMessage();
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩淼燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值