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

 更新时间:2018年04月25日 11:15:29   作者:编程人,在天涯  
这篇文章主要介绍了PHP设计模式之适配器模式,简单描述了适配器模式的概念、原理并结合实例形式分析了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 500报错的快速解决方法

    PHP 500报错的快速解决方法

    下面小编就为大家带来一篇PHP 500报错的快速解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C# WinForm中实现快捷键自定义设置实例

    C# WinForm中实现快捷键自定义设置实例

    这篇文章主要介绍了对C# WinForm中实现快捷键自定义设置实例,本文实现了大多数软件如QQ、有道词典中的快捷键设置功能,即按下相应按键后显示在文本框中,需要的朋友可以参考下
    2015-01-01
  • PHP使用适合阅读的格式显示文件大小的方法

    PHP使用适合阅读的格式显示文件大小的方法

    这篇文章主要介绍了PHP使用适合阅读的格式显示文件大小的方法,实例分析了php实现数据转换的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • php使用date和strtotime函数输出指定日期的方法

    php使用date和strtotime函数输出指定日期的方法

    这篇文章主要介绍了php使用date和strtotime函数输出指定日期的方法,实例汇总了生成各种日期格式的方法,非常具有实用价值,需要的朋友可以参考下
    2014-11-11
  • PHP正则删除HTML代码中宽高样式的方法

    PHP正则删除HTML代码中宽高样式的方法

    这篇文章主要介绍了PHP正则删除HTML代码中宽高样式的方法,涉及php针对HTML代码的正则匹配、替换等操作技巧,需要的朋友可以参考下
    2017-06-06
  • 如何用php获取程序执行的时间

    如何用php获取程序执行的时间

    本篇文章是对使用php获取程序执行的时间进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php数组合并array_merge()函数使用注意事项

    php数组合并array_merge()函数使用注意事项

    array_merge()函数在php中是对数组进行合并的,可以把多个数组合成一个数组,并且不改变原数组(www.111cn.net)的值了,但今天我在使用array_merge合并数组时碰到几个小细节上的问题,下面我举例子给各位朋友看看
    2014-06-06
  • wamp服务器访问php非常缓慢的解决过程

    wamp服务器访问php非常缓慢的解决过程

    这篇文章主要介绍了wamp服务器访问php非常缓慢的解决过程,十分的简单实用,有需要的小伙伴可以参考下。
    2015-07-07
  • 使用php自动备份数据库表的实现方法

    使用php自动备份数据库表的实现方法

    下面小编就为大家带来一篇使用php自动备份数据库表的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • PHP排序算法类实例

    PHP排序算法类实例

    这篇文章主要介绍了PHP排序算法类,实例分析了插入排序、选择排序、冒泡排序、快速排序等排序算法的原理与实现技巧,需要的朋友可以参考下
    2015-06-06

最新评论