一、什么是适配器模式

  适配器模式有两种:类适配器模式和对象适配器模式。其中类适配器模式使用继承方式,而对象适配器模式使用组合方式。由于类适配器模式包含双重继承,而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设计模式四:适配器模式的更多相关文章

  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. Akka(25): Stream:对接外部系统-Integration

    在现实应用中akka-stream往往需要集成其它的外部系统形成完整的应用.这些外部系统可能是akka系列系统或者其它类型的系统.所以,akka-stream必须提供一些函数和方法来实现与各种不同类型 ...

  2. Safe Area Layout Guide

    原文:Safe Area Layout Guide Apple在iOS 7中引入了topLayoutGuide和bottomLayoutGuide作为UIViewController属性.它们允许您创 ...

  3. 翻译 | 一行 JavaScript 代码的逆向工程

    原文地址:Reverse Engineering One Line of JavaScript 原文作者:Alex Kras 译者:李波 校对者:冬青.小萝卜 几个月前,我看到一个邮件问:有没有人可以 ...

  4. 研磨SpringCloud系列(一)第一个Spring Boot应用

    在此之前,给大家推荐几个东西. STS,Spring官方基于eclipse做的扩展ide.Spring官方背书. 第二个,lombok,注解生成get/set,构造以及基本方法的插件,"隐藏 ...

  5. css常用属性1

    1  背景相关 背景颜色 background-color     = 颜色名称/rgb值/十六进制值 背景图片 background-image = url('') 背景图片平铺方式 backgro ...

  6. 15.linux-LCD层次分析(详解)

    如果我们的系统要用GUI(图形界面接口),这时LCD设备驱动程序就应该编写成frambuffer接口,而不是像之前那样只编写操作底层的LCD控制器接口. 什么是frambuffer设备? frambu ...

  7. 在JavaScript中使用json.js:Ajax项目之POST请求(异步)

    经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 ...

  8. poj2155一个二维树状数组

                                                                                                         ...

  9. [bzoj1059] [ZJOI2007] 矩阵游戏 (二分图匹配)

    小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏--矩阵游戏.矩阵游戏在一个N *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换操作:选 ...

  10. 关于el-dialog,我更推荐的用法

    最近的项目里用上了vue和element-ui.vue这种轻量级渐进式框架的舒适自不必说,但一直困扰着我的,是如何方便又优雅的弹出模态dialog... 对于我这种在jquery出现之前就用docum ...