design mode--5. Builder mode

Source: Internet
Author: User

1. Mode motivation

Both in the real world and in software systems, there are some complex objects that have multiple components, such as automobiles, which include wheels, steering wheel, transmitter and many other components. For most users, it is not necessary to know the assembly details of these parts, but rather to use a complete car, which can be designed and described by the builder mode, which separates the parts from the assembly process, creating a complex object step by step. The user simply needs to specify the type of the complex object to be able to get the object without having to know the specifics of the construction inside.

In software development, there are also a lot of complex objects like automobiles, which have a series of member properties, some of which are member objects of reference types. And in these complex objects, there may be some restrictions, such as some properties are not assigned to a complex object can not be used as a complete product; Some attributes must be assigned in a certain order, one property may not be assigned before a value is assigned, and so on.

Complex objects are equivalent to a car to be built, and the properties of the object are equivalent to the parts of the car, and the process of building the product is equivalent to the assembly process. Because of the complexity of the assembly process, the assembly process of these components is often "externally" to an object called a builder, which is returned to the client by a complete product object that has been built, and the user does not have to care about the properties that the object contains and how they are assembled. This is the model motive of the builder pattern.

2. Pattern definition

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.

3. Pattern structure

The builder pattern contains the following roles:

Builder: Abstract Builder

ConcreteBuilder: Concrete Builders

Director: Conductor

Product: Role

4. Timing Diagram

5. Code Analysis
#include <iostream>#include"ConcreteBuilder.h"#include"Director.h"#include"Builder.h"#include"Product.h"using namespacestd;intMainintargcChar*argv[]) {ConcreteBuilder* Builder =NewConcreteBuilder ();    Director Director;    Director.setbuilder (builder); Product* PD =director.constuct (); PD-Show (); DeleteBuilder; DeletePD; return 0;}
#include"ConcreteBuilder.h"Concretebuilder::concretebuilder () {}concretebuilder::~ConcreteBuilder () {}voidConcretebuilder::buildparta () {M_prod->seta ("A Style");//different builders can achieve different product construction}voidCONCRETEBUILDER::BUILDPARTB () {M_prod-&GT;SETB ("B Style");}voidCONCRETEBUILDER::BUILDPARTC () {M_prod-&GT;SETC ("C Style");}
" Director.h " Director::D irector () {}director::~Director () {}product* director::constuct () {    m_ Pbuilder, Buildparta ();    M_pbuilder, BUILDPARTB ();    M_pbuilder, BUILDPARTC ();         return m_pbuilder->getresult ();} void Director::setbuilder (builder* buider) {    = buider;}

Operation Result:

6. Pattern Analysis

The abstract builder class defines how the product is created and how it is returned.

The builder model also introduces a Commander class director, which has two main functions: it isolates the customer from the production process, on the other hand it controls the product generation process. The conductor is programmed for the abstract builder, and the client simply needs to know the type of the builder to return a complete product object by invoking the builder's associated method with the command class.

In client code, there is no need to care about the specific assembly process of the Product object, just determine the type of the concrete builder, and the builder pattern separates the construction of the complex object from the performance of the object so that the same build process can create different performance.

7. Example

Example: KFC Package

The builder mode can be used to describe how KFC creates a package: The package is a complex object, it usually contains staple foods (such as hamburgers, chicken rolls, etc.) and beverages (such as fruit juice, cola, etc.) and other components, different packages have different components, and KFC waiter can according to customer requirements, the step by stage assembly of these components , construct a complete package and return it to the customer.

8. Advantages

In builder mode, the client does not have to know the details of the internal composition of the product, decoupling the product itself from the product creation process, allowing the same creation process to create different product objects.

Each concrete builder is relatively independent and not related to other concrete builders, so it is easy to replace concrete builders or add new concrete builders, and users can get different product objects using different concrete builders.

You can control the product creation process more finely. The creation of complex products is broken down into different methods, making the creation process clearer and easier to use to control the creation process.

Adding new concrete builders without modifying the original class library code, the Commander class for the Abstract builder class programming, system expansion convenient, in line with the "open and close principle."

9. Disadvantages

The builder model creates products that generally have a lot in common and have similar components, and if the differences between the products are large, they are not suitable for use in the builder mode, so their scope of use is limited.

If the internal variation of the product is complex, it may result in the need to define many concrete builder classes to achieve this change, resulting in a large system.

10. Applicable environment

The builder mode can be used in the following situations:

The product objects that need to be generated have complex internal structures that typically contain multiple member properties.

The properties of the product objects that need to be generated depend on each other and need to specify their build order.

The creation of an object is independent of the class that created the object. The leader class was introduced in builder mode, which encapsulates the creation process in the leader class, not the builder class.

Isolate the creation and use of complex objects, and allow the same creation process to create different products.

11. 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 characters.

12. Mode expansion

Simplification of builder mode:

Omit abstract builder role: If you only need a concrete builder in your system, you can omit the abstract builder.

Omit the Commander role: in the case of a concrete builder, if the role of the abstract builder has been omitted, the role of the commander can be omitted, allowing the builder role to play the role of the leader and the builder.

Comparison of builder and abstract factory models:

In contrast to the abstract factory model, the builder mode returns an assembled complete product, while the abstract Factory mode returns a series of related products that are located in different product hierarchy structures that form a product family.

In abstract Factory mode, the client instance of the chemical plant class, and then calls the factory method to obtain the desired product object, and in the builder mode, the client can not directly invoke the builder's related methods, but through the command class to guide how to build the object, including the object assembly process and construction steps, It focuses on constructing a complex object in one step, returning a complete object.

If the abstract factory model is regarded as a car parts production plant, producing a product family product, then the builder model is a car assembly plant, through the assembly of components can be returned to a complete car.

13. Summary

The builder pattern separates the construction of a complex object from its representation, allowing the same build process to 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.

The builder pattern consists of four roles: the abstract builder specifies an abstract interface for each part that creates a product object, and the concrete builder implements an abstract builder interface that implements the construction and assembly methods of each component, defines and clarifies the complex objects it creates, and provides a way to return the created complex product object The product role is a complex object constructed, consisting of multiple components, and the conductor is responsible for arranging the sequence of construction of complex objects, the relationship between the conductor and the abstract builder, and the construction and assembly method of the Builder object in its construct () construction method to complete complex objects.

A class of conductors was introduced into the structure of the builder pattern, which has two main functions: on the one hand it isolates the customer from the production process, on the other hand it controls the product generation process. The conductor is programmed for the abstract builder, and the client simply needs to know the type of the builder to return a complete product object by invoking the builder's associated method with the command class.

The main advantage of the builder pattern is that the client does not have to know the details of the internals of the product, decoupling the product itself from the product creation process, so that the same creation process can create different product objects, each individual builder is relatively independent, and is not related to the other concrete builders, Therefore, it is convenient to replace the concrete builders or add new concrete builders, conform to the "open and close principle", but also can control the product creation process more finely, the main disadvantage is that the product created by the builder pattern has more common, its components are similar, so its use range is limited, If the internal variation of the product is complex, it may result in the need to define many concrete builder classes to achieve this change, resulting in a large system.

Builder mode applicability includes: Product objects that need to be generated have complex internal structures that typically contain multiple member properties, the properties of the product objects that need to be generated depend on each other, the order in which they are generated, the creation of objects is independent of the class that created the object, and the creation and use of complex objects is isolated. and enables the same creation process to create different types of products.

design mode--5. 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.