PHP Builder Builder Mode Learning notes

Source: Internet
Author: User

Idea: Since the interstellar picture is made up of several parts: the map (the terrain and the minerals), the building, the troops. Then we think of them as parts, assembled to be the final product (the whole picture).


Builder (Builder) mode example:

The code is as follows Copy Code

<?php

Specification of interfaces for manufacturing individual parts

Interface Builder

{

Manufacturing Map Parts

Public function Buildmappart ();

Manufacturing Building Parts

Public function Buildbuildingpart ();

Manufacturing Force Parts

Public function Buildarmypart ();

Assemble parts

Public function GetResult ();

}

Actual builder class, such as initialization of a task

Class ConcreteBuilder implements Builder

{

Manufacturing Map Parts

Public Function Buildmappart ()

{

Draw a map based on the task set.

echo ' map parts n ';

}

Manufacturing Building Parts

Public Function Buildbuildingpart ()

{

Draw the construction according to the task, including the player's and Enemy's

Echo ' Building parts n ';

}

Manufacturing Force Parts

Public Function Buildarmypart ()

{

Draw the force according to the task, including the player's and Enemy's

Echo ' Force parts n ';

}

Assemble parts

Public Function GetResult ()

{

Stacking and processing all things to form an initialization screen

Echo ' assemble parts n ';

}

}

Monitoring class, which is the class that controls the drawing process

Class Director

{

Private properties, determining the builder used

Private $builder;

constructor method, parameter is the selected builder object

Public function __construct ($builder)

{

Determine which builder to use

$this->builder = $builder;

}

Responsible for the method of construction process, call Builder object method, make all parts

Public Function Buildeallpart ()

{

Manufacturing Map Parts

$this->builder->buildmappart ();

Manufacturing Building Parts

$this->builder->buildbuildingpart ();

Manufacturing Force Parts

$this->builder->buildarmypart ();

}

}

Assuming that the actual builder object we need is initialized according to the task

$concreteBuilder = new ConcreteBuilder ();

Initialize a supervised object

$director = new Director ($concreteBuilder);

Make all parts

$director->buildeallpart ();

Finally let the builder assemble the parts and create the picture

$concreteBuilder->getresult ();

?>

Usage Summary: The builder model can separate the process from the details and perform the duties.


Implementation summary: Need a builder interface or abstract class, responsible for the specification of various methods, such as the above builder. Then let the actual builder class implement all the methods, such as the concretebuilder above. At the same time need to be responsible for process management of the supervision class, such as the above Director, responsible for calling the builder of the various parts manufacturing methods. And finally let the builder assemble all the parts.

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.