PHP-适配器模式

什么是适配器模式

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。

举个栗子:

PHP常用的操作数据库的方法有三种:MySQL、MySQLi和PDO,需要怎么去适配呢,又或者说,还有Oracle、非关系型数据库(Memcache、Redis)又怎么去适配呢。

适配器模式的实现


<?php

/**

 * 数据库适配器

 * Interface DatabaseAdapter

 */

interface DatabaseAdapter

{

    /**

     * 连接数据库

     * @param string $host

     * @param int $port

     * @param string $username

     * @param string $password

     * @param string $dbname

     * @return mixed

     */

    public function connect(string $host, int $port, string $username, string $password, string $dbname);

    /**

     * 执行sql

     * @param string $sql

     * @return mixed

     */

    public function query(string $sql);

    /**

     * 关闭连接

     * @return mixed

     */

    public function close();

}

/**

 * Class MySQLi

 */

class MySQLi implements DatabaseAdapter

{

    /**

     * 连接数据库

     * @param string $host

     * @param int $port

     * @param string $username

     * @param string $password

     * @param string $dbname

     * @return mixed

     */

    public function connect(string $host, int $port, string $username, string $password, string $dbname)

    {

        // TODO: Implement connect() method.

    }

    /**

     * 执行sql

     * @param string $sql

     * @return mixed

     */

    public function query(string $sql)

    {

        // TODO: Implement query() method.

    }

    /**

     * 关闭连接

     * @return mixed

     */

    public function close()

    {

        // TODO: Implement close() method.

    }

}

/**

 * Class MySQL

 */

class MySQL implements DatabaseAdapter

{

    /**

     * 连接数据库

     * @param string $host

     * @param int $port

     * @param string $username

     * @param string $password

     * @param string $dbname

     * @return mixed

     */

    public function connect(string $host, int $port, string $username, string $password, string $dbname)

    {

        // TODO: Implement connect() method.

    }

    /**

     * 执行sql

     * @param string $sql

     * @return mixed

     */

    public function query(string $sql)

    {

        // TODO: Implement query() method.

    }

    /**

     * 关闭连接

     * @return mixed

     */

    public function close()

    {

        // TODO: Implement close() method.

    }

}

/**

 * Class PDO

 */

class PDO implements DatabaseAdapter

{

    /**

     * 连接数据库

     * @param string $host

     * @param int $port

     * @param string $username

     * @param string $password

     * @param string $dbname

     * @return mixed

     */

    public function connect(string $host, int $port, string $username, string $password, string $dbname)

    {

        // TODO: Implement connect() method.

    }

    /**

     * 执行sql

     * @param string $sql

     * @return mixed

     */

    public function query(string $sql)

    {

        // TODO: Implement query() method.

    }

    /**

     * 关闭连接

     * @return mixed

     */

    public function close()

    {

        // TODO: Implement close() method.

    }

}

更多内容关注个人博客:lemonlyue.github.io/

本作品采用《CC 协议》,转载必须注明作者和本文链接
lemon_lyue
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!