PHP设计模式四:适配器模式
一、什么是适配器模式
适配器模式有两种:类适配器模式和对象适配器模式。其中类适配器模式使用继承方式,而对象适配器模式使用组合方式。由于类适配器模式包含双重继承,而PHP并不支持双重继承,所以一般都采取结合继承和实现的方式来模拟双重继承,即继承一个类,同时实现一个接口。类适配器模式很简单,但是与对象适配器模式相比,类适配器模式的灵活性稍弱。采用类适配器模式时,适配器继承被适配者并实现一个接口;采用对象适配器模式时,适配器使用被适配者,并实现一个接口。
二、什么时候使用适配器模式
适配器模式的作用就是解决兼容性问题,如果需要通过适配(使用多重继承或组合)来结合两个不兼容的系统,那就使用适配器模式。
三、类适配器模式
以货币兑换为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | <?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(); ?> |
四、对象适配器模式
以桌面环境转向移动环境为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | <?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(); ?> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)