PHP design Pattern Series-Builder Mode _php tutorial

Source: Internet
Author: User
What is Builder mode
The builder pattern is primarily designed to eliminate the complexity of other object creation processes.
Design Scenarios
There is a user's UserInfo class, create this class, need to create user's name, age, money and other information in order to obtain the user specific information results.
Create a Userinfobuilder user builder class that encapsulates the UserInfo complex creation of names, ages, and money to simplify the creation of user classes
Code: UserInfo class, creating a UserInfo class is complex and painful.
[PHP] View plaincopyprint?
Builder mode, designed to eliminate the complexity of other object creation processes

/* Describe a user's class, including user name, age, Money */
Class UserInfo {

protected $userName = ";
protected $userAge = ";
protected $userMoney = ";

Public Function Setusername ($userName) {
$this->username = $userName;
}

Public Function Setuserage ($userAge) {
$this->userage = $userAge;
}

Public Function Setusermoney ($userMoney) {
$this->usermoney = $userMoney;
}

Public Function GetPeople () {
echo "This person's name is:". $this->setusername. ', Age is: '. $this->userage. ', Money: '. $this->usermoney;
}
}
/* instantiation, and when creating this user, is very painful, need to set user name, age and Money */
$peopleInfo = Array (
' UserName ' = ' initphp ',
' Userage ' = 28,
' Usermoney ' = ' 100 Yuan '
);
$UserInfo = new UserInfo;
Below need step by step set user information, can get user details, process tangled and painful
$UserInfo->setusername ($peopleInfo [' userName ']);
$UserInfo->setuserage ($peopleInfo [' userage ']);
$UserInfo->setusermoney ($peopleInfo [' Usermoney ']);
$UserInfo->getpeople ();
Code: Userinfobuilder User Information Builder class that encapsulates the UserInfo creation process and is comfortable for developers to use.
[PHP]
Builder mode, designed to eliminate the complexity of other object creation processes
Include ("userinfo.php");
Class Userinfobuilder {
protected $obj;

Public Function __construct () {
$this->obj = new UserInfo;
}

Public Function Buildpeople ($peopleInfo) {
$this->obj->setusername ($peopleInfo [' userName ']);
$this->obj->setuserage ($peopleInfo [' userage ']);
$this->obj->setusermoney ($peopleInfo [' Usermoney ']);
}

Public Function GetPeople () {
$this->obj->getpeople ();
}
}

/* The creation process is encapsulated and the user is simple to use */
$peopleInfo = Array (
' UserName ' = ' initphp ',
' Userage ' = 28,
' Usermoney ' = ' 100 Yuan '
);
$UserInfoBuilder = new Userinfobuilder;
$UserInfoBuilder->buildpeople ($peopleInfo); Direct a Build
$UserInfoBuilder->getpeople ();
Author: initphp

http://www.bkjia.com/PHPjc/478143.html www.bkjia.com true http://www.bkjia.com/PHPjc/478143.html techarticle What is the builder Pattern Builder pattern is primarily to eliminate the complexity of other object creation processes. The design scenario has a user's UserInfo class that creates this class and needs to create the user's last name ...

  • 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.