Home > Backend Development > PHP Tutorial > How to use PHP factory mode_PHP tutorial

How to use PHP factory mode_PHP tutorial

WBOY
Release: 2016-07-21 15:37:38
Original
941 people have browsed it

Basic factory class

Copy code The code is as follows:

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

Use factory class to parse image files
Copy the code The code is as follows:

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
//Fill in $_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(){
//Complete the parsing of JPEG format
//And fill in $ _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:
// There is a problem
}
if($ret instanceof IImage){
return $ret;
}else {
//There is a problem
}
}
}
//When calling the factory method using the image file name, different objects are obtained depending on the file type 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 portability issues
In database applications, the factory pattern can work in the following two aspects.
. Make it easier for the software to support various database platforms to expand the user base
. If the software is used internally and the database needs to be modified, the application can be easily migrated to another platform
In the code, a database table named User is created to test it. This table defines a varchar type field named email
Copy code The code is as follows :

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 will only deal directly with the instances returned by the factory based on the rules defined by the IDatabaseBindings interface.
Copy code The code is as follows:

//Call DatabaseFactoy
$db=DatabaseFactory::factory();
$db->userExists('person@example.com');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321890.htmlTechArticleThe basic factory class copy code code is as follows: class MyObject{ //The object will be returned from the factory} class MyFactory{ public static function factory(){ return new MyObject(): } } $instance=My...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template