PHP factory mode usage _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP factory mode usage. The code for copying basic factory classes is as follows: classMyObject {the object will be returned from the factory} classMyFactory {publicstaticfunctionfactory () {returnnewMyObject () :}} $ instanceMy basic factory class

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 ();


Parse image files using the factory class

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 parsing in 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 JPEG format parsing
// 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 the factory method is called using the image file name, different objects are obtained based on the input file type.
// Call ImageFactoyr
$ Image = ImageFactory: factory ('/path/to/my.jpg ');
// $ Image is an example of the Image_JPEG class.
Echo $ image-> getWidth ();


Use the factory class to solve the database value portability problem
In database applications, the factory model can work in the following two aspects.
Make it easier for the software to support different database platforms for scaling user groups.
If the software is used internally, you can easily move the application value to another platform when you need to modify the database.
In the code, a database table named User is created to test it. this table defines a varchar field named email.

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 need to know the type of database connection it connects to. Instead, it only deals with the instances returned by the factory based on the rules defined by the IDatabaseBindings interface.

The code is as follows:


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

The response code is as follows: class MyObject {// The object will return} 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.