摘要:简介适配器模式有时候也称包装样式或者包装将一个类的接口适配成用户所期待的。应用场景如程序数据库有关联等操作,而你需要根据情况换数据库操作时,可以使用适配器模式统一接口,这样代码中除了数据库配置之外,就不需要做而外的更改。
简介
适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起。
UML 角色Target适配目标 : 该角色定义把其他类转换为何种接口,也就是我们的期望接口。
Adaptee被适配者 :就是需要被适配的接口。
Adapter适配器:其他的两个角色都是已经存在的角色,而适配器角色是需要新建立的,它用来对Adaptee与Target接口进行适配。
应用场景如程序数据库有关联mysql、mysqli、pdo、sqlite、postgresql等操作,而你需要根据情况换数据库操作时,可以使用适配器模式统一接口,这样代码中除了数据库配置之外,就不需要做而外的更改。
同理cache(缓存)的场景也是,无论使用memcache还是redis等,在更换的时候都会很方便,节约时间。
注:在一些流行框架中都可以看到此模式,详情请自行参阅框架源码。
实现代码:
</>复制代码
connect = $connect;
//其他操作
}
/**
* 实现查询方法
*
* @param $sql 需要被查询的sql语句
* @return mi
*/
public function query($sql)
{
return mysql_query($sql);
}
// 实现关闭方法
public function close()
{
mysql_close();
}
}
/**
* Class Mysql 适配器
*/
class Mysql_i implements IDatabase
{
protected $connect; // 连接资源
/**
* 实现连接方法
*
* @param $host host
* @param $username 用户名
* @param $password 密码
* @param $database 数据库名
*/
public function connect($host, $username, $password, $database)
{
$connect = mysqli_connect($host, $username, $password, $database);
$this->connect = $connect;
//其他操作
}
/**
* 实现查询方法
*
* @param $sql 需要被查询的sql语句
*/
public function query($sql)
{
return mysqli_query($this->connect, $sql);
}
// 实现关闭
public function close()
{
mysqli_close($this->connect);
}
}
/**
* Class Postgresql 适配器
*/
class Postgresql implements IDatabase
{
protected $connect; // 连接资源
/**
* 实现连接方法
*
* @param $host
* @param $username
* @param $password
* @param $database
*/
public function connect($host, $username, $password, $database)
{
$this->connect = pg_connect("host=$host dbname=$database user=$username password=$password");
//其他操作
}
/**
* 实现查询方法
*
* @param $sql 需要被查询的sql语句
*/
public function query($sql)
{
// 其他操作
}
// 实现关闭方法
public function close()
{
}
}
/**
* 客户端使用演示
* 这里以mysql为例
* 只要模式设计好,不论有多少种数据库,实现和调用方式都是一样的
* 因为都是实现的同一个接口,所以都是可以随意切换的
*/
$host = "localhost";
$username = "root";
$password = "root";
$database = "mysql";
//$client = new Postgresql();
//$client = new Mysql();
$client = new Mysql_i();
$client->connect($host, $username, $password, $database);
$result = $client->query("select * from db");
while ($rows = mysqli_fetch_array($result)) {
var_dump($rows);
}
$client->close();
运行结果:
</>复制代码
array(44) {
[0]=>
string(1) "%"
["Host"]=>
string(1) "%"
[1]=>
string(4) "test"
["Db"]=>
string(4) "test"
[2]=>
string(0) ""
["User"]=>
string(0) ""
[3]=>
string(1) "Y"
["Select_priv"]=>
string(1) "Y"
[4]=>
string(1) "Y"
["Insert_priv"]=>
string(1) "Y"
[5]=>
string(1) "Y"
["Update_priv"]=>
string(1) "Y"
[6]=>
string(1) "Y"
["Delete_priv"]=>
string(1) "Y"
[7]=>
string(1) "Y"
["Create_priv"]=>
string(1) "Y"
[8]=>
string(1) "Y"
["Drop_priv"]=>
string(1) "Y"
[9]=>
string(1) "N"
["Grant_priv"]=>
string(1) "N"
[10]=>
string(1) "Y"
["References_priv"]=>
string(1) "Y"
[11]=>
string(1) "Y"
["Index_priv"]=>
string(1) "Y"
[12]=>
string(1) "Y"
["Alter_priv"]=>
string(1) "Y"
[13]=>
string(1) "Y"
["Create_tmp_table_priv"]=>
string(1) "Y"
[14]=>
string(1) "Y"
["Lock_tables_priv"]=>
string(1) "Y"
[15]=>
string(1) "Y"
["Create_view_priv"]=>
string(1) "Y"
[16]=>
string(1) "Y"
["Show_view_priv"]=>
string(1) "Y"
[17]=>
string(1) "Y"
["Create_routine_priv"]=>
string(1) "Y"
[18]=>
string(1) "N"
["Alter_routine_priv"]=>
string(1) "N"
[19]=>
string(1) "N"
["Execute_priv"]=>
string(1) "N"
[20]=>
string(1) "Y"
["Event_priv"]=>
string(1) "Y"
[21]=>
string(1) "Y"
["Trigger_priv"]=>
string(1) "Y"
}
从以上结果可看出,数据库连接以及查询语句都已经执行成功。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/22059.html
摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...
摘要:适配器模式只要应用于希望复用一些现存的类,但接口又与复用环境要求不一致的情况这是一种亡羊补牢的方法。首选的方法应该是重构代码,统一接口。 定义 将某个对象的接口适配为另一个对象所期望的接口,adapter模式使原本由于接口不兼容而不能一起工作的类可以一起工作 使用场景 需要的东西在面前,但却不能用,而短时间又无法改造它,于是就想办法适配 系统的数据和行为都正确,但接口不符时,应该考虑...
摘要:序列文章面试之函数面试之对象面试之数组的几个不操作面试之对比分析面试之数据结构与算法前言设计模式如果应用到项目中,可以实现代码的复用和解耦,提高代码质量。 showImg(https://segmentfault.com/img/bVbq2VA?w=480&h=260); 序列文章 JS面试之函数(1)JS面试之对象(2)JS面试之数组的几个不low操作(3)JS面试之http0.9~...
阅读 1117·2021-11-22 14:56
阅读 1191·2021-11-11 16:54
阅读 8620·2021-09-23 11:55
阅读 3117·2021-09-22 15:57
阅读 2872·2021-08-27 16:25
阅读 757·2019-08-30 15:55
阅读 1722·2019-08-30 15:43
阅读 1674·2019-08-30 14:23