Simple Example of Builder Pattern Definition and Usage of PHP Design Pattern

  • 2021-10-27 06:51:39
  • OfStack

This paper illustrates the builder pattern of PHP design pattern. Share it for your reference, as follows:

Builder mode:

Separate the creation process and presentation of complex objects (well, I can't understand what it means 1).

Something I can understand as a human:

1. In the client's view, only the instantiated class object is needed (in most cases, the attribute of the class is needed). 2. In the traditional way, when the class is available, 1 passes directly new class() Instantiate directly in the way of, and then pass the $obj->set1() Build Property 1, $obj->set2() Build Attribute 2, $obj->set3() Build property 3. . . 3. The traditional way has a big drawback: When our class changes, we need a lot of modifications, such as adding in file 1 $obj->set4() In document 2, add $obj->set4() , adding a lot of workload. 4. The Builder Pattern will give the concrete implementation class of the built object, encapsulate the instantiation process of the object creation in the Builder Pattern, and give a method to return the built object, and return the built object. 5. When the class changes, you only need to change the build() Method, which is not visible to the client, and the modified object is obtained without modification. What is changed is only the logical processing after the modification of the requirement object.

Code:


/**
*  Product category Person
*/
class Person
{
  public $_head;
  public $_body;
  public function setHead($head){
    $this->_head=$head;
  }
  public function getHead(){
    echo $this->_head;
  }
  public function setBody($body){
    $this->_body=$body;
  }
  public function getBody(){
    echo $this->_body;
  }
}
/*
 Abstract Builder: 
 Defined 1 Abstract interfaces for specifying concrete builder classes 
*/
interface Builder{
  public function buildHead();
  public function buildBody();
  public function getResult();
}
/*
 Specific builder: 
 Used to implement the concrete builder class 
*/
class ConcreteBuilder implements Builder{
  public $person;
  public $data;
  public function __construct($data){
    $this->person=new Person();
    $this->data=$data;
  }
  public function buildHead(){
    $this->person->setHead($this->data['head']);
  }
  public function buildBody(){
    $this->person->setBody($this->data['body']);
  }
  public function getResult(){
    return $this->person;
  }
}
/*
 Director category: 
 Used to call the concrete builder class to create a product class instance 
*/
class Director{
  public function __construct(ConcreteBuilder $builder){
    $builder->buildHead();
    $builder->buildBody();
  }
}
/*
 Client: 
 Logical processing according to requirements 
*/
$data=array(
  'head'=>' Big head son ',
  'body'=>' Great body '
  );
$builder=new ConcreteBuilder($data);
$director=new Director($builder);
$person=$builder->getResult();
echo $person->_head;
echo $person->_body;

Run results:

Big head son is in great health

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: