PHP Design Pattern Factory Pattern Study Notes_PHP Tutorial
WBOY
Release: 2016-07-13 17:15:41
Original
1057 people have browsed it
This article will introduce to you a new thing in php5, which is the factory mode. Here are some study notes of my commonly used factory mode. I will share it with you below for reference by friends who need to know more.
In large systems, a lot of code depends on a few key classes. Difficulties may arise when these classes need to be changed. For example, suppose you have a User class that reads from a file. You want to change it to a different class that reads from the database, however, all your code references the original class that reads from the file. At this time, it will be very convenient to use factory mode.
Factory pattern is a class that has certain methods that create objects for you. You can use a factory class to create objects without using new directly. This way, if you want to change the type of object created, you only need to change the factory. All code using this factory is automatically changed.
Example 1: Display a column of the factory class.
The server side of the equation consists of two parts: a database and a set of PHP pages that allow you to add feedback, request a feedback list, and get articles related to specific feedback
The code is as follows
Copy code
代码如下
复制代码
interface IUser
{
function getName();
}
class User implements IUser
{
public function __construct( $id ) { }
public function getName()
{
return "Jack";
}
}
class UserFactory
{
public static function Create( $id )
{
return new User( $id );
}
}
$uo = UserFactory::Create( 1 );
echo( $uo->getName()."n" );
?>
interface IUser
{
function getName();
}
class User implements IUser
{
public function __construct( $id ) { }
public function getName()
{
Return "Jack";
}
}
class UserFactory
{
public static function Create( $id )
{
Return new User( $id );
}
代码如下
复制代码
interface IUser
{
function getName();
}
class User implements IUser
{
public static function Load( $id )
{
return new User( $id );
}
public static function Create( )
{
return new User( null );
}
public function __construct( $id ) { }
public function getName()
{
return "Jack";
}
}
$uo = User::Load( 1 );
echo( $uo->getName()."n" );
?>
}
$uo = UserFactory::Create( 1 );
echo( $uo->getName()."n" );
?>
The IUser interface defines what operations the user object should perform. The implementation of IUser is called User, and the UserFactory factory class creates IUser objects. This relationship can be represented by UML in Figure 1.
Figure 1. Factory class and its related IUser interface and user class
If you run this code on the command line using the php interpreter, you will get the following results:
% php factory1.php
Jack
%
The test code will request the User object from the factory and output the result of the getName method.
There is a variant of the factory pattern that uses factory methods. These public static methods in a class construct objects of that type. This method is useful if it is important to create objects of this type. For example, suppose you need to create an object and then set a number of properties. This version of the factory pattern encapsulates the process in a single location, so you don't have to copy complex initialization code and paste it all over the code base.
Example 2 shows an example of using factory methods.
The code is as follows
Copy code
interface IUser<🎜>
{<🎜>
function getName();<🎜>
}<🎜>
class User implements IUser<🎜>
{<🎜>
public static function Load( $id )<🎜>
{<🎜>
return new User( $id );<🎜>
}<🎜>
public static function Create( )<🎜>
{<🎜>
return new User( null );<🎜>
}<🎜>
public function __construct( $id ) { }<🎜>
public function getName()<🎜>
{<🎜>
Return "Jack";<🎜>
}<🎜>
}<🎜>
$uo = User::Load( 1 );<🎜>
echo( $uo->getName()."n" );
?>
Okay, I’ve talked a lot above, let me look at an example
class factory
{
function factory()
{
$this->mClasses=array('T1'=>'t1.inc.php','T2'=>'t2.inc.php');
}
function &create($class)
{
if (!class_exists($class))
{
require_once($this->mClasses[$class]);
}
return new $class;
}
}
?>
$t1=&$f->create('T1');
echo $t1->getName();
echo $config;
代码如下
复制代码
global $config;
$config='surfchen';
class T1
{
var $mName='name::T1';
function getName()
{
return $this->mName;
}
}
?>
?>
f.inc.php
代码如下
复制代码
class T2
{
function T2()
{
echo 't2 is ok';
}
}
?>
{
if (!class_exists($class))
{
require_once($this->mClasses[$class]);
}
Return new $class;
}
}
?>
t1.inc.php
The code is as follows
Copy code
global $config;<🎜>
$config='surfchen';<🎜>
class T1<🎜>
{<🎜>
var $mName='name::T1';<🎜>
function getName()<🎜>
{<🎜>
Return $this->mName;
}
}
?>
t2.inc.php
The code is as follows
Copy code
class T2<🎜>
{<🎜>
function T2()<🎜>
{<🎜>
echo 't2 is ok';<🎜>
}<🎜>
}<🎜>
?>
In index.php, we create other class instances through a factory class.
In the factory, there is an array $this->mClasses saved in the format of array("class name" => "class file path").
When we create a class instance through factory::create(), in create(), it will first detect whether the class exists. If it does not exist, the class file corresponding to the class will be included according to $this->mClasses. Then create and return an instance of this class.
In this way, we only need to include the factory class file into the executed script (such as index.php).
You may also notice these two lines of code in t1.inc.php.
The code is as follows
Copy code
global $config;
$config='surfchen';
Why do we need global? Because t1.inc.php is included in factory::create, the variables in the t1 file will default to create's function-level variables. So we need to make the variables (such as $config) global so that index.php can access them.
Run index.php and it will output
代码如下
复制代码
name::T1surfchen
http://www.bkjia.com/PHPjc/628801.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628801.htmlTechArticleThis article will introduce to you a new thing in php5, which is the factory mode. Here is my commonly used factory mode Some study notes of
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