PHP 工廠模式使用方法
更新時(shí)間:2010年05月18日 23:21:09 作者:
工廠類是指包含一個(gè)專門用來創(chuàng)建其他對(duì)象的方法的類,工廠類在多態(tài)性編程實(shí)踐中是至關(guān)重要的,它允許動(dòng)態(tài)的替換類,修改配置,通常會(huì)使應(yīng)用程序更加靈活,熟練掌握工廠模式高級(jí)PHP開發(fā)人員是很重要的。
基本的工廠類
class MyObject{
//對(duì)象將從工廠返回
}
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)用 工廠方法時(shí),根據(jù)傳入的文件類型不同,取得不同對(duì)象。
//調(diào)用ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg');
//$image是Image_JPEG類的一個(gè)實(shí)例
echo $image->getWidth();
使用工廠類解決數(shù)據(jù)庫(kù)可移值性問題
在數(shù)據(jù)庫(kù)應(yīng)用程序中,工廠模式可以在以下兩個(gè)方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫(kù)平臺(tái),用于擴(kuò)展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫(kù)時(shí),可以容易將應(yīng)用程序移值到別一個(gè)平臺(tái)
在代碼中,創(chuàng)建了一個(gè)名為User的數(shù)據(jù)庫(kù)表來測(cè)試它,這個(gè)表定義一個(gè)名為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ù)庫(kù)連接,只會(huì)基于IDatabaseBindings接口定義的規(guī)則直接與工廠返回的實(shí)例打交道。
//調(diào)用DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('person@example.com');
復(fù)制代碼 代碼如下:
class MyObject{
//對(duì)象將從工廠返回
}
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)用 工廠方法時(shí),根據(jù)傳入的文件類型不同,取得不同對(duì)象。
//調(diào)用ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg');
//$image是Image_JPEG類的一個(gè)實(shí)例
echo $image->getWidth();
使用工廠類解決數(shù)據(jù)庫(kù)可移值性問題
在數(shù)據(jù)庫(kù)應(yīng)用程序中,工廠模式可以在以下兩個(gè)方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫(kù)平臺(tái),用于擴(kuò)展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫(kù)時(shí),可以容易將應(yīng)用程序移值到別一個(gè)平臺(tái)
在代碼中,創(chuàng)建了一個(gè)名為User的數(shù)據(jù)庫(kù)表來測(cè)試它,這個(gè)表定義一個(gè)名為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ù)庫(kù)連接,只會(huì)基于IDatabaseBindings接口定義的規(guī)則直接與工廠返回的實(shí)例打交道。
復(fù)制代碼 代碼如下:
//調(diào)用DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('person@example.com');
您可能感興趣的文章:
- PHP設(shè)計(jì)模式之工廠方法設(shè)計(jì)模式實(shí)例分析
- php設(shè)計(jì)模式之簡(jiǎn)單工廠模式詳解
- php設(shè)計(jì)模式 Factory(工廠模式)
- php基礎(chǔ)設(shè)計(jì)模式大全(注冊(cè)樹模式、工廠模式、單列模式)
- PHP中“簡(jiǎn)單工廠模式”實(shí)例代碼講解
- PHP設(shè)計(jì)模式之工廠模式與單例模式
- PHP實(shí)現(xiàn)設(shè)計(jì)模式中的抽象工廠模式詳解
- PHP高級(jí)對(duì)象構(gòu)建 工廠模式的使用
- 基于php設(shè)計(jì)模式中工廠模式詳細(xì)介紹
- php設(shè)計(jì)模式之工廠方法模式分析【星際爭(zhēng)霸游戲案例】
相關(guān)文章
PHP基于反射機(jī)制實(shí)現(xiàn)插件的可插拔設(shè)計(jì)詳解
這篇文章主要介紹了PHP基于反射機(jī)制實(shí)現(xiàn)插件的可插拔設(shè)計(jì),結(jié)合實(shí)例形式較為詳細(xì)的分析了插件的功能、反射機(jī)制原理與實(shí)現(xiàn)可插拔設(shè)計(jì)的操作步驟,需要的朋友可以參考下2016-11-11PHP的SQL注入實(shí)現(xiàn)(測(cè)試代碼安全不錯(cuò))
看黑客是如何入侵的,我們寫編寫php代碼的過程中,最好自己先測(cè)試效果。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)記語(yǔ)言實(shí)例
這篇文章主要介紹了php將html轉(zhuǎn)成wml的WAP標(biāo)記語(yǔ)言的方法,實(shí)例分析了php實(shí)現(xiàn)標(biāo)簽的轉(zhuǎn)換與過濾的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-07-07phpinfo()中Loaded Configuration File(none)的解決方法
這篇文章主要給大家介紹了phpinfo()中Loaded Configuration File(none)問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01PHP根據(jù)手機(jī)號(hào)判斷運(yùn)營(yíng)商(詳細(xì)介紹附代碼)
這篇文章主要介紹了PHP根據(jù)手機(jī)號(hào)判斷運(yùn)營(yíng)商,詳細(xì)介紹附代碼,大家可以根據(jù)最新的號(hào)段進(jìn)行添加即可,通過正則判斷實(shí)現(xiàn),需要的朋友可以參考下2018-01-01PHP二維數(shù)組分頁(yè)2種實(shí)現(xiàn)方法解析
這篇文章主要介紹了PHP二維數(shù)組分頁(yè)2種實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07