PHP Implementation Builder mode

Source: Internet
Author: User

Builder pattern: Separates the construction of a complex object from its representation so that the same build process can create different representations.

The builder pattern is a step-by-step creation of a complex object that allows the user to build them only by specifying the type and content of the complex objects, and the user does not need to know the specifics of the build in-house. Builder mode belongs to the object creation mode. Depending on the Chinese translation, the builder pattern can also be called the generator pattern.

(i) Why the builder model is needed

1, the production of objects requires complex initialization, such as assigning an initial value to a large set of class member attributes, and setting up other system environment variables. This initialization work can be encapsulated by using the builder mode.
2, the generation of objects can generate different roles depending on the order in which they were initialized or the data.

(ii) Builder model structure diagram

(iii) mode application

In many game software, the map includes the sky, ground, background and other components, personas including human body, clothing, equipment and other components, you can use the Builder model to design it, through different concrete builders to create different types of maps or people

(iv) Design examples

If we want to create a person class, we set the property by instantiating it differently, let them both a fast child, a knowledge deep elder

Classperson {Public $age;Public $speed;public $knowledge;}Abstract Builder ClassAbstractClassbuilder{Public $_person;PublicAbstractfunctionSetage();PublicAbstractfunctionSetspeed();PublicAbstractfunctionSetknowledge();Publicfunction__construct(Person $person) {$this->_person= $person; }PublicfunctionGetperson(){Return$this->_person; }}Elderly BuildersClassOlderbuiderExtendsbuilder{PublicfunctionSetage(){$this->_person->age=70; }PublicfunctionSetspeed(){$this->_person->speed="Low"; }PublicfunctionSetknowledge(){$this->_person->knowledge=' More '; }}Child BuildersClassChildbuiderExtendsbuilder{PublicfunctionSetage(){$this->_person->age=10; }PublicfunctionSetspeed(){$this->_person->speed="Fast"; }PublicfunctionSetknowledge(){$this->_person->knowledge=' Litte '; }}Building a commanding personClassdirector{Private $_builder;Publicfunction__construct(Builder $builder) { $this->_builder = $builder;} public function built() { $this->_builder->setage (); $this->_builder->setspeed (); $this->_builder->setknowledge ();}} //instantiation of an elder builder $oldb = new Olderbuider (new person);  Instantiation of a construction conductor $director = new Director ($oldB);  Directing the construction of $director->built (); //Get elder $older = $oldB->getperson (); Var_dump ($older);          
(v) Summary

When using builder mode, we split the process of creating a person instance into two steps.

The first step is to hand over to the builder of the corresponding role, such as the Elder Builder. This has the advantage of encapsulating the character's property settings, and we don't need to be in the new person because we're going to get an instance of the older role and write a bunch of $older->age=70 out there.

Another step is to give a construction conductor a built method that initializes the character by setting the age and then setting the speed order. In this example, of course, the order of initialization is irrelevant. But if the order of the initialization is different for a hamburger, or a map, there may be different results.

Perhaps, you will say, I directly set up is also very convenient AH. Yes, that's true for some situations. But if you think about it, I want to add a youth role now? What if I wanted to have three different orders for the construction to initialize now?

If you use the builder model, these two questions are simple and add a young person's role, then add a youth-year builder class. Initialize three different sequences, then add two construction methods to the command builder.

PHP Implementation Builder mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.