This article mainly introduces the builder mode in the php design mode, and uses php to implement the builder mode, interested partners can refer to the builder model to split the internal appearance of a product and the production process of the product, so as to generate products with different internal appearances.
I. Builder mode structure
II. main roles in Builder mode
Abstract Builder role:Define an abstract interface to standardize the construction of each component of the product (that is, to standardize the implementation of specific builder methods ). The standard method must include the construction method and result return method.
Specific builder role:Implement the methods defined by the abstract builder role. The specific builder is highly correlated with the business logic, and the application will eventually create a product according to the business logic by calling the construction method implemented in this role, return the created product instance using the result return method after the build is complete. It is generally created externally by a customer or an abstract factory.
Ctor role:This role is used to call a specific builder role to build a product. There is no direct relationship between the director and the product class, and the specific abstract role is used to talk with the product class.
Product role:The complex object created by the builder under the guidance of the instructor
The director's role deals directly with the client. it understands the client's business logic, splits the client's request for creating a product into a product component, and then calls a specific product role to perform the construction operation. It separates the client from the specific builder.
III. Advantages and disadvantages of Builder mode
Advantages of the Builder mode: The Builder mode can well separate the implementation of an object from the related "business" logic, so that the event logic can be kept unchanged, makes it easy to add (or change) implementations.
Disadvantages of Builder mode: Modification of Builder interface will lead to modification of all execution classes.
IV. use scenarios and effects of the Builder mode
The builder mode should be used in the following situations:
1. The product objects to be generated have a complex internal structure.
2. the properties of the product objects to be generated are mutually dependent. The Builder mode can force the generation order.
3. some other objects in the system will be used during object creation. these objects are not easy to obtain during product object creation.
The builder mode has the following effects:
1. the builder mode enables independent changes to the internal appearance of the product. By using the builder mode, the client does not have to know the details of the product composition.
2. Each Builder is relatively independent of other builders.
3. the final product built by the model is easier to control.
V. Builder mode and other modes
Abstract factory mode (abstract factory mode ):In the abstract factory model, a complete product object is returned every time a Factory object is called, and the client may assemble these products into a larger and more complex product, or not. The builder mode is different. it creates a complex product at 1.1 points, and the product assembly process occurs within the builder. The difference between the two lies in whether there is an assembly process and where the assembly process occurs. These two design patterns can be used together, and the client indirectly calls the factory role of another abstract factory model by calling one build role. Factory models return parts of different product families, while builder models assemble them.
Strategy mode ):The builder mode is very similar to the policy mode in structure. In fact, the builder mode is a special case of the policy mode. The difference between the two lies in their different intentions. The builder mode acts on creating new objects at on the client, while the policy mode aims to provide abstract interfaces for algorithms.
Builder mode and Template method mode:The builder mode can evolve to the template method mode after degradation and loss of the director role (the algorithm implementation of the construction process will be placed in the construction role ).
Builder mode and synthesis mode: the synthesis mode describes the composition structure of an object tree, while the builder mode can be used to describe the generation process of the object tree.
The above four points are from Java and mode.
VI. Builder mode PHP example
<? Php/*** Product ** here is only a string in the Product class to demonstrate the Product */class Product {/*** Product component set */private $ _ parts; public function _ construct () {$ this-> _ parts = array ();} public function add ($ part) {return array_push ($ this-> _ parts, $ part) ;}public function show () {echo "the product include:"; array_map ('printf', $ this-> _ parts );}} /*** abstract Builder */abstract class Builder {/*** product part construction method 1 */public abstract function buildPa Rt1 ();/*** product part construction method 2 */public abstract function buildPart2 ();/*** product return method */public abstract function getResult ();} /*** specific Builder */class ConcreteBuilder extends Builder {private $ _ product; public function _ construct () {$ this-> _ product = new Product ();} public function buildPart1 () {$ this-> _ product-> add ("Part1");} public function buildPart2 () {$ this-> _ product-> add ("Part2");} public funct Ion getResult () {return $ this-> _ product;}/*** */class Director {public function _ construct (Builder $ builder) {$ builder-> buildPart1 (); $ builder-> buildPart2 () ;}} class Client {/*** Main program. */public static function main () {$ buidler = new ConcreteBuilder (); $ director = new Director ($ buidler); $ product = $ buidler-> getResult (); $ product-> show () ;}} Client: main () ;?>
The above is the code that uses php to implement the builder mode. There are also some concepts about the builder mode, and I hope it will be helpful for everyone's learning.