How to use PHP Factory mode _php Tutorial

Source: Internet
Author: User
Basic Factory class
Copy CodeThe code is as follows:
Class myobject{
Object will be returned from the factory
}
Class myfactory{
public static function factory () {
return new MyObject ():
}
}
$instance =myfactory::factory ();

Parsing an image file using the factory class
Copy CodeThe 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 () {
Finish parsing the PNG format
and fill $_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 () {
Finish parsing work in JPEG format
and fill $_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
Have a problem
}
if ($ret instanceof IImage) {
return $ret;
}else {
Have a problem
}
}
}
When calling the factory method using the image file name, different objects are obtained depending on the type of file being passed in.
Call Imagefactoyr
$image =imagefactory::factory ('/path/to/my.jpg ');
$image is an example of the Image_jpeg class
echo $image->getwidth ();

Using factory classes to troubleshoot database-related issues
In a database application, the factory pattern can function in the following two ways.
The software makes it easier to support a variety of different database platforms for expanding the user base
If the software is used internally and you need to modify the database, you can easily move the application value to one platform
In the code, a database table named user is created to test it, and this table defines a varchar type field called email
Copy CodeThe 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 connection it has, and only deals with the instances returned by the factory directly based on the rules defined by the Idatabasebindings interface.
Copy CodeThe code is as follows:
Call Databasefactoy
$db =databasefactory::factory ();
$db->userexists (' person@example.com ');

http://www.bkjia.com/PHPjc/321890.html www.bkjia.com true http://www.bkjia.com/PHPjc/321890.html techarticle the basic factory class copy code code is as follows: Class myobject{//object will return from factory} class myfactory{public static function factory () {return new MyObject ( ):}} $instance =my ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.