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();
- ?>
四、对象适配器模式
以桌面环境转向移动环境为例:
- <?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设计模式四:适配器模式的更多相关文章
- 【白话设计模式四】单例模式(Singleton)
转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...
- C#设计模式(7)——适配器模式(Adapter Pattern)
一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...
- C#设计模式之七适配器模式(Adapter)【结构型】
一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题, ...
- C#设计模式之六适配器模式(Adapter Pattern)【结构型】
一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题,那[ ...
- C#设计模式(7)——适配器模式(Adapter Pattern)(转)
一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...
- 每天一个设计模式-3 适配器模式(Adapteer)
每天一个设计模式-3 适配器模式(Adapteer) 1.现实中的情况 旧式电脑的硬盘是串口的,直接与硬盘连接,新硬盘是并口的,显然新硬盘不能直接连在电脑上,于是就有了转接线.好了,今天的学习主题出来 ...
- Head First 设计模式之适配器模式与外观模式
Head First设计模式之适配器模式与外观模式 前言: 之前讲过装饰者模式,将对象包装起来并赋予新的职责,这一章我们也会将对象进行包装,只不过是让它们看起来不像自己而像是别的东西.这样就可以在设计 ...
- Java(Android)编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)
1.组合和继承之间的选择 组合和继承都允许在新的类中放置子对象,组合是显式的这样做,而继承则是隐式的做. 组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形.即在新类中嵌入某个对象,让其实 ...
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...
- 8.5 GOF设计模式四: 观察者模式Observer
GOF设计模式四: 观察者模式Observer 现实中遇到的问题 当有许多不同的客户都对同一数据源感兴趣,对相同的数据有不同的处理方式,该如 何解决?5.1 定义: 观察者模式 观察者模式 ...
随机推荐
- json中关于jo.[]中字符串一致的问题
procedure TForm1.btn1Click(Sender: TObject);var jo:ISuperObject; Temp:string; temp1:Boolean;begin j ...
- 二分求最长上升子序列 二分LIS
#include <iostream> #include <cstring> #define N 50010 using namespace std; int n; int n ...
- POJ-3045 Cow Acrobats (C++ 贪心)
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- 【轉】JS,Jquery获取各种屏幕的宽度和高度
Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...
- ng-options的使用
参考:官方文档.zhx1991 select 无默认选择一项 <select name="" id="" class="form-control ...
- 《MATLAB从入门到放弃》打通 “矩阵” 障碍
目录: » 矩阵的生成与大小 > 简单矩阵的生成 > 随机矩阵的生成 > 矩阵的大小 » 矩阵的索引与访问 » 矩阵的拼接与裁剪 > 矩阵的拼接 &g ...
- 修改NSMutableArray中的元素时的注意事项
最近做项目遇到从文件加载数组,并对数组中的元素进行操作的问题,特意写了个Demo,记录下要注意的东西: 代码如下: NSArray *array = @["]; NSMutableArray ...
- 修改Form ->Top和Left 造成的莫名其妙的显示异常 “轴信息”
相关代码: 运行程序: 要等待很久,或者把主窗体最小化,再最大化打开"轴信息" 才会恢复正常. 这个"不爽"很蛋蛋 ,网友亲亲们,有独到见解的亲亲们,期待得到你 ...
- Qt中的坐标系统
Qt使用统一的坐标系统来定位窗口部件的位置和大小. 以屏幕的左上角为原点即(0, 0)点,从左向右为x轴正向,从上向下为y轴正向,这整个屏幕的坐标系统就用来定位顶层窗口: 此外,窗口内部也有自己的坐标 ...
- Java并发/多线程系列——初识篇
回到过去,电脑有一个CPU,一次只能执行一个程序.后来多任务处理意味着计算机可以同时执行多个程序(AKA任务或进程).这不是真的"同时".单个CPU在程序之间共享.操作系统将在运行 ...