Builder Pattern (Builder For PHP)

Builder Pattern: A design pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations.

Design scene:

  1. There is a user's UserInfo class. To create this class, you need to create the user's name, age, hobbies and other information in order to obtain the user's specific information results.
  2. Create a UserBuilder user builder class, which encapsulates the complex creation name, age, hobbies and other operations of UserInfo, and simplifies the creation process of user classes

 

This is a user class

copy code
class UserInfo
{
    protected $_userName;
    protected $_userAge;
    protected $_userHobby;

    public function setUserName($userName)
    {
        $this->_userName = $userName;
    }

    public function setUserAge($userAge)
    {
        $this->_userAge = $userAge;
    }

    public function setUserHobby($userHobby)
    {
        $this->_userHobby = $userHobby;
    }

    public function getPeopleInfo()
    {
        echo   "<br>This person's name is:" . $this ->_userName . "<br>Age is:" . $this ->_userAge . "<br>Hobbies:" . $this -> _userHobby;
    }
}
copy code

At this time, we need to obtain the information of a user, and the process is as follows:

$modelUser = new UserInfo();
$modelUser->setUserName('松涛');
$modelUser->setUserAge('23');
$modelUser->setUserHobby('推理小说');
$modelUser->getPeopleInfo();

得到的结果是:

这个人的名字是:松涛
年龄为:23
爱好:推理小说

 

这时候创建一个用户建造者类

copy code
class UserBuilder
{
    protected $_obj;

    public function __construct()
    {
        $this->_obj = new UserInfo();
    }

    public function builderPeople($userInfo)
    {
        $this->_obj->setUserName($userInfo['userName']);
        $this->_obj->setUserAge($userInfo['userAge']);
        $this->_obj->setUserHobby($userInfo['userHobby']);
    }

    public function getBuliderPeopleInfo()
    {
        $this->_obj->getPeopleInfo();
    }
}
copy code

这个是将复杂的创建过程封装在了builderPeople这个方法里面。 接下来是创建对象:

copy code
$userArr = array(
    'userName' => '松涛',
    'userAge' => '23',
    'userHobby' => '推理小说');

$modelUserBuilder = new UserBuilder();
$modelUserBuilder->builderPeople($userArr);
$modelUserBuilder->getBuliderPeopleInfo();
copy code

输出结果为:

这个人的名字是:松涛
年龄为:23
爱好:推理小说

优点

  • 简化复杂对象构建,对象本身与创建过程解耦,无需知道具体的 
    内部实现细节,相同的创建过程可以创建不同的产品对象;
  • 建造者模式可以很好的将一个对象的实现与相关的“业务”逻辑分离开来,从而可以在不改变事件逻辑的前提下,使增加(或改变)实现变得非常容易。

缺点

  • 可能会产生多余的建造者和构建过程对象,消耗内存;
  • 不适用与内部建造顺序不稳定,变化复杂的对象,可能导致需要 
    创建很多具体建造者来实现这些变化。
  • 建造者接口的修改会导致所有执行类的修改。

以下情况应当使用建造者模式:

1、 需要生成的产品对象有复杂的内部结构。
2、 需要生成的产品对象的属性相互依赖,建造者模式可以强迫生成顺序。
3、 在对象创建过程中会使用到系统中的一些其它对象,这些对象在产品对象的创建过程中不易得到。

According to the above examples, we can get the effect of the builder mode:
1. The use of the builder mode makes the internal appearance of the product change independently. Using the builder pattern allows the client to not have to know the details of the internal composition of the product.
2. Each Builder is relatively independent and has nothing to do with other Builders (independent control logic).
3. The final product built by the pattern is easier to control.

 

The difference between the builder pattern and the factory pattern:

We can see that the builder mode is very similar to the factory mode. In general, the builder mode only has one more "director" role than the factory mode. In the class diagram of the builder pattern, if the director class is regarded as the final call client, then the rest of the diagram can be regarded as a simple factory pattern.

Compared with the factory pattern, the builder pattern is generally used to create more complex objects, because the object creation process is more complex, so the object creation process is independent to form a new class - the director class. That is to say, the factory pattern encapsulates the entire creation process of the object in the factory class, and the factory class provides the final product to the client; while in the builder pattern, the builder class generally only provides the construction of each component in the product class. The specific construction process is handed over to the director class. The director class is responsible for assembling each component into a product according to specific rules, and then delivering the assembled product to the client.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325849707&siteId=291194637