PHP适配器模式概述
本例详细解析了PHP中适配器模式的核心原理及其实际应用场景,旨在为开发者提供有价值的参考。
适配器模式的定义与分类
一、什么是适配器模式
适配器模式主要包含类适配器与对象适配器两种形式。前者依赖继承机制,后者依赖组合机制。鉴于PHP语言本身不支持多重继承,若需采用类适配器,通常需结合继承单个类与实现特定接口的方式来模拟双重继承的效果。尽管类适配器结构直观,但其灵活性不及对象适配器。在类适配器中,适配器类直接继承被适配者并实现目标接口;而在对象适配器中,适配器内部持有被适配者的实例(组合),同时实现目标接口。
适配器模式的使用场景
二、什么时候使用适配器模式
该模式的核心价值在于处理系统间的兼容性冲突。当需要整合两个原本不兼容的系统,且必须通过适配手段(如多重继承或对象组合)来实现互通时,应选用适配器模式。
类适配器模式实例分析
三、类适配器模式
以下以货币兑换场景为例进行说明:
<?php
/**
* 类适配器模式
* 以货币兑换为例
**/
//美元计算类
class DollarCalc
{
private $dollar;
private $product;
private $service;
public $rate = 1;
public function requestCalc($product,$service)
{
$this->product = $product;
$this->service = $service;
$this->dollar = $this->product + $this->service;
return $this->requestTotal();
}
public function requestTotal()
{
$this->dollar *= $this->rate;
return $this->dollar;
}
}
//欧元计算类
class EuroCalc
{
private $euro;
private $product;
private $service;
public $rate = 1;
public function requestCalc($product,$service)
{
$this->product = $product;
$this->service = $service;
$this->euro = $this->product + $this->service;
return $this->requestTotal();
}
public function requestTotal()
{
$this->euro *= $this->rate;
return $this->euro;
}
}
//欧元适配器接口
interface ITarget
{
function requester();
}
//欧元适配器实现
class EuroAdapter extends EuroCalc implements ITarget
{
public function __construct()
{
$this->requester();
}
function requester()
{
$this->rate = .8111;
return $this->rate;
}
}
//客户类
class Client
{
private $euroRequest;
private $dollarRequest;
public function __construct()
{
$this->euroRequest = new EuroAdapter();
$this->dollarRequest = new DollarCalc();
$euro = "€";
echo "Euros: $euro" . $this->makeAdapterRequest($this->euroRequest) . "
";
echo "Dollars: $" . $this->makeDollarRequest($this->dollarRequest);
}
private function makeAdapterRequest(ITarget $req)
{
return $req->requestCalc(40,50);
}
private function makeDollarRequest(DollarCalc $req)
{
return $req->requestCalc(40,50);
}
}
$client = new Client();
?>
运行结果:
Euros: €72.999 Dollars: $90
对象适配器模式实例分析
四、对象适配器模式
以下以从桌面环境迁移至移动环境的场景为例进行说明:
<?php
/**
* 对象适配器模式
* 从桌面环境转向移动环境
**/
//桌面布局接口
interface IFormat
{
public function formatCSS();
public function formatGraphics();
public function horizontalLayout();
}
//桌面布局类实现
class Desktop implements IFormat
{
public function formatCSS()
{
//调用桌面布局CSS文件
}
public function formatGraphics()
{
//调用图片
}
public function horizontalLayout()
{
//桌面水平布局
}
}
//移动布局接口
interface IMobileFormat
{
public function formatCSS();
public function formatGraphics();
public function verticalLayout();
}
//移动布局类实现
class Mobile implements IMobileFormat
{
public function formatCSS()
{
//调用移动布局CSS文件
}
public function formatGraphics()
{
//调用图片
}
public function verticalLayout()
{
//移动垂直布局
}
}
//移动布局适配器
class MobileAdapter implements IFormat
{
private $mobile;
public function __construct(IMobileFormat $mobile)
{
$this->mobile = $mobile;
}
public function formatCSS()
{
$this->mobile->formatCSS();
}
public function formatGraphics()
{
$this->mobile->formatGraphics();
}
public function horizontalLayout()
{
$this->mobile->verticalLayout();
}
}
//客户类
class Client
{
private $mobile;
private $mobileAdapter;
public function __construct()
{
$this->mobile = new Mobile();
$this->mobileAdapter = new MobileAdapter($this->mobile);
$this->mobileAdapter->formatCSS();
$this->mobileAdapter->formatGraphics();
$this->mobileAdapter->horizontalLayout();
}
}
$client = new Client();
?>
希望上述内容能对各位的PHP编程实践有所助益。