PHP设计模式之适配器模式原理与用法分析

yipeiwu_com6年前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) . "<br />";
    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相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php 如何设置一个严格控制过期时间的session

1.php session 有效期 PHP的session有效期默认是1440秒(24分钟),如果客户端超过24分钟没有刷新,当前session会被回收,失效。 当用户关闭浏览器,会话结...

完美解决:Apache启动问题—(OS 10022)提供了一个无效的参数

今天启动apache2始终无法启动,本以为又是端口问题,可后来查看并未有端口占用问题,于是查看错误日志如下:[Sat Jun 20 03:12:52 2009] [notice] Apa...

Apache连接PHP后无法启动问题解决思路

问题:apache之前正常,连接配置完PHP后无法启动,用apache Test Configration测试后报错形式为: Cannot load D:/php/php5apache2...

php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释

复制代码 代码如下:function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset...

Thinkphp结合AJAX长轮询实现PC与APP推送详解

前言 本文主要给大家介绍的关于Thinkphp结合AJAX长轮询实现PC与APP推送的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍。 实现逻辑 某个操作(比如新建一条...