Question
How can you create such a "complex" object easily and conveniently without pasting and copying?
Solution
Create a factory (a function or a class method) to create new objects. To understand the usefulness of the factory, imagine the difference below...
Code:
Copy code The code is as follows:
$connection =& new MySqlConnection($user, $password, $database);
...Make your code extensible and more concise...
Copy the code The code is as follows:
$connection =& create_connection();
The latter code snippet focuses on connecting to the database The create_connect() factory, as just mentioned, makes the process of creating a database connection 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
The 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 of 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 status 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);
}
}
There are many calls to new MysqlConnection(DB_USER, DB_PW, DB_NAME) Methods are now concentrated on the _getConnection() method.
The above introduces the graduation project ppt format and introduces the factory mode in PHP design mode, including the contents of the graduation project ppt format. I hope it will be helpful to friends who are interested in PHP tutorials.