Home > Backend Development > PHP Tutorial > Abstract factory pattern in php

Abstract factory pattern in php

WBOY
Release: 2016-08-08 09:31:30
Original
809 people have browsed it

The difference between the abstract factory pattern and the factory method pattern is that the abstract factory first creates the factory, and then the factory creates the product (instance);


defines an interface for creating objects and lets the subclass decide which class to instantiate. He can solve the closed-open principle problem in the simple factory pattern;

// 产品(数据库)标准


 interface DbInterface{
	public function connect(Array $params=array());
    public function query($sql);
    public function insert($table, $record);
    public function update($table, $record, $where);
    public function delete($table, $where);
}



// 具体产品(Mysql)

class MysqlDb implements DbInterface(){

    public function connect(Array $params=array());
    public function query($sql){}
    public function insert($table, $record){}
    public function update($table, $record, $where){}
    public function delete($table, $where){}

}


class OracalDb implements DbInterface(){

    public function connect(Array $params=array()){}
    public function query($sql){}
    public function insert($table, $record){}
    public function update($table, $record, $where){}
    public function delete($table, $where){}

}



// 构造工厂 

interface CreateFactory(){

    function createDB(); //分为 内敛的和外向的

}


class FactoryMysql implements CreateFactory{
    function createDB() {
        return  new  MysqlDb();
    }
}


class FactoryOracle implements CreateFactory{
    function createDB() {
        return  new  OracalDb();
    }
}




(1) 如果想使用mysql

$db = new FactoryMysql()->createDB();   //
Copy after login


The above has introduced the abstract factory pattern in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template