php设计模式 Factory(工厂模式)

前端之家收集整理的这篇文章主要介绍了php设计模式 Factory(工厂模式)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="78754" class="copybut" id="copybut78754" onclick="doCopy('code78754')"> 代码如下:

<div class="codebody" id="code78754">
<?PHP
/*
工厂方法模式

定义一个用于创建对象的接口,让子类决定将哪一个类实例化,使用一个类的实例化延迟到其子类
/ /
class DBFactory
{
public static function create($type)
{
swtich($type)
{
case "MysqL":
return new MysqLDB(); break;
case "Postgre":
return new PostgreDB(); break;
case "Mssql":
return new MssqlDB(); break;
}
}
}
*/
class DBFactory
{
public static function create($type)
{
$class = $type."DB";
return new $class;
}
} interface DB
{
public function connect();
public function exec();
} class MysqLDB implements DB
{
public function construct() {
echo "MysqL db
";
} public function connect() {
} public function exec() {
}
} class PostgreDB implements DB
{
public function __construct() {
echo "Postgre db
";
} public function connect() {
} public function exec() {
}
} class MssqlDB implements 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");

原文链接:https://www.f2er.com/php/27841.html

猜你在找的PHP相关文章