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 定义: 观察者模式 观察者模式 ...
随机推荐
- 我的Spring学习记录(二)
本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...
- CyclicBarrier的使用之王者荣耀打大龙
最近一直整并发这块东西,顺便写点Java并发的例子,给大家做个分享,也强化下自己记忆,如果有什么错误或者不当的地方,欢迎大家斧正. LOL和王者荣耀的玩家很多,许多人应该都有打大龙的经历,话说前期大家 ...
- 关于Android WebView上传文件的解决方案
我们在开发需求的时候,难免会接入一下第三方的H5页面,有些H5页面是具有上传照片的功能,Android 中的 WebView是不能直接打开文件选择弹框的 接下来我讲简单提供一下解决方案,先说一下思路 ...
- [解读REST] 4.基于网络应用的架构风格
上篇文章介绍了一组自洽的术语来描述和解释软件架构:如何利用架构属性评估一个架构风格:以及对于基于网络的应用架构来说,那些架构属性是值得我们重点关注评估的.本篇在以上的基础上,列举一下一些常见的(RES ...
- Https系列之二:https的SSL证书在服务器端的部署,基于tomcat,spring boot
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- NameError: name 'messagebox' is not defined 错误处理
写了段代码,想在按下button的时候跳个提示框出来,调试的时候提示了messagebox未定义 from tkinter import * def test_show(): messagebox.s ...
- APUE 4 - 线程
对传统的UNIX进程来讲,一个进程中只有一个线程,这就意味着一个进程在同一时刻只能做一件事(即使是多核CPU).使用多线程技术, 我们可以设计程序使得一个进程在同一时刻做多件事.使用多线程编程具有以下 ...
- hdu 4778 Gems Fight! 状态压缩DP
Gems Fight! Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)T ...
- PyCharm基本操作
1.1 PyCharm基本使用 视频学习连接地址:http://edu.51cto.com/course/9043.html 1.1.1 在Pycharm下为你的Python项目配置Python解释器 ...
- cnpm的全局安装
npm install -g cnpm --registry=https://registry.npm.taobao.org