Автор оригинала: David Wong.
Разберитесь с некоторыми демонстрациями и их собственным пониманием шаблона проектирования PHP, который написал Аарон Сарай. После прочтения, если вы обнаружите, что я понимаю это неправильно, пожалуйста, немедленно укажите, спасибо, что хлопаете по кирпичам, становитесь на колени и просите о порке
/**
*Builder mode
* -------------------------------------
** from description**
*
*The mother of builder design pattern is to eliminate the complex creation process of other objects,
*Using builder design patterns is not just a best practice
*In addition, when the construction and configuration methods of an object change, the repetitive code shall be reduced as much as possible
*
* =====================================
** application scenarios**
*
*Database interface class optimization base class
*
* -------------------------------------
*
* @version ${Id}$
* @author Shaowei Pu <[email protected]>
*///Now it's the case that you are a student personal file entry clerk. You see the entry class that your boss wrote many years ago when you were a wet baby
class Entering {
public $info = [];
public function setName( $name ){
$this->info['name'] = $name;
}
public function setOld( $old ){
$this->info['old'] = $old;
}
public function setGender( $gender ){
$this->info['gander'] = $gender;
}
}
// then
$worker = new Entering;
$worker->setName('jacky');
$worker->setOld('22');
$worker - > setgender ('male ');
// then这样就可以了,一个学生的完整信息就这么弄出来了
var_dump($worker->info);
/*
+----------------------------------------------------------------------
| array (size=3)
| 'name' => string 'jacky' (length=5)
| 'old' => string '22' (length=2)
|'gander' = > string 'male' (length = 3)
+----------------------------------------------------------------------
*/
//But you who love tossing never give up any chance. Since you learn the builder mode, you always have a desire to move, so you have the following categories
class EnteringBuilder{
protected $_baseObject = null;
protected $_newInfo = [] ;
public function __construct( array $info ){
$this->_baseObject = new Entering;
$this->_newInfo = $info;
}
public function build(){
$this->_baseObject->setName ( $this->_newInfo['name']);
$this->_baseObject->setOld ( $this->_newInfo['old']);
$this->_baseObject->setGender( $this->_newInfo['gander']);
}
public function getInfo(){
return $this->_baseObject->info;
}
}
// then这样
$new_worker = new EnteringBuilder([
'name' => 'lucy',
'old' => 22,
'gander' = > female ',
]);
$new_worker->build();
// then这样就可以了,一个学生的完整信息就这么弄出来了
var_dump($new_worker->getInfo());
/*
+----------------------------------------------------------------------
| array (size=22)
| 'Host' => string 'localhost' (length=9)
| 'Db' => string 'sys' (length=3)
| 'User' => string 'mysql.sys' (length=9)
| 'Select_priv' => string 'N' (length=1)
| 'Insert_priv' => string 'N' (length=1)
| 'Update_priv' => string 'N' (length=1)
| 'Delete_priv' => string 'N' (length=1)
| 'Create_priv' => string 'N' (length=1)
| 'Drop_priv' => string 'N' (length=1)
| 'Grant_priv' => string 'N' (length=1)
| 'References_priv' => string 'N' (length=1)
| 'Index_priv' => string 'N' (length=1)
| 'Alter_priv' => string 'N' (length=1)
| 'Create_tmp_table_priv' => string 'N' (length=1)
| 'Lock_tables_priv' => string 'N' (length=1)
| 'Create_view_priv' => string 'N' (length=1)
| 'Show_view_priv' => string 'N' (length=1)
| 'Create_routine_priv' => string 'N' (length=1)
| 'Alter_routine_priv' => string 'N' (length=1)
| 'Execute_priv' => string 'N' (length=1)
| 'Event_priv' => string 'N' (length=1)
| 'Trigger_priv' => string 'Y' (length=1)
+----------------------------------------------------------------------
*/Оригинал: “https://developpaper.com/php-design-pattern-builder-pattern/”