PHP 工廠模式使用方法
更新時間:2010年05月18日 23:21:09 作者:
工廠類是指包含一個專門用來創(chuàng)建其他對象的方法的類,工廠類在多態(tài)性編程實踐中是至關重要的,它允許動態(tài)的替換類,修改配置,通常會使應用程序更加靈活,熟練掌握工廠模式高級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 {
//有問題
}
}
}
//當使用圖像文件名調(diào)用 工廠方法時,根據(jù)傳入的文件類型不同,取得不同對象。
//調(diào)用ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg');
//$image是Image_JPEG類的一個實例
echo $image->getWidth();
使用工廠類解決數(shù)據(jù)庫可移值性問題
在數(shù)據(jù)庫應用程序中,工廠模式可以在以下兩個方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫平臺,用于擴展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫時,可以容易將應用程序移值到別一個平臺
在代碼中,創(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;
}
}
}
應用程序不必知道它與何種類型的數(shù)據(jù)庫連接,只會基于IDatabaseBindings接口定義的規(guī)則直接與工廠返回的實例打交道。
//調(diào)用DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('person@example.com');
復制代碼 代碼如下:
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 {
//有問題
}
}
}
//當使用圖像文件名調(diào)用 工廠方法時,根據(jù)傳入的文件類型不同,取得不同對象。
//調(diào)用ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg');
//$image是Image_JPEG類的一個實例
echo $image->getWidth();
使用工廠類解決數(shù)據(jù)庫可移值性問題
在數(shù)據(jù)庫應用程序中,工廠模式可以在以下兩個方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫平臺,用于擴展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫時,可以容易將應用程序移值到別一個平臺
在代碼中,創(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;
}
}
}
應用程序不必知道它與何種類型的數(shù)據(jù)庫連接,只會基于IDatabaseBindings接口定義的規(guī)則直接與工廠返回的實例打交道。
復制代碼 代碼如下:
//調(diào)用DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('person@example.com');
相關文章
PHP實現(xiàn)的統(tǒng)計數(shù)據(jù)功能詳解
這篇文章主要介紹了PHP實現(xiàn)的統(tǒng)計數(shù)據(jù)功能,結合實例形式分析了php數(shù)據(jù)查詢與顯示處理的相關操作技巧,需要的朋友可以參考下2016-12-12
php 模擬 asp.net webFrom 按鈕提交事件的思路及代碼
這篇文章主要介紹了php模擬asp.net webFrom 按鈕提交事件的思路及代碼,有需要的朋友可以參考一下2013-12-12
php循環(huán)語句 for()與foreach()用法區(qū)別介紹
下面我用兩個實例來介紹一下關于在php中foreach與for語句用法區(qū)別介紹,有需要的朋友可參考一下2012-09-09

