<?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<br/>";
}
public function connect() {
}
public function exec() {
}
}
class PostgreDB implements DB
{
public function __construct() {
echo "Postgre db<br/>";
}
public function connect() {
}
public function exec() {
}
}
class MssqlDB implements DB
{
public function __construct() {
echo "mssql db<br/>";
}
public function connect() {
}
public function exec() {
}
}
$oMysql = DBFactory::create("Mysql");
$oPostgre = DBFactory::create("Postgre");
$oMssql = DBFactory::create("Mssql");
php技術:php設計模式 Factory(工廠模式),轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。