Home  >  Article  >  Backend Development  >  PHP Design Pattern Series - Builder Pattern_PHP Tutorial

PHP Design Pattern Series - Builder Pattern_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:52:15935browse

What is Builder Mode
The builder pattern is mainly to eliminate the complex creation process of other objects.
Design scene
There is a user's UserInfo class. To create this class, you need to create the user's name, age, money and other information to obtain the user's specific information results.
Create a UserInfoBuilder user builder class. This class encapsulates the complex operations of creating UserInfo such as name, age, money, etc., simplifying the creation process of user classes
Code: UserInfo class. Creating the UserInfo class is complex and painful.
[php] view plaincopyprint?
//Builder pattern, the purpose is to eliminate the complex creation process of other objects

/* Class that describes a user, 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;
}  
}
/* It is very painful to instantiate and create this user. You need to set the user name, age and money*/
$peopleInfo = array(
'userName' => 'initphp',
'userAge' => 28,
'userMoney' => '100 yuan'
);
$UserInfo = new UserInfo;
//You need to set the user information step by step below to get the user details. The process is tangled and painful
$UserInfo->setUserName($peopleInfo['userName']);
$UserInfo->setUserAge($peopleInfo['userAge']);
$UserInfo->setUserMoney($peopleInfo['userMoney']);
$UserInfo->getPeople();
Code: UserInfoBuilder User information builder class, which encapsulates the creation process of UserInfo, making developers feel comfortable when using it
[php]
//Builder pattern, the purpose is to eliminate the complex creation process of other objects
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, making it easy for users to use */
$peopleInfo = array(
'userName' => 'initphp',
'userAge' => 28,
'userMoney' => '100 yuan'
);
$UserInfoBuilder = new UserInfoBuilder;
$UserInfoBuilder->buildPeople($peopleInfo); //Directly build
$UserInfoBuilder->getPeople();
Author: initphp

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478143.htmlTechArticleWhat is the builder pattern? The builder pattern is mainly to eliminate the complex creation process of other objects. Design scenario There is a user's UserInfo class. To create this class, you need to create the user's last name...
Statement:
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