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ù)庫可移值性問題
在數(shù)據(jù)庫應(yīng)用程序中,工廠模式可以在以下兩個(gè)方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫平臺(tái),用于擴(kuò)展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫時(shí),可以容易將應(yīng)用程序移值到別一個(gè)平臺(tái)
在代碼中,創(chuàng)建了一個(gè)名為User的數(shù)據(jù)庫表來測(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ù)庫連接,只會(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ù)庫可移值性問題
在數(shù)據(jù)庫應(yīng)用程序中,工廠模式可以在以下兩個(gè)方面起作用。
.使軟件更容易支持各種不同的數(shù)據(jù)庫平臺(tái),用于擴(kuò)展用戶群
.如果軟件是內(nèi)部使用,需要修改數(shù)據(jù)庫時(shí),可以容易將應(yīng)用程序移值到別一個(gè)平臺(tái)
在代碼中,創(chuàng)建了一個(gè)名為User的數(shù)據(jù)庫表來測(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ù)庫連接,只會(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ì)模式之簡單工廠模式詳解
- php設(shè)計(jì)模式 Factory(工廠模式)
- php基礎(chǔ)設(shè)計(jì)模式大全(注冊(cè)樹模式、工廠模式、單列模式)
- PHP中“簡單工廠模式”實(shí)例代碼講解
- PHP設(shè)計(jì)模式之工廠模式與單例模式
- PHP實(shí)現(xiàn)設(shè)計(jì)模式中的抽象工廠模式詳解
- PHP高級(jí)對(duì)象構(gòu)建 工廠模式的使用
- 基于php設(shè)計(jì)模式中工廠模式詳細(xì)介紹
- php設(shè)計(jì)模式之工廠方法模式分析【星際爭霸游戲案例】
相關(guān)文章
PHP實(shí)現(xiàn)的統(tǒng)計(jì)數(shù)據(jù)功能詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)的統(tǒng)計(jì)數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了php數(shù)據(jù)查詢與顯示處理的相關(guān)操作技巧,需要的朋友可以參考下2016-12-12
PHP源代碼數(shù)組統(tǒng)計(jì)count分析
偶然在百度知道中看到有個(gè)同學(xué)問起count及strlen的效率問題,好吧這個(gè)問題我當(dāng)初沒理解透徹,認(rèn)為其不屬兩個(gè)不一樣的東西不可比較,后來看了樓主的回復(fù)才反應(yīng)過來,所以自己也去找了下源碼查看下?,F(xiàn)在總結(jié)下查看到的結(jié)果并記錄之。2011-08-08
php 模擬 asp.net webFrom 按鈕提交事件的思路及代碼
這篇文章主要介紹了php模擬asp.net webFrom 按鈕提交事件的思路及代碼,有需要的朋友可以參考一下2013-12-12
解析使用ThinkPHP應(yīng)該掌握的調(diào)試手段
本篇文章是對(duì)使用ThinkPHP應(yīng)該掌握的調(diào)試手段進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)組合模式與裝飾模式詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)組合模式與裝飾模式,結(jié)合實(shí)例形式詳細(xì)分析了php組合模式與裝飾模式的定義、功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12
php循環(huán)語句 for()與foreach()用法區(qū)別介紹
下面我用兩個(gè)實(shí)例來介紹一下關(guān)于在php中foreach與for語句用法區(qū)別介紹,有需要的朋友可參考一下2012-09-09

