Home > Backend Development > PHP Tutorial > Introducing the Factory Pattern in PHP Design Patterns_PHP Tutorial

Introducing the Factory Pattern in PHP Design Patterns_PHP Tutorial

WBOY
Release: 2016-07-21 15:51:37
Original
846 people have browsed it

Question

How can you create such a "complex" object easily and conveniently without pasting and copying during operation?

Solution

Create a factory (a function or a class method) to create new objects. To understand the usefulness of factories, imagine the following differences...

code:

Copy code The code is as follows:

$connection =& new MySqlConnection($user, $password, $database);

... Make your code scalable and more concise...

Copy code The code is as follows:

$connection =& create_connection();

The latter The code snippets focus on the create_connect() factory that connects to the database. As just mentioned, the process of creating a database connection becomes a simple operation - just like the new operation. The advantage of the factory pattern lies in creating objects. Its task is to encapsulate the object creation process and then return a required new class.

Want to change the structure of the object and the way to create the object? You only need to select the object factory and change the code only once. (The function of the factory pattern is so powerful, it is at the bottom of the application, so it will continue to appear in many other complex patterns and applications.)

Sample Code

Factory Pattern Encapsulates the object creation process. You can create an object factory on the object itself or an additional factory class - it depends on your specific application. Let's look at an example of a factory object.

We found that in the following code, the database connection part appears repeatedly:

Copy the code The code is as follows:

// PHP4
class Product {
function getList() { $db =& new MysqlConnection(DB_USER, DB_PW, DB_NAME);
//...
}
function getByName($name) { $db =& new MysqlConnection(DB_USER, DB_PW, DB_NAME);
//...
}
//...
}
Why is this bad? Database connection parameters appear in too many places. When you set these parameters as constants, it means that you define them uniformly and assign values ​​to them. Obviously this approach is not very appropriate:

You can easily change the parameters for connecting to the database, but you cannot increase or change the order of these parameters unless you change all the connection code.
You cannot easily instantiate a new class to connect to another database, such as PostgresqlConnection.
This makes it difficult to test and verify the state of the connection object alone.
Using the factory design pattern, the code will be greatly improved:

Copy the code The code is as follows:

class Product {
function getList() {
$db =& $this->_getConnection();
//...
}
function &_getConnection() {
return new MysqlConnection (DB_USER, DB_PW, DB_NAME); on the _getConnection() method.




http://www.bkjia.com/PHPjc/319122.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/319122.htmlTechArticleQuestion: How can you create such a "complex" object easily and conveniently without pasting and copying during operation? Solution Method creates a factory (a function or a class method) to make new...
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