简单工厂

通过静态方法传入不同的参数创建不同的对象,实现对象创建和使用的分离

<?php
class mysql_conn{
    private $dbh = null;
    private $host = 'localhost';
    private $port = '3306';
    private $user = 'root';
    private $password = '****';
    private $dbname = 'test';

    public function __construct(){
        $this->dbh = new PDO('mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name){
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row){
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}

class psql_conn{
    private $dbh = null;
    private $host = 'localhost';
    private $port = '5432';
    private $user = 'postgres';
    private $password = '****';
    private $dbname = 'test';

    public function __construct(){
        $this->dbh = new PDO('pgsql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name){
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row){
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql){
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}

class Factory{
    public static function pdo_conn($db = 'mysql'){
        if ($db == 'mysql') {
            return new mysql_conn();
        }
        else if ($db == 'psql'){
            return new psql_conn();
        }
    }
}

// mysql
// $mysql_dbh = Factory::pdo_conn('mysql');
// $mysql_dbh->get_table_data('user');
// $mysql_dbh->get_table_data('order');

// postgresql
// $psql_dbh = Factory::pdo_conn('psql');
// $psql_dbh->custom_query('select * from student');

通过上面的代码可以看出,简单工厂的缺点就是后面如果想增加新的连接,需要频繁地去修改静态方法

工厂方法

通过定义一个抽象的核心工厂类,并定义创建产品对象的接口。
创建具体产品示例的工作延迟到其工厂子类去完成。

当系统需要新增一个产品是,无需修改现有系统代码,只需要添加一个具体产品类和其对应的工厂子类,使系统的扩展性变得很好,符合面向对象编程的开闭原则;

<?php
interface database_option{
    public function custom_query($sql);
    public function get_table_data($table_name);
}

class mysql_conn implements database_option{
    private $dbh = null;
    private $host = 'localhost';
    private $port = '3306';
    private $user = 'root';
    private $password = '****';
    private $dbname = 'test';

    public function __construct(){
        $this->dbh = new PDO('mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name){
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row){
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql){
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}

class psql_conn implements database_option{
    private $dbh = null;
    private $host = 'localhost';
    private $port = '5432';
    private $user = 'postgres';
    private $password = '****';
    private $dbname = 'test';

    public function __construct(){
        $this->dbh = new PDO('pgsql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name){
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql){
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}

abstract class Factory{
    abstract static function pdo_conn();
}

class mysql_Factory extends Factory{
    public static function pdo_conn(){
        return new mysql_conn();
    }
}

class psql_Factory extends Factory{
    public static function pdo_conn(){
        return new psql_conn();
    }
}

// mysql
// $mysql_dbh = mysql_Factory::pdo_conn();
// $mysql_dbh->get_table_data('order');

// psql
// $psql_dbh = psql_Factory::pdo_conn();
// $psql_dbh->custom_query('select * from student');

工厂方法是简单工厂的进一步抽象,由于使用了面向对象的多态性,工厂方法保持了简单工厂的优点,在工厂方法中,核心的工厂类不再负责所有产品的创建,而是将具体创建工作交给子类。所以可以在不修改工厂方法的情况下引入新的产品,符合开闭原则。

抽象工厂

提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。

在工厂方法模式中,一个具体的工厂负责生产一类具体的产品,即一对一的关系,但是,如果需要一个具体的工厂生产多种产品对象,那么就需要用到抽象工厂模式了。

<?php
interface database_option{
    public function custom_query($sql);
    public function get_table_data($table_name);
}

class mysql_conn implements database_option{
    private $dbh = null;
    private $host = 'localhost';
    private $port = '3306';
    private $user = 'root';
    private $password = '****';
    private $dbname = 'test';

    public function __construct(){
        $this->dbh = new PDO('mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name){
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql){
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}

class psql_conn implements database_option{
    private $dbh = null;
    private $host = 'localhost';
    private $port = '5432';
    private $user = 'postgres';
    private $password = '****';
    private $dbname = 'test';

    public function __construct(){
        $this->dbh = new PDO('pgsql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name){
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql){
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}

abstract class Factory{
    abstract public static function create_mysql_conn();
    abstract public static function create_psql_conn();
}

class db_Factory extends Factory{
    public static function create_mysql_conn(){
        return new mysql_conn();
    }

    public static function create_psql_conn(){
        return new psql_conn();
    }
}

// mysql
// $mysql_dbh = db_Factory::create_mysql_conn();
// $mysql_dbh->get_table_data('order');

// psql
// $psql_dbh = db_Factory::create_psql_conn();
// $psql_dbh->custom_query('select * from student');