将一个类的接口转换为用户期望的另外一个接口.适配器使得原本由于接口不兼容而不能一起工作的类可以一起工作  

UML:

一.类适配器:

class A
{
public function methodA()
{
..............
}
} class Adapter extends A
{
private $objectA; public function methodB()
{
echo 'Append data';
}
} 调用: $a = new Adapter();
$a->methodA();
$a->methodB();

  

二.对象适配器:

const HOST = '127.0.0.1';
const USER = 'root';
const PASSWORD = 'root';
const DB_NAME = 'test'; interface Db
{
public function connect();
public function select($sql);
} class MysqlDb implements Db
{
protected $conn; public function connect()
{
$this->conn = mysql_connect(HOST, USER, PASSWORD);
mysql_select_db(DB_NAME, $this->conn);
} public function select($sql)
{
$res = mysql_query($sql, $this->conn);
$data = array();
while ($row = mysql_fetch_assoc($res)) {
$data[] = $row;
}
return $data;
}
} class MysqliDb implements Db
{
protected $conn; public function connect()
{
$this->conn = mysqli_connect(HOST, USER, PASSWORD);
mysqli_select_db($this->conn, DB_NAME);
} public function select($sql)
{
$res = mysqli_query($this->conn, $sql);
$data = array();
while ($row = mysqli_fetch_assoc($res)) {
$data[] = $row;
}
return $data;
}
} class DbAdapter
{
const MYSQL = 'MysqlDb';
const MYSQLI = 'MysqliDb';
protected $db; public function __construct($type)
{
$this->db = new $type();
} public function connectDb()
{
$this->db->connect();
} public function select($sql)
{
return $this->db->select($sql);
}
} $db = new DbAdapter(DbAdapter::MYSQLI);
$db->connectDb();
var_dump($db->select("select * from test"));

  

三.接口适配器

要求现有类ExistClass适配接口DemoInterface

现有类:

class ExistClass
{
} interface DemoInterface
{
public function method();
} // 适配的新类
class NewClass extends ExistClass implements DemoInterface
{
public function method()
{
// TODO: Implement method() method.
} }

  

S1:适配器 Adapter的更多相关文章

  1. 设计模式--适配器(Adapter)模式

    今天学习另一个设计模式,适配器(Adapter)模式,这是一个共同方向,但有特殊要求,就应用到此设计模式.写到这里,想起很久以前,有写过一篇<ASP.NET的适配器设计模式(Adapter)&g ...

  2. 【原】模式之-适配器Adapter模式

    适配器Adapter模式 适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 模式所涉及的角色有 ...

  3. Ruby设计模式透析之 —— 适配器(Adapter)

    转载请注明出处:http://blog.csdn.net/sinyu890807/article/details/9400153 此为Java设计模式透析的拷贝版,专门为Ruby爱好者提供的,不熟悉R ...

  4. 理解什么是适配器(adapter)和接口(interface)

    ● 适配器(adapter) In computing, adapter is a hardware device or software component that converts transm ...

  5. 设计模式学习心得<适配器 Adapter>

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接 ...

  6. 安卓开发笔记——打造万能适配器(Adapter)

    为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...

  7. 适配器(Adapter)模式

    适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的一些其他名称:变压器模式.转换器模式.包装(Wrapper)模式.适 ...

  8. 如何实现 axios 的自定义适配器 adapter

    Axios 是一个非常优秀的基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.并且提供了很多便捷的功能,例如: 支持 Promise API 拦截请求和响应 转换请求数据和 ...

  9. java演示适配器(adapter)模式

    为什么要使用模式: 模式是一种做事的一种方法,也即实现某个目标的途径,或者技术. adapter模式的宗旨就是,保留现有类所提供的服务,向客户提供接口,以满足客户的需求. 类适配器:客户端定义了接口并 ...

随机推荐

  1. WCF回调操作

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.s ...

  2. 一种提高Android应用进程存活率新方法

    一.基础知识 1.Android 进程优先级 1.1 进程优先级等级一般分法:- Activte process- Visible Process- Service process- Backgrou ...

  3. symfony3常用记忆

    1.控制器里获取当前用户信息 $user = $this->getUser(); 2.判断当前用户是否登录 // yay! Use this to see if the user is logg ...

  4. [ CodeVS冲杯之路 ] P1501

     不充钱,你怎么AC? 题目:http://codevs.cn/problem/1501/ 水题一道 直接dfs,记录上当前深度,到了叶子节点就更新答案,并且每个节点将当前深度的计数+1,答案即为ma ...

  5. 货车运输(LCA+最大生成树)

    神奇传送门 恩,这是一道神奇的LCA+难度的题目. 题目是这样的: A 国有n座城市,编号从1到n,城市之间有 m条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q辆货车在运输货物,司机们 ...

  6. Error C1189: #error: Please use the /MD switch for _AFXDLL builds(转)

    原文转自 https://www.cnblogs.com/zwh0214/p/6048360.html 在VS 2013中编译程序时出现错误: 错误提示1: error C1189: #error : ...

  7. 实现自己的系统调用针对linux-2.6.34【转】

    转自:http://biancheng.dnbcw.net/linux/303362.html 在linux下实现自己的系统调用.主要功能是:遍历系统的进程,并将相关的进程信息存放在自己定义的结构体中 ...

  8. windwos grpc 编译

    此文档是windwos grpc c++ 编译 ,基于 vs2015 编译完成 获取gRPC源码 gRPC是开源框架,项目代码在github上,所以首先要安装github.github安装后,在指定文 ...

  9. vue.js基本使用

    #原创,转载请留言联系 什么是vue.js Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架. Vue 只关注视图层, 采用自底向上增量开发的设计. Vue 的目 ...

  10. hdu 5166(水题)

    Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...