PHP 工廠模式使用方法
更新時間:2010年05月18日 23:21:09 作者:
工廠類是指包含一個專門用來創(chuàng)建其他對象的方法的類,工廠類在多態(tài)性編程實(shí)踐中是至關(guān)重要的,它允許動態(tài)的替換類,修改配置,通常會使應(yīng)用程序更加靈活,熟練掌握工廠模式高級PHP開發(fā)人員是很重要的。
基本的工廠類
class MyObject{
//對象將從工廠返回
}
class MyFactory{
public static function factory(){
return new MyObject():
}
}
$instance=MyFactory::factory();
使用工廠類解析圖像文件
<?php
interface IImage{
function getHeight();
function getWidth();
function getData();
}
class Image_PNG implements IImage{
private $_width,$_height,$_data;
public function __construct($file){
$this->_file=$file;
$this->_parse();
}
private function _parse(){
//完成PNG格式的解析工作
//并填充$_width,$_height,$_data;
}
public function getWidth(){
return $this->_width;
}
public function getHeight(){
return $this->_height;
}
public function getData(){
return $this->_data;
}
}
class Image_JPEG implements IImage{
private $_width,$_height,$_data;
public function __construct($file){
$this->_file=$file;
$this->_parse();
}
private function _parse(){
//完成JPEG格式的解析工作
//并填充$_width,$_height,$_data;
}
public function getWidth(){
return $this->_width;
}
public function getHeight(){
return $this->_height;
}
public function getData(){
return $this->_data;
}
}
class ImageFactory{
public static function factory($file){
$pathParts=pathinfo($file);
switch (strtolower($pathParts['extension']))
{
case 'jpg':
$ret=new Image_JPEG($file);
break;
case 'png':
$ret=new Image_PNG($file);
break;
default:
//有問題
}
if($ret instanceof IImage){
return $ret;
}else {
//有問題
}
}
}
//當(dāng)使用圖像文件名調(diào)用 工廠方法時,根據(jù)傳入的文件類型不同,取得不同對象。
//調(diào)用ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg');
//$image是Image_JPEG類的一個實(shí)例
echo $image->getWidth();
使用工廠類解決數(shù)據(jù)庫可移值性問題
在數(shù)據(jù)庫應(yīng)用程序中,工廠模式可以在以下兩個方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫平臺,用于擴(kuò)展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫時,可以容易將應(yīng)用程序移值到別一個平臺
在代碼中,創(chuàng)建了一個名為User的數(shù)據(jù)庫表來測試它,這個表定義一個名為email的varchar類型字段
<?php
interface IDatabaseBindings{
public function userExists($email);
}
class PGSQL implements IDatabaseBindings{
protected $_connection;
public function __construct(){
$this->_connection=pg_connect('dbname=example_db');
}
public function userExists($email){
$emailEscaped=pg_escape_string($email);
$query="select 1 from users where email='".$emailEscaped."'";
if($result=pg_query($query,$this->_connection)){
return (pg_num_rows($result)>0)?true:false;
}else{
return false;
}
}
}
class MYSQL implements IDatabaseBindings{
protected $_connection;
public function __construct(){
$this->_connection=mysql_connect('localhost');
mysql_select_db('example_db',$this->_connection);
}
public function userExists($email){
$emailEscaped=mysql_real_escape_string($email);
$query="select 1 from users where email='".$emailEscaped."'";
if($result=mysql_query($query,$this->_connection)){
return (mysql_num_rows($result)>0)?true:false;
}else{
return false;
}
}
}
class DatabaseFactory{
public static function factory(){
$type=loadtypefromconfigfile();
switch ($type){
case 'PGSQL':
return new PGSQL();
break;
case 'MYSQL':
return new MYSQL();
break;
}
}
}
應(yīng)用程序不必知道它與何種類型的數(shù)據(jù)庫連接,只會基于IDatabaseBindings接口定義的規(guī)則直接與工廠返回的實(shí)例打交道。
//調(diào)用DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('person@example.com');
復(fù)制代碼 代碼如下:
class MyObject{
//對象將從工廠返回
}
class MyFactory{
public static function factory(){
return new MyObject():
}
}
$instance=MyFactory::factory();
使用工廠類解析圖像文件
復(fù)制代碼 代碼如下:
<?php
interface IImage{
function getHeight();
function getWidth();
function getData();
}
class Image_PNG implements IImage{
private $_width,$_height,$_data;
public function __construct($file){
$this->_file=$file;
$this->_parse();
}
private function _parse(){
//完成PNG格式的解析工作
//并填充$_width,$_height,$_data;
}
public function getWidth(){
return $this->_width;
}
public function getHeight(){
return $this->_height;
}
public function getData(){
return $this->_data;
}
}
class Image_JPEG implements IImage{
private $_width,$_height,$_data;
public function __construct($file){
$this->_file=$file;
$this->_parse();
}
private function _parse(){
//完成JPEG格式的解析工作
//并填充$_width,$_height,$_data;
}
public function getWidth(){
return $this->_width;
}
public function getHeight(){
return $this->_height;
}
public function getData(){
return $this->_data;
}
}
class ImageFactory{
public static function factory($file){
$pathParts=pathinfo($file);
switch (strtolower($pathParts['extension']))
{
case 'jpg':
$ret=new Image_JPEG($file);
break;
case 'png':
$ret=new Image_PNG($file);
break;
default:
//有問題
}
if($ret instanceof IImage){
return $ret;
}else {
//有問題
}
}
}
//當(dāng)使用圖像文件名調(diào)用 工廠方法時,根據(jù)傳入的文件類型不同,取得不同對象。
//調(diào)用ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg');
//$image是Image_JPEG類的一個實(shí)例
echo $image->getWidth();
使用工廠類解決數(shù)據(jù)庫可移值性問題
在數(shù)據(jù)庫應(yīng)用程序中,工廠模式可以在以下兩個方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫平臺,用于擴(kuò)展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫時,可以容易將應(yīng)用程序移值到別一個平臺
在代碼中,創(chuàng)建了一個名為User的數(shù)據(jù)庫表來測試它,這個表定義一個名為email的varchar類型字段
復(fù)制代碼 代碼如下:
<?php
interface IDatabaseBindings{
public function userExists($email);
}
class PGSQL implements IDatabaseBindings{
protected $_connection;
public function __construct(){
$this->_connection=pg_connect('dbname=example_db');
}
public function userExists($email){
$emailEscaped=pg_escape_string($email);
$query="select 1 from users where email='".$emailEscaped."'";
if($result=pg_query($query,$this->_connection)){
return (pg_num_rows($result)>0)?true:false;
}else{
return false;
}
}
}
class MYSQL implements IDatabaseBindings{
protected $_connection;
public function __construct(){
$this->_connection=mysql_connect('localhost');
mysql_select_db('example_db',$this->_connection);
}
public function userExists($email){
$emailEscaped=mysql_real_escape_string($email);
$query="select 1 from users where email='".$emailEscaped."'";
if($result=mysql_query($query,$this->_connection)){
return (mysql_num_rows($result)>0)?true:false;
}else{
return false;
}
}
}
class DatabaseFactory{
public static function factory(){
$type=loadtypefromconfigfile();
switch ($type){
case 'PGSQL':
return new PGSQL();
break;
case 'MYSQL':
return new MYSQL();
break;
}
}
}
應(yīng)用程序不必知道它與何種類型的數(shù)據(jù)庫連接,只會基于IDatabaseBindings接口定義的規(guī)則直接與工廠返回的實(shí)例打交道。
復(fù)制代碼 代碼如下:
//調(diào)用DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('person@example.com');
相關(guān)文章
PHP基于反射機(jī)制實(shí)現(xiàn)插件的可插拔設(shè)計詳解
這篇文章主要介紹了PHP基于反射機(jī)制實(shí)現(xiàn)插件的可插拔設(shè)計,結(jié)合實(shí)例形式較為詳細(xì)的分析了插件的功能、反射機(jī)制原理與實(shí)現(xiàn)可插拔設(shè)計的操作步驟,需要的朋友可以參考下2016-11-11PHP的SQL注入實(shí)現(xiàn)(測試代碼安全不錯)
看黑客是如何入侵的,我們寫編寫php代碼的過程中,最好自己先測試效果。2011-02-02PHP實(shí)現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題,結(jié)合具體實(shí)例形式分析了php使用單鏈表解決約瑟夫環(huán)問題的算法原理與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09php將html轉(zhuǎn)成wml的WAP標(biāo)記語言實(shí)例
這篇文章主要介紹了php將html轉(zhuǎn)成wml的WAP標(biāo)記語言的方法,實(shí)例分析了php實(shí)現(xiàn)標(biāo)簽的轉(zhuǎn)換與過濾的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-07-07phpinfo()中Loaded Configuration File(none)的解決方法
這篇文章主要給大家介紹了phpinfo()中Loaded Configuration File(none)問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01PHP根據(jù)手機(jī)號判斷運(yùn)營商(詳細(xì)介紹附代碼)
這篇文章主要介紹了PHP根據(jù)手機(jī)號判斷運(yùn)營商,詳細(xì)介紹附代碼,大家可以根據(jù)最新的號段進(jìn)行添加即可,通過正則判斷實(shí)現(xiàn),需要的朋友可以參考下2018-01-01PHP二維數(shù)組分頁2種實(shí)現(xiàn)方法解析
這篇文章主要介紹了PHP二維數(shù)組分頁2種實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07