当前位置:首页 > PHP教程 > php高级应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-15 10:16:05 浏览: 评论:0 

这篇文章主要介绍了PHP设计模式之适配器模式,简单描述了适配器模式的概念、原理并结合实例形式分析了php类适配器模式与对象适配器模式的具体定义与使用方法,需要的朋友可以参考下。

本文实例讲述了PHP设计模式之适配器模式原理与用法,分享给大家供大家参考,具体如下:

一、什么是适配器模式

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

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

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

三、类适配器模式

以货币兑换为例:

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

运行结果:

Euros: €72.999

Dollars: $90

四、对象适配器模式

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

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

Tags: PHP设计模式 PHP适配器模式

分享到: