一、什么是适配器模式

  适配器模式有两种:类适配器模式和对象适配器模式。其中类适配器模式使用继承方式,而对象适配器模式使用组合方式。由于类适配器模式包含双重继承,而PHP并不支持双重继承,所以一般都采取结合继承和实现的方式来模拟双重继承,即继承一个类,同时实现一个接口。类适配器模式很简单,但是与对象适配器模式相比,类适配器模式的灵活性稍弱。采用类适配器模式时,适配器继承被适配者并实现一个接口;采用对象适配器模式时,适配器使用被适配者,并实现一个接口。

二、什么时候使用适配器模式

  适配器模式的作用就是解决兼容性问题,如果需要通过适配(使用多重继承或组合)来结合两个不兼容的系统,那就使用适配器模式。

三、类适配器模式

  以货币兑换为例:

  1. <?php
  2. /**
  3. * 类适配器模式
  4. * 以货币兑换为例
  5. **/
  6.  
  7. //美元计算类
  8. class DollarCalc
  9. {
  10. private $dollar;
  11. private $product;
  12. private $service;
  13. public $rate = 1;
  14.  
  15. public function requestCalc($product,$service)
  16. {
  17. $this->product = $product;
  18. $this->service = $service;
  19. $this->dollar = $this->product + $this->service;
  20. return $this->requestTotal();
  21. }
  22.  
  23. public function requestTotal()
  24. {
  25. $this->dollar *= $this->rate;
  26. return $this->dollar;
  27. }
  28. }
  29.  
  30. //欧元计算类
  31. class EuroCalc
  32. {
  33. private $euro;
  34. private $product;
  35. private $service;
  36. public $rate = 1;
  37.  
  38. public function requestCalc($product,$service)
  39. {
  40. $this->product = $product;
  41. $this->service = $service;
  42. $this->euro = $this->product + $this->service;
  43. return $this->requestTotal();
  44. }
  45.  
  46. public function requestTotal()
  47. {
  48. $this->euro *= $this->rate;
  49. return $this->euro;
  50. }
  51. }
  52.  
  53. //欧元适配器接口
  54. interface ITarget
  55. {
  56. function requester();
  57. }
  58.  
  59. //欧元适配器实现
  60. class EuroAdapter extends EuroCalc implements ITarget
  61. {
  62. public function __construct()
  63. {
  64. $this->requester();
  65. }
  66.  
  67. function requester()
  68. {
  69. $this->rate = .8111;
  70. return $this->rate;
  71. }
  72. }
  73.  
  74. //客户类
  75. class Client
  76. {
  77. private $euroRequest;
  78. private $dollarRequest;
  79.  
  80. public function __construct()
  81. {
  82. $this->euroRequest = new EuroAdapter();
  83. $this->dollarRequest = new DollarCalc();
  84. $euro = "€";
  85. echo "Euros: $euro" . $this->makeAdapterRequest($this->euroRequest) . "<br />";
  86. echo "Dollars: $" . $this->makeDollarRequest($this->dollarRequest);
  87. }
  88.  
  89. private function makeAdapterRequest(ITarget $req)
  90. {
  91. return $req->requestCalc(40,50);
  92. }
  93.  
  94. private function makeDollarRequest(DollarCalc $req)
  95. {
  96. return $req->requestCalc(40,50);
  97. }
  98. }
  99.  
  100. $client = new Client();
  101. ?>

  

四、对象适配器模式

  以桌面环境转向移动环境为例:

  1. <?php
  2. /**
  3. * 对象适配器模式
  4. * 从桌面环境转向移动环境
  5. **/
  6.  
  7. //桌面布局接口
  8. interface IFormat
  9. {
  10. public function formatCSS();
  11. public function formatGraphics();
  12. public function horizontalLayout();
  13. }
  14.  
  15. //桌面布局类实现
  16. class Desktop implements IFormat
  17. {
  18. public function formatCSS()
  19. {
  20. //调用桌面布局CSS文件
  21. }
  22.  
  23. public function formatGraphics()
  24. {
  25. //调用图片
  26. }
  27. public function horizontalLayout()
  28. {
  29. //桌面水平布局
  30. }
  31. }
  32.  
  33. //移动布局接口
  34. interface IMobileFormat
  35. {
  36. public function formatCSS();
  37. public function formatGraphics();
  38. public function verticalLayout();
  39. }
  40.  
  41. //移动布局类实现
  42. class Mobile implements IMobileFormat
  43. {
  44. public function formatCSS()
  45. {
  46. //调用移动布局CSS文件
  47. }
  48.  
  49. public function formatGraphics()
  50. {
  51. //调用图片
  52. }
  53.  
  54. public function verticalLayout()
  55. {
  56. //移动垂直布局
  57. }
  58. }
  59.  
  60. //移动布局适配器
  61. class MobileAdapter implements IFormat
  62. {
  63. private $mobile;
  64.  
  65. public function __construct(IMobileFormat $mobile)
  66. {
  67. $this->mobile = $mobile;
  68. }
  69.  
  70. public function formatCSS()
  71. {
  72. $this->mobile->formatCSS();
  73. }
  74.  
  75. public function formatGraphics()
  76. {
  77. $this->mobile->formatGraphics();
  78. }
  79.  
  80. public function horizontalLayout()
  81. {
  82. $this->mobile->verticalLayout();
  83. }
  84. }
  85.  
  86. //客户类
  87. class Client
  88. {
  89. private $mobile;
  90. private $mobileAdapter;
  91.  
  92. public function __construct()
  93. {
  94. $this->mobile = new Mobile();
  95. $this->mobileAdapter = new MobileAdapter($this->mobile);
  96. $this->mobileAdapter->formatCSS();
  97. $this->mobileAdapter->formatGraphics();
  98. $this->mobileAdapter->horizontalLayout();
  99. }
  100. }
  101.  
  102. $client = new Client();
  103. ?>

  

PHP设计模式四:适配器模式的更多相关文章

  1. 【白话设计模式四】单例模式(Singleton)

    转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...

  2. C#设计模式(7)——适配器模式(Adapter Pattern)

    一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...

  3. C#设计模式之七适配器模式(Adapter)【结构型】

    一.引言   从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题, ...

  4. C#设计模式之六适配器模式(Adapter Pattern)【结构型】

    一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题,那[ ...

  5. C#设计模式(7)——适配器模式(Adapter Pattern)(转)

    一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...

  6. 每天一个设计模式-3 适配器模式(Adapteer)

    每天一个设计模式-3 适配器模式(Adapteer) 1.现实中的情况 旧式电脑的硬盘是串口的,直接与硬盘连接,新硬盘是并口的,显然新硬盘不能直接连在电脑上,于是就有了转接线.好了,今天的学习主题出来 ...

  7. Head First 设计模式之适配器模式与外观模式

    Head First设计模式之适配器模式与外观模式 前言: 之前讲过装饰者模式,将对象包装起来并赋予新的职责,这一章我们也会将对象进行包装,只不过是让它们看起来不像自己而像是别的东西.这样就可以在设计 ...

  8. Java(Android)编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)

    1.组合和继承之间的选择 组合和继承都允许在新的类中放置子对象,组合是显式的这样做,而继承则是隐式的做. 组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形.即在新类中嵌入某个对象,让其实 ...

  9. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  10. 8.5 GOF设计模式四: 观察者模式Observer

    GOF设计模式四: 观察者模式Observer  现实中遇到的问题  当有许多不同的客户都对同一数据源感兴趣,对相同的数据有不同的处理方式,该如 何解决?5.1 定义: 观察者模式  观察者模式 ...

随机推荐

  1. json中关于jo.[]中字符串一致的问题

    procedure TForm1.btn1Click(Sender: TObject);var jo:ISuperObject; Temp:string; temp1:Boolean;begin  j ...

  2. 二分求最长上升子序列 二分LIS

    #include <iostream> #include <cstring> #define N 50010 using namespace std; int n; int n ...

  3. POJ-3045 Cow Acrobats (C++ 贪心)

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  4. 【轉】JS,Jquery获取各种屏幕的宽度和高度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  5. ng-options的使用

    参考:官方文档.zhx1991 select 无默认选择一项 <select name="" id="" class="form-control ...

  6. 《MATLAB从入门到放弃》打通 “矩阵” 障碍

    目录: »   矩阵的生成与大小  >   简单矩阵的生成  >  随机矩阵的生成  >   矩阵的大小 »  矩阵的索引与访问 »  矩阵的拼接与裁剪 >  矩阵的拼接 &g ...

  7. 修改NSMutableArray中的元素时的注意事项

    最近做项目遇到从文件加载数组,并对数组中的元素进行操作的问题,特意写了个Demo,记录下要注意的东西: 代码如下: NSArray *array = @["]; NSMutableArray ...

  8. 修改Form ->Top和Left 造成的莫名其妙的显示异常 “轴信息”

    相关代码: 运行程序: 要等待很久,或者把主窗体最小化,再最大化打开"轴信息" 才会恢复正常. 这个"不爽"很蛋蛋 ,网友亲亲们,有独到见解的亲亲们,期待得到你 ...

  9. Qt中的坐标系统

    Qt使用统一的坐标系统来定位窗口部件的位置和大小. 以屏幕的左上角为原点即(0, 0)点,从左向右为x轴正向,从上向下为y轴正向,这整个屏幕的坐标系统就用来定位顶层窗口: 此外,窗口内部也有自己的坐标 ...

  10. Java并发/多线程系列——初识篇

    回到过去,电脑有一个CPU,一次只能执行一个程序.后来多任务处理意味着计算机可以同时执行多个程序(AKA任务或进程).这不是真的"同时".单个CPU在程序之间共享.操作系统将在运行 ...