Benefits of parsing the PHP factory model

Source: Internet
Author: User
This article gives a detailed analysis of the benefits of the PHP factory model. if you need a friend, as the name suggests, the factory can process parts, the factory mode in the PHP program also has the same function, so that you can easily use a static factory method to instantiate a class. What are the advantages of doing so? My understanding of PHP design patterns is as follows:
In general, when instantiating a class, we will give it some parameters so that we can provide the expected results according to different parameters during its analysis.
For example, the following is a User class, which is very simple:

The code is as follows:


Interface IUser {
Function getName ();
Function getAge ();
}
Class User implements IUser {
Protected $ _ name;
Protected $ _ age;
Function _ construct ($ name, $ age ){
$ This-> _ name = $ name;
$ This-> _ age = (int) $ age;
}
Function getName (){
Return $ this-> _ name;
}
Function getAge (){
Return $ this-> _ age;
}
}
?>


If we want to instantiate this class, we need to do this:
$ U = new User ('xiaoming ', 19 );
Generally, if this class is rarely used, it will have no significant impact and will be very good.
Suddenly I want to add a category for this class, and put James into the student group. it is very easy to modify the class code, however, if this class is instantiated multiple times in many file locations before we want to modify it, it will be very cumbersome to add a parameter to it, because we need to replace it:
$ U = new User ('xiaoming ', 19, 'Student ');
Of course, we can also set the default value in the _ construct function to avoid this repetitive work, but in fact this is not good from the code elegance perspective, suppose we have a factory method that can correspond to a set of parameters through an identifier, and store this parameter in a text document or directly in the factory class as an array, when we call the User class, it will become much easier. even if we need to increase or decrease the parameter attributes, we do not need to replace code everywhere, the following is a factory class (you can also directly store the method in the User class)

The code is as follows:


Interface IUser {
Function getName ();
Function getAge ();
}
Class User implements IUser {
Protected $ _ group;
Protected $ _ name;
Protected $ _ age;
Function _ construct ($ name, $ age, $ group ){
$ This-> _ group = $ group;
$ This-> _ name = $ name;
$ This-> _ age = (int) $ age;
}
Function getName (){
Return $ this-> _ name;
}
Function getAge (){
Return $ this-> _ age;
}
}
Class Fuser {
Private static $ group = array (
Array ('xiaoming ', 19, 'Student '),
Array ('Wang ', 19, 'students ')
);
Static function create ($ id ){
List ($ name, $ age, $ group) = self: $ group [(int) $ id];
Return new User ($ name, $ age, $ group );
}
}
Echo Fuser: create (0)-> getName ();


The result should be "James ".

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.