美文网首页
设计模式(十一):适配器模式

设计模式(十一):适配器模式

作者: 骑着母猪砍大象 | 来源:发表于2018-12-11 13:02 被阅读10次

转载请注明作者和出处https://www.jianshu.com/p/c81e5d4fa5c2

运行平台: Windows

php版本: php7.0

作者简介: 一个本该成为游戏职业选手却被编程耽误的程序员


适配器模式(Adapter Pattern):将某个对象的接口适配为另一个对象所期望的接口。属于结构型设计模式。
我们常见的适配器模式就是数据库操作,无论是mysql还是mysqli都是使用connet()连接,close()关闭,这就是适配器模式

//MySQL待操作适配类
class MySQLAdaptee implements Target
{
    protected $conn;    //用于存放数据库连接句柄
    //实现连接方法
    public function connect($host, $user, $passwd, $dbname)
    {
        $conn = mysql_connect($host, $user, $passwd);
        mysql_select_db($dbname, $conn);
        $this->conn = $conn;
    }
    //查询方法
    public function query($sql)
    {
        $res = mysql_query($sql, $this->conn);
        return $res;
    }
    //关闭方法
    public function close()
    {
        mysql_close($this->conn);
    }
}
//MySQLi操作待适配类
class MySQLiAdaptee 
{
    protected $conn;
    public function connect($host, $user, $passwd, $dbname)
    {
        $conn = mysqli_connect($host, $user, $passwd, $dbname);
        $this->conn = $conn;
    }
    public function query($sql)
    {
        return mysqli_query($this->conn, $sql);
    }
    public function close()
    {
        mysqli_close($this->conn);
    }
}
//用户所期待的接口
Interface Target{
    public function connect($host, $user, $passwd, $dbname);
    public function query($sql);
    public function close();
}
//用户期待适配类
Class DataBase implements Target {
    protected $db ;     //存放MySQLiAdapter对象或MySQLAdapter对象
    public function  __construct($type){
        $type = $type."Adapter" ;
        $this->db = new $type ;
    }
    public function connect($host, $user, $passwd, $dbname){
        $this->db->connect($host, $user, $passwd, $dbname);
    }
    public function query($sql){
        return $this->db->query($sql);
    }
    public function close(){
        $this->db->close();
    }
}
//用户调用同一个接口,使用MySQL和mysqli这两套不同示例。
$db1 = new DataBase('MySQL');
$db1->connect('127.0.0.1','root','1234','myDB');die;
$db1->query('select * from test');
$db1->close();

$db2 = new DataBase('MySQLi');
$db2->connect('127.0.0.1','root','1234','myDB');
$db2->query('select * from test');
$db2->close();

相关文章

  • 简说设计模式之适配器模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是适配器模式 适配器模式(Adapter...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 最常用的设计模式---适配器模式(C++实现)

    适配器模式属于结构型的设计模式,它是结构型设计模式之首(用的最多的结构型设计模式)。 适配器设计模式也并不复杂,适...

  • iOS设计模式(5)策略模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(6)模板模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(7)建造者模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(4)抽象工厂模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(1)简单工厂模式

    设计模式系列文章 《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象工厂...

  • iOS设计模式(2)工厂模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象...

  • iOS设计模式(8)外观模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

网友评论

      本文标题:设计模式(十一):适配器模式

      本文链接:https://www.haomeiwen.com/subject/mhbshqtx.html