资讯专栏INFORMATION COLUMN

PHP设计模式之适配器模式

netScorpion / 2390人阅读

摘要:简介适配器模式有时候也称包装样式或者包装将一个类的接口适配成用户所期待的。应用场景如程序数据库有关联等操作,而你需要根据情况换数据库操作时,可以使用适配器模式统一接口,这样代码中除了数据库配置之外,就不需要做而外的更改。

简介

适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起。

UML 角色

Target适配目标 : 该角色定义把其他类转换为何种接口,也就是我们的期望接口。

Adaptee被适配者 :就是需要被适配的接口。

Adapter适配器:其他的两个角色都是已经存在的角色,而适配器角色是需要新建立的,它用来对Adaptee与Target接口进行适配。

应用场景

如程序数据库有关联mysql、mysqli、pdo、sqlite、postgresql等操作,而你需要根据情况换数据库操作时,可以使用适配器模式统一接口,这样代码中除了数据库配置之外,就不需要做而外的更改。

同理cache(缓存)的场景也是,无论使用memcache还是redis等,在更换的时候都会很方便,节约时间。

:在一些流行框架中都可以看到此模式,详情请自行参阅框架源码。

实现

代码:

</>复制代码

  1. connect = $connect;
  2. //其他操作
  3. }
  4. /**
  5. * 实现查询方法
  6. *
  7. * @param $sql 需要被查询的sql语句
  8. * @return mi
  9. */
  10. public function query($sql)
  11. {
  12. return mysql_query($sql);
  13. }
  14. // 实现关闭方法
  15. public function close()
  16. {
  17. mysql_close();
  18. }
  19. }
  20. /**
  21. * Class Mysql 适配器
  22. */
  23. class Mysql_i implements IDatabase
  24. {
  25. protected $connect; // 连接资源
  26. /**
  27. * 实现连接方法
  28. *
  29. * @param $host host
  30. * @param $username 用户名
  31. * @param $password 密码
  32. * @param $database 数据库名
  33. */
  34. public function connect($host, $username, $password, $database)
  35. {
  36. $connect = mysqli_connect($host, $username, $password, $database);
  37. $this->connect = $connect;
  38. //其他操作
  39. }
  40. /**
  41. * 实现查询方法
  42. *
  43. * @param $sql 需要被查询的sql语句
  44. */
  45. public function query($sql)
  46. {
  47. return mysqli_query($this->connect, $sql);
  48. }
  49. // 实现关闭
  50. public function close()
  51. {
  52. mysqli_close($this->connect);
  53. }
  54. }
  55. /**
  56. * Class Postgresql 适配器
  57. */
  58. class Postgresql implements IDatabase
  59. {
  60. protected $connect; // 连接资源
  61. /**
  62. * 实现连接方法
  63. *
  64. * @param $host
  65. * @param $username
  66. * @param $password
  67. * @param $database
  68. */
  69. public function connect($host, $username, $password, $database)
  70. {
  71. $this->connect = pg_connect("host=$host dbname=$database user=$username password=$password");
  72. //其他操作
  73. }
  74. /**
  75. * 实现查询方法
  76. *
  77. * @param $sql 需要被查询的sql语句
  78. */
  79. public function query($sql)
  80. {
  81. // 其他操作
  82. }
  83. // 实现关闭方法
  84. public function close()
  85. {
  86. }
  87. }
  88. /**
  89. * 客户端使用演示
  90. * 这里以mysql为例
  91. * 只要模式设计好,不论有多少种数据库,实现和调用方式都是一样的
  92. * 因为都是实现的同一个接口,所以都是可以随意切换的
  93. */
  94. $host = "localhost";
  95. $username = "root";
  96. $password = "root";
  97. $database = "mysql";
  98. //$client = new Postgresql();
  99. //$client = new Mysql();
  100. $client = new Mysql_i();
  101. $client->connect($host, $username, $password, $database);
  102. $result = $client->query("select * from db");
  103. while ($rows = mysqli_fetch_array($result)) {
  104. var_dump($rows);
  105. }
  106. $client->close();

运行结果:

</>复制代码

  1. array(44) {
  2. [0]=>
  3. string(1) "%"
  4. ["Host"]=>
  5. string(1) "%"
  6. [1]=>
  7. string(4) "test"
  8. ["Db"]=>
  9. string(4) "test"
  10. [2]=>
  11. string(0) ""
  12. ["User"]=>
  13. string(0) ""
  14. [3]=>
  15. string(1) "Y"
  16. ["Select_priv"]=>
  17. string(1) "Y"
  18. [4]=>
  19. string(1) "Y"
  20. ["Insert_priv"]=>
  21. string(1) "Y"
  22. [5]=>
  23. string(1) "Y"
  24. ["Update_priv"]=>
  25. string(1) "Y"
  26. [6]=>
  27. string(1) "Y"
  28. ["Delete_priv"]=>
  29. string(1) "Y"
  30. [7]=>
  31. string(1) "Y"
  32. ["Create_priv"]=>
  33. string(1) "Y"
  34. [8]=>
  35. string(1) "Y"
  36. ["Drop_priv"]=>
  37. string(1) "Y"
  38. [9]=>
  39. string(1) "N"
  40. ["Grant_priv"]=>
  41. string(1) "N"
  42. [10]=>
  43. string(1) "Y"
  44. ["References_priv"]=>
  45. string(1) "Y"
  46. [11]=>
  47. string(1) "Y"
  48. ["Index_priv"]=>
  49. string(1) "Y"
  50. [12]=>
  51. string(1) "Y"
  52. ["Alter_priv"]=>
  53. string(1) "Y"
  54. [13]=>
  55. string(1) "Y"
  56. ["Create_tmp_table_priv"]=>
  57. string(1) "Y"
  58. [14]=>
  59. string(1) "Y"
  60. ["Lock_tables_priv"]=>
  61. string(1) "Y"
  62. [15]=>
  63. string(1) "Y"
  64. ["Create_view_priv"]=>
  65. string(1) "Y"
  66. [16]=>
  67. string(1) "Y"
  68. ["Show_view_priv"]=>
  69. string(1) "Y"
  70. [17]=>
  71. string(1) "Y"
  72. ["Create_routine_priv"]=>
  73. string(1) "Y"
  74. [18]=>
  75. string(1) "N"
  76. ["Alter_routine_priv"]=>
  77. string(1) "N"
  78. [19]=>
  79. string(1) "N"
  80. ["Execute_priv"]=>
  81. string(1) "N"
  82. [20]=>
  83. string(1) "Y"
  84. ["Event_priv"]=>
  85. string(1) "Y"
  86. [21]=>
  87. string(1) "Y"
  88. ["Trigger_priv"]=>
  89. string(1) "Y"
  90. }

从以上结果可看出,数据库连接以及查询语句都已经执行成功。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22059.html

相关文章

  • php设计模式

    摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...

    Dionysus_go 评论0 收藏0
  • php设计模式

    摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...

    vspiders 评论0 收藏0
  • PHP 设计模式——配器模式

    摘要:适配器模式只要应用于希望复用一些现存的类,但接口又与复用环境要求不一致的情况这是一种亡羊补牢的方法。首选的方法应该是重构代码,统一接口。 定义 将某个对象的接口适配为另一个对象所期望的接口,adapter模式使原本由于接口不兼容而不能一起工作的类可以一起工作 使用场景 需要的东西在面前,但却不能用,而短时间又无法改造它,于是就想办法适配 系统的数据和行为都正确,但接口不符时,应该考虑...

    周国辉 评论0 收藏0
  • PHP基础

    摘要:分别为适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模式,享元模式。设计模式五适配器模式适配器模式将某个对象的接生成器和协程的实现在这篇文章中,作者针对那些比较难以理解的概念,以一个更为通俗的方式去讲明白。。 PHP 源码注解 PHP 的详细源码注解 PHP 字符串操作整理 一些有关字符串的常用操作。 Redis 常见七种使用场景 (PHP 实战) 这篇文章主要介绍利用 R...

    HtmlCssJs 评论0 收藏0
  • JS14种设计模式 (6)

    摘要:序列文章面试之函数面试之对象面试之数组的几个不操作面试之对比分析面试之数据结构与算法前言设计模式如果应用到项目中,可以实现代码的复用和解耦,提高代码质量。 showImg(https://segmentfault.com/img/bVbq2VA?w=480&h=260); 序列文章 JS面试之函数(1)JS面试之对象(2)JS面试之数组的几个不low操作(3)JS面试之http0.9~...

    luckyyulin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<