首页 > 编程语言 >php适配器模式

php适配器模式

时间:2023-02-09 11:35:00浏览次数:40  
标签:function adapter 模式 mp4 file php type 适配器


阅读目录

  • ​​场景​​
  • ​​test.php​​
  • ​​AudioPlayer.php​​
  • ​​MediaInterface.php​​
  • ​​Adapter.php​​
  • ​​AdvanceMp4Player.php​​
  • ​​MediaAdvanceInterface.php​​
  • ​​AdvanceWmaPlayer.php​​
  • ​​知识点​​
  • ​​test.php 注册自加载打印​​

场景

应用 adapter 目录

php适配器模式_php

test.php

<?php
/**
* 结构型模式
*
* php适配器模式
* 把实现了不同接口的对象通过适配器的方式组合起来放在一个新的环境
*
* @author willem <https://wgchen.blog.csdn.net>
* @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();
}
playing file:忍者.mp3
AdvanceWmaPlayer driver playing file: 彩虹.wma
AdvanceMp4Player driver playing file: 龙卷风.mp4

AudioPlayer.php

<?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;
}

}
}

MediaInterface.php

<?php
namespace adapter;

/**
* 普通媒体接口
*/
interface MediaInterface
{
public function play($file='');
}

Adapter.php

<?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;
}
}
}

AdvanceMp4Player.php

<?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
}
}

MediaAdvanceInterface.php

<?php
namespace adapter;

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

AdvanceWmaPlayer.php

<?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";
}
}

知识点

test.php 注册自加载打印

// 注册自加载
spl_autoload_register('autoload');
function autoload($class)
{
$dir = dirname($_SERVER['SCRIPT_FILENAME']).'//..//'.str_replace('\\','/',$class).'.php';
print_r($dir);
require $dir;
}
.//..//adapter/AudioPlayer.php.//..//adapter/MediaInterface.phpplaying file:忍者.mp3
.//..//adapter/Adapter.php.//..//adapter/AdvanceWmaPlayer.php.//..//adapter/MediaAdvanceInterface.phpAdvanceWmaPlayer driver playing file: 彩虹.wma
.//..//adapter/AdvanceMp4Player.phpAdvanceMp4Player driver playing file: 龙卷风.mp4


标签:function,adapter,模式,mp4,file,php,type,适配器
From: https://blog.51cto.com/u_13571520/6046461

相关文章

  • PHP里的日期加减方法
    //第一步,假设有一个时间$a='2012-04-2510:10:00';//第二步,获得这个日期的时间戳$a_time=strtotime($a);//第三步,获得加五个月后的时间戳$b_time=strtotime('+5......
  • 获取PHP版本,获取Redis版本,获取MongoDB版本输出已安装扩展
    <?phperror_reporting(E_ALL);ini_set('display_errors',1);echo'<h1style="text-align:center;">欢迎使用DNMP!</h1>';echo'<h2>版本信息-----willem</h2>';echo'<ul>......
  • PHP注册模式
    阅读目录PHP注册模式PHP注册模式注册模式解决全局共享和交换对象,将已创建好的对象,挂在全局可以使用的数组上,在需要使用的时候,直接从该数组上获取该类的实现即可......
  • brew 安装 nginx 配合PHP工作
    1.前言环境介绍:macbookprom12020本文记录使用brew安装nginx配合PHP工作2.安装PHP#查看有哪些PHP版本可以安装brewsearchphp#安装php7.2brewinstall......
  • 使用iis设置网站php版本为7.3
    内容:使用iis设置网站php版本为7.3这张图是多少人的噩梦 早期的宝塔版本没办法在线升级,php版本只能到7.1 默认就没有7.2以上版本 怎么办?可以在iis设置第一步: ......
  • 以 standalone 模式启动 Aapche Pulsar
    以standalone模式启动AapchePulsarstandalone模式常用于开发测试阶段,请勿在生产环境使用。目录以standalone模式启动AapchePulsar常见问题内存小于4GB,导致无......
  • 7、install_mysql_httpd_php_wordpress
    #!/bin/bash##********************************************************************#Author: zikang#QQ: [email protected]#Date: 2021-03-03......
  • Kubernetes中PV&PVC访问模式
    问题为什么PV的accessmode设置的访问模式是ReadWriteOnce,但是其他节点还是可以读写。为什么PV的accessmode设置的模式为ReadOnlyMany,其他节点还是可以读写。前言这是明......
  • 正则表达式-(4)匹配模式
    所谓匹配模式,指的是正则中一些改变元字符匹配行为的方式,比如匹配时不区分英文字母大小写。常见的匹配模式有4种,分别是不区分大小写模式、点号通配模式、多行模式和注释模......
  • PHP , Navicat安装
    1、PHPStudy小皮面板:https://public.xp.cn/upgrades/phpStudy_64.zip    解压缩后       浏览器输入127.0.0.1:80  通过路径查看站点 ......