php设计模式之五__适配器模式
2016-12-29 19:15
477 查看
适配器模式
1.定义:
将一个类的接口转换成客户希望的另外一个接口,使得原来由于接口不兼容而不能一起工作的类,可以一起工作了。通常分为类适配器和对象适配器两种。
2.UML图
![](http://img.blog.csdn.net/20161229182511829?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDI1MDg2Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3.代码示例:
<?php
//适配器模式(对象适配【组合关系】)
//通用接口
interface Target{
public function request();//普通请求的接口
}
//适配器
class Adapter implements Target{
private $_adaptee;
public function __construct($adaptee){
$this->_adaptee = $adaptee;
}
public function request(){
$this->_adaptee->specialRequest();
}
}
//适配者
class Adaptee{
public function specialRequest(){
echo "特殊的请求";
}
}
//test case
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
$adapter->request();
4.适用情况:
针对不同数据库(mysql,oracle)的适配,不同缓存系统(memcache,redis)等。
1.定义:
将一个类的接口转换成客户希望的另外一个接口,使得原来由于接口不兼容而不能一起工作的类,可以一起工作了。通常分为类适配器和对象适配器两种。
2.UML图
3.代码示例:
<?php
//适配器模式(对象适配【组合关系】)
//通用接口
interface Target{
public function request();//普通请求的接口
}
//适配器
class Adapter implements Target{
private $_adaptee;
public function __construct($adaptee){
$this->_adaptee = $adaptee;
}
public function request(){
$this->_adaptee->specialRequest();
}
}
//适配者
class Adaptee{
public function specialRequest(){
echo "特殊的请求";
}
}
//test case
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
$adapter->request();
4.适用情况:
针对不同数据库(mysql,oracle)的适配,不同缓存系统(memcache,redis)等。
相关文章推荐
- PHP设计模式之适配器模式,建造者模式,数据访问对象模式
- PHP设计模式--适配器模式
- PHP 设计模式 笔记与总结(7)适配器模式
- PHP设计模式之适配器模式
- PHP设计模式之适配器模式代码实例
- PHP设计模式之适配器模式,建造者模式,数据访问对象模式
- php实现设计模式之 适配器模式
- PHP设计模式——适配器模式
- php中的设计模式之--适配器模式
- 设计模式之适配器模式(PHP实现)
- PHP之设计模式—适配器模式
- PHP设计模式之适配器模式 推荐
- PHP设计模式-适配器模式
- PHP设计模式——适配器模式
- php设计模式 Adapter(适配器模式)
- PHP设计模式——适配器模式(研磨设计模式学习笔记)
- PHP设计模式笔记:使用PHP实现适配器模式
- PHP设计模式-适配器模式
- PHP设计模式之适配器模式代码实例
- PHP设计模式之适配器模式