關於原廠模式的問題
Example #1 調用Factory 方法(帶參數)
class Example
{
// The parameterized factory method
public static function factory($type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
?>
按上面的方式可以動態載入drivers。如果Example類是一個資料庫抽象類別,那麼 可以這樣來產生MySQL和 SQLite驅動對象:
// Load a MySQL Driver
$mysql = Example::factory('MySQL');
// Load a SQLite Driver
$sqlite = Example::factory('SQLite');
?>