interface Factory {
public static function getDB();
}
class MySQLFactory implements Factory
{
public static function getDB(){
return new MySQL();
}
}
class SqliteFactory implements Factory
{
public static function getDB(){
return new Sqlite();
}
}
class MyPDOFactory implements Factory
{
public static function getDB(){
return new MyPDO();
}
}
/*具体实现*/·
class MySQL{}
class Sqlite{}
class MyPDO{}
// 配置文件选择的是pdo
$fact = 'MyPDOFactory';
$db = MyPDOFactory::getDB();
print_r($db);
02-抽象工厂模式
于 2023-02-03 11:16:34 首次发布