Home >Backend Development >PHP Tutorial >Abstract factory pattern in php
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(); //