PHP factory mode usage method

  • 2020-03-31 20:43:19
  • OfStack

Basic factory class
 
class MyObject{ 
//The object will be returned from the factory
} 
class MyFactory{ 
public static function factory(){ 
return new MyObject(): 
} 
} 
$instance=MyFactory::factory(); 

Use the factory class to parse the image file
 
<?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(){ 
//Complete the parsing of PNG format
//And populate $_width, $_height, $_data while forming;
} 
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(){ 
//Complete the parsing of JPEG format
//And populate $_width, $_height, $_data while forming;
} 
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: 
//There is a problem
} 
if($ret instanceof IImage){ 
return $ret; 
}else { 
//There is a problem
} 
} 
} 
//When calling a factory method with an image filename, different objects are obtained depending on the type of file passed in.
//Call ImageFactoyr
$image=ImageFactory::factory('/path/to/my.jpg'); 
//$image is an instance of the Image_JPEG class
echo $image->getWidth(); 

Use factory classes to solve database movability problems
In a database application, the factory pattern works in two ways.
. Make it easier for the software to support a variety of different database platforms for expanding the user base
If the software is used internally and the database needs to be modified, it is easy to move the application to another platform
In the code, you test it by creating a database table named User, which defines a varchar type field named email
 
<?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; 
} 
} 
} 

The application does not have to know what type of database it is connected to, and only deals directly with the instance returned from the factory based on the rules defined by the IDatabaseBindings interface.
 
//Call DatabaseFactoy
$db=DatabaseFactory::factory(); 
$db->userExists('person@example.com'); 

Related articles: