Introduction to the factory pattern in php design pattern _ PHP Tutorial

Source: Internet
Author: User
This section describes the factory mode in php design mode. How can you easily create such a complex object, that is, the operation does not need to be pasted and copied? Solution: build a factory (a function or a class method) to create new problems

How can you easily create such a "complex" object, that is, the operation does not need to be pasted and copied?

Solution

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

Code:

The code is as follows:


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


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

The code is as follows:


$ Connection = & create_connection ();


The code snippets of the latter are concentrated in the create_connect () factory connected to the database, just as we mentioned earlier, make the process of creating a database connection a simple operation-just like a new operation. The advantage of the factory mode lies in the creation of objects. Its task is to encapsulate the object creation process and return a new class.

Do you want to change the object structure and object creation method? You only need to select an object factory, and only one change to the code is required. (The factory model is so powerful that it is at the bottom of the application, so it will not stop appearing in many other complex models and applications .)

Sample code

The factory mode encapsulates the object creation process. You can create an object factory or an additional factory class on the object itself-depending on your specific application. Let's look at an example of a factory object.

In the following code, we find that the database connection part has appeared repeatedly:

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 not good? There are too many database connection parameters. setting these parameters as constants means you define them and assign values to them in a unified manner. Obviously, this is not appropriate:

You can easily change the database connection parameters, but you cannot add 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, for example, PostgresqlConnection.
In this way, it is difficult to test and verify the status of the connected object separately.
Using the factory design pattern, the code will be greatly improved:

The code is as follows:


Class Product {
Function getList (){
$ Db = & $ this-> _ getConnection ();
//...
}
Function & _ getConnection (){
Return new MysqlConnection (DB_USER, DB_PW, DB_NAME );
}
}

In the previous class, many methods that call new MysqlConnection (DB_USER, DB_PW, DB_NAME) are now concentrated on the _ getConnection () method.

How can you easily create such a "complex" object, that is, the operation does not need to be pasted and copied? The solution is to create a factory (a function or a class method) to create a new...

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.