Home  >  Article  >  Backend Development  >  Advanced object-oriented design patterns: builder pattern

Advanced object-oriented design patterns: builder pattern

巴扎黑
巴扎黑Original
2017-05-20 15:04:491467browse

What is 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 scenario:

There is a user's UserInfo class. To create this class, you need to create the user's name, age, hobbies and other information to obtain the user's specific information. result.

Create a UserBuilder user builder class. This class encapsulates the complex creation of UserInfo's name, age, hobbies and other operations, simplifying the creation process of user classes.

This is a User class

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->_userName . "<br>年龄为:" . $this->_userAge . "<br>爱好:" . $this->_userHobby;
    }
}

At this time we need to obtain a user's information. The process is as follows:

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

The result obtained is:

The person's name is :Songtao
Age: 23
Hobbies: Mystery novels

Create a user builder class at this time

class UserBuilder
{    
      protected $_obj;    

      public function __construct()
    {        
            $this->_obj = new UserInfo();
    }   
    
     public function builderPeople($userInfo)
    {       
            $this->_obj->setUserName($userInfo[&#39;userName&#39;]);
            $this->_obj->setUserAge($userInfo[&#39;userAge&#39;]); 
            $this->_obj->setUserHobby($userInfo[&#39;userHobby&#39;]);
    }    
    
     public function getBuliderPeopleInfo()
    {        
            $this->_obj->getPeopleInfo();
    }
}

This will be complicated The creation process is encapsulated in the builderPeople method. The next step is to create the object:

$userArr = array(    &#39;userName&#39; => &#39;松涛&#39;,
    &#39;userAge&#39; => &#39;23&#39;,
    &#39;userHobby&#39; => &#39;推理小说&#39;);
    $modelUserBuilder = new UserBuilder();
    $modelUserBuilder->builderPeople($userArr);
    $modelUserBuilder->getBuliderPeopleInfo();

The output result is:

The person’s name is: Songtao
The age is: 23
Hobby: Mystery novels

Advantages:

The builder pattern can well separate the implementation of an object from the related "business" logic, so that the event logic can be used without changing the event logic. , making it very easy to add (or change) implementations.

Disadvantages:

Modification of the builder interface will lead to modifications of all execution classes.

The builder pattern should be used in the following situations:

1. The product object that needs to be generated has a complex internal structure.
2. The properties of the product objects that need to be generated depend on each other, and the builder pattern can force the generation order.
3. During the object creation process, some other objects in the system will be used, which are not easy to obtain during the creation of product objects.

Based on the above examples, we can get the effects of the builder pattern:
1. The use of the builder pattern allows the internal appearance of the product to change independently. Using the builder pattern eliminates the need for the client to know the details of the product's internal makeup.
2. Each Builder is relatively independent and has nothing to do with other Builders (independent control logic).
3. The final product built by the model is easier to control.

The difference between builder mode and factory mode:

We can see that builder mode and factory mode are very similar. Overall On the other hand, 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 client that is ultimately called, 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 complicated, the object creation process is separated to form a new class - Director kind. In other words, the factory pattern encapsulates the entire object creation process in the factory class, and the factory class provides the final product to the client; 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 configuring each component into a product according to specific rules, and then delivering the assembled product to the client.

The above is the detailed content of Advanced object-oriented design patterns: builder pattern. For more information, please follow other related articles on the PHP Chinese website!

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