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