复制代码 代码如下:
/**
* 工厂方法模式
*
* 定义一个用于创建对象的接口,让子类决定将哪一个类实例化,使用一个类的实例化延迟到其子类
*/
/*
class DBFactory
{
public static function create($type)
{
swtich($type)
{
case "Mysql":
return new MysqlDB();休息;
案例“Postgre”:
返回新的PostgreDB();休息;
case "Mssql":
返回新的 MssqlDB();休息;
}
}
}
*/
class DBFactory
{
public static function create($type)
{
$class = $type. “数据库”;
返回新的$class;
}
}
接口数据库
{
public function connect();
公共函数exec();
}
类 MysqlDB 实现 DB
{
public function __construct() {
echo "mysql db
";
}
public function connect() {
}
public function exec() {
}
}
class PostgreDB 实现 DB
{
public function __construct() {
echo "Postgre db
";
}
public function connect() {
}
public function exec() {
}
}
类 MssqlDB 实现 DB
{
public function __construct() {
echo "mssql db
";
}
public function connect() {
}
public function exec() {
}
}
$oMysql = DBFactory::create (“MySQL”);
$oPostgre = DBFactory::create("Postgre");
$oMssql = DBFactory::create("Mssql");