SoFunction
Updated on 2025-03-02

Simple example of the builder pattern definition and usage of PHP design pattern

This article describes the builder model of PHP design pattern. Share it for your reference, as follows:

Builder Mode:

Separate the creation process and representation of complex objects (well, I don't understand what it means, either).

Let me understand something humans can understand:

  • 1. In the eyes of the client, what is needed is only the instantiated class object (in most cases, the attributes of the class are required).
  • 2. When the class is available under the traditional method, it is generally directly passednew class()Instantiate it directly through$obj->set1()Build attribute 1,$obj->set2()Build attribute 2,$obj->set3()Build attribute 3. . .
  • 3. The traditional method has a big disadvantage: when our class changes, we need to modify it a lot, such as adding it to file 1.$obj->set4(), add in file 2$obj->set4(), increase the workload a lot.
  • 4. The builder pattern will give the specific implementation class of the build object, encapsulate the instantiation process of the object creation and instantiation process in the builder class, 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 constructed object in the builder class.build()Method, this is not visible to the client, and the modified object is obtained without modification. All that is changed is the logical processing after the request object is modified.

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:
 An abstract interface defined to regulate specific builder classes
 */
interface Builder{
  public function buildHead();
  public function buildBody();
  public function getResult();
}
/*
 Specific builder:
 Used to implement specific builders
 */
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 specific builder classes to create product class instances
 */
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;

Running results:

Big Head Son is in great health

For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

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