php设计模式之适配器模式
2015-07-30 17:46
721 查看
适配器模式分为两种分别是类适配器和对象适配器
借鉴/article/7644633.html适用性:
你想使用一个已经存在的类,而它的接口不符合你的需求。你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类协同工作。
(仅适用于对象适配器)你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。即仅仅引入一个对象,并不需要额外的指针以间接取得adaptee。
原理:
将要用到的角色分为以下几种:target最早实现的接口,adaptee改善之后的接口,adapter对target和adapter进行适配,client调用接口类适配器:
<?php /** * 类适配器模式 * @author mtg * */ /** * 目标角色 * @version 1.0 */ class Target { /** * 这个方法将来有可能改进 */ public function hello(){ echo 'Hello '; } /** * 目标点 */ public function world(){ echo 'world'; } } /** * Client程序 调用target所定义的接口 * */ class Client { public static function main() { $Target = new Target(); $Target->hello(); $Target->world(); } } Client::main(); ?>
Target已经明确指出hello()方法会在未来的版本中改进,甚至不被支持或者淘汰。接下来,现在假设第二版的Target已经发布。一个全新的greet()方法代替了hello()。
<?php /** * 类适配器模式 * @author guisu * */ /** * 目标角色 * @version 2.0 */ class Target { /** * 这个方法将来有可能继续改进 */ public function greet(){ echo 'Greet '; } public function world(){ echo 'world'; } }
如果我们继续使用原来的client代码,肯定会报错,找不到hello方法。
针对API“升级”的解决办法就是创建一个适配器(Adapter)。
<?php /** * 类适配器模式 * @author mtg * */ /** * 目标角色 * @version 2.0 */ interface Target { /** *将原本的target类改造为一个接口 */ public function hello(); /** * 目标点 */ public function world(); } /** * 源角色:被适配的角色,新的接口类 */ class Adaptee { /** * 源类含有的方法 */ public function world() { echo ' world <br />'; } /** * 加入新的方法 */ public function greet() { echo ' Greet '; } } /** * 类适配器角色 即继承adaptee又实现target,现在的adapter有三个方法,分别是world(),greet(),和hello()方法,并且hello方法就是新增加的greet方法,此时,客户端代码是不需要改变的,只需将需要调用的类替换掉 */ class Adapter extends Adaptee implements Target { /** * 重写事项的接口中的hello方法 */ public function hello() { parent::greet(); } } /** * 客户端程序 * */ class Client { public static function main() { $adapter = new Adapter(); $adapter->hello(); $adapter->world(); } } Client::main(); ?>
对象适配器
<?php /** * 对象适配器模式 * @author guisu * */ /** * 目标角色 * @version 2.0 */ interface Target { /** * 源类的方法:这个方法将来有可能继续改进 */ public function hello(); /** * 目标点 */ public function world(); } /** * 源角色:被适配的角色 */ class Adaptee { /** * 源类含有的方法 */ public function world() { echo ' world <br />'; } /** * 加入新的方法 */ public function greet() { echo ' Greet '; } } /** * 在adapter中持有adptee的对象,此时adapter类就可以使用adaptee的接口,adapter类再通过接口转换,使客户端可以直接使用 */ class Adapter implements Target { private $_adaptee; public function __construct(Adaptee $adaptee) { $this->_adaptee = $adaptee; } /** * 通过接口转换,将greet()方法转换为hello() */ public function hello() { $this->_adaptee->greet(); } /** * 通过接口转换,将旧版本的world()方法转换为新版本的world的方法 */ public function world() { $this->_adaptee->world(); } } /** * 客户端程序 * */ class Client { /** * Main program. */ public static function main() { $adaptee = new Adaptee(); $adapter = new Adapter($adaptee); $adapter->hello(); $adapter->world(); } } Client::main(); ?>
相关文章推荐
- log4php安装
- PHP的元语言编程
- ThinkPHP I方法
- PHP: chr和pack、unpack那些事
- Laravel中URL,ACTION,ROUTE区别
- PHP: 深入pack/unpack
- PHP中使用cURL实现Get和Post请求的方法
- 什么是TPS,什么是QPS,区别是什么?
- PHP中CURL方法curl_setopt()函数的一些参数
- phpstorm+Xdebug断点调试PHP
- php 常用代码段
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第55讲:PHP smarty模板自定义函数
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第53讲:PHP smarty模板配置及变量操作
- 利用PHP将部分内容用星号替换
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第52讲:PHP仿smarty模板的实现
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第51讲:PHP中smarty模板技术介绍
- ThinkPHP3.2.2调试模式下正常访问,关闭调试模式,部署时出现删出缓存第一次没错,第二次开始错误。
- FastCgi与PHP-fpm之间是个什么样的关系
- php异常、错误处理机制