程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP 工廠模式使用方法

PHP 工廠模式使用方法

編輯:關於PHP編程

基本的工廠類
復制代碼 代碼如下:
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 {
//有問題
}
}
}
//當使用圖像文件名調用 工廠方法時,根據傳入的文件類型不同,取得不同對象。
//調用ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg');
//$image是Image_JPEG類的一個實例
echo $image->getWidth();

使用工廠類解決數據庫可移值性問題
在數據庫應用程序中,工廠模式可以在以下兩個方面起作用。
.使軟件更容易支持各種不同的數據庫平台,用於擴展用戶群
.如果軟件是內部使用,需要修改數據庫時,可以容易將應用程序移值到別一個平台
在代碼中,創建了一個名為User的數據庫表來測試它,這個表定義一個名為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;
}
}
}

應用程序不必知道它與何種類型的數據庫連接,只會基於IDatabaseBindings接口定義的規則直接與工廠返回的實例打交道。
復制代碼 代碼如下:
//調用DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('[email protected]');

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved