PHP设计模式-Builder 建造者模式

整理一下自己的学习Aaron Saray 写的PHP设计模式一些demo和自己的理解。大佬看完如果发现鄙人理解有误请立即指出,感谢拍砖,跪求鞭打

/**
 * Builder  建造者模式
 * -------------------------------------
 * ** 来自说明 **
 *
 * 建造者设计模式的母的是消除其他对象的复杂创建过程,
 * 使用建造者设计模式不仅是最佳的做法
 * 而且在某个对象的构造和配置方法改变时尽可能地减少重复的代码
 * 
 * ===================================== 
 * ** 应用场景 **
 *
 * 数据库接口类 | 优化基类
 * 
 * -------------------------------------
 * 
 * @version ${Id}$
 * @author Shaowei Pu <54268491@qq.com>
 */

// 现在是这样一个情况,您是学生个人档案录入员,你看到了你们Boss 在许多年前,你还是一个乳臭未干的小孩时写的录入类 
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;
    }
}
// 然后
$worker = new Entering;
$worker->setName('jacky');
$worker->setOld('22');
$worker->setGender('男');
// 然后这样就可以了,一个学生的完整信息就这么弄出来了
var_dump($worker->info);
/* 
+----------------------------------------------------------------------
| array (size=3)
|   'name'      => string 'jacky' (length=5)
|   'old'      => string '22' (length=2)
|   'gander' => string '男' (length=3)
+----------------------------------------------------------------------
*/

// 但是爱折腾的你从来不会放弃任何机会,自从学习了建造者模式的你 总有种蠢蠢欲动,于是乎就有了下面的类
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;
    }
}

// 然后这样
$new_worker = new EnteringBuilder([
            'name'      => 'lucy',
            'old'      =>  22,
            'gander' => '女',
    ]);
$new_worker->build();
// 然后这样就可以了,一个学生的完整信息就这么弄出来了
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://segmentfault.com/a/1190000008442023
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞