Builder Mode (creator)

Source: Internet
Author: User

Defined

(I read the book after the two version of the Chinese and English found that the builder model to understand the nine out of the box, so directly to the online search for other people's analysis, so the majority of this chapter is based on the information collected on the Internet, in addition, the code example shows that the example of this book is inappropriate, also changed, so this chapter and the )

The builder pattern separates the construction of a complex object from its representation, allowing the same build process to create different representations . The two most important roles of the builder model: the abstract builder and the director, in which the builder knows how to build the product, and the instructor knows what to make, The builder model is more vivid with examples of building houses. Buildings are different, the shape of the building, number of floors, the number of internal rooms, room decoration and so on are not the same, but for the builders, the abstract construction process is determined, often building a building consists of the following steps: (1) Piling, building the Foundation (2) to establish a framework. The nature of the builder model is consistent with building buildings: the process remains the same, but the specifics of each process implementation are constantly changing. The benefit of the builder model is that it is important to ensure that the process does not change, that the process does not increase, and that it does not omit or produce a process sequence error.


In addition to the two characters mentioned above, the builder model includes the product and the Concrete Builder (ConcreteBuilder), whose approximate relationship is as follows:


The specific roles and profiles of these characters are as follows:

    • Builder: An abstract interface is given to standardize the construction of each component of the Product object. This interface specifies which parts of a complex object to create, and does not involve the creation of specific object parts.
    • ConcreteBuilder: Implements the Builder interface, which materializes the creation of parts of complex objects for different business logic. After the completion of the construction process, provide an example of the product.
    • Director: Call concrete builders to create parts of complex objects that do not involve specific product information in the instructor and are responsible for ensuring that the parts of the object are created completely or in some order.
    • Product: The complex object to create.


Abstract builders Abstract fixed methods, and then concrete builders implement their own methods, while the instructor builds the product by ordering construct and finally obtains the built product by means of getresult. The call sequence diagram of the process can be summed up as:


As can be seen from the timing diagram, the product that is needed is not immediately available and requires the final step to obtain the desired product, which is different from the immediate acquisition of the factory model. In fact, there are many similarities between the factory model and the builder pattern, which are the creation of the encapsulated object, which makes the product creation transparent, but there are also different points, as shown in the following table:

Builder mode and abstract Factory mode
Builder Mode Abstract Factory mode
Building a complex object Building Simple or complex objects
Building one object in multiple steps Build objects directly in one step
Multiple ways to build an object Only one way to build an object
Step over the last step to return the created object Immediately return to the constructed object
Focus on the creation of an object Emphasize a range of objects

In practice, the main use of the earlier model of the situation is:

    1. When creating complex objects, the construction order between the internal components of these objects is stable, but the internal components of the objects face complex changes.
    2. The algorithm of the complex object to be created, independent of the component of the object, and also independent of the Assembly method of the component.


Code examples This example takes the robot manufacturing to demonstrate the construction mode, assuming that the robot manufacturing process is: Build head, build the body, build hands, build feet, so we can abstract out the robot's four manufacturing methods, the manufacturing sequence is assumed to be head, body, hands and feet, based on such a scene, We can use the Builder mode to create different robots. The specific need for this example is to make robots for Superman robots and Batman Robots, so the class on builders can be designed like this. The class of the abstract Builder is defined as follows:
#import <foundation/foundation.h>//Abstract builder @interface roboter:nsobject{    @protected    nsstring* _header;    nsstring* _body;    nsstring* _hand;    nsstring* _feet;} @property (nonatomic, readonly) nsstring* header; @property (nonatomic, readonly) nsstring* body; @property (nonatomic, ReadOnly) nsstring* hand, @property (nonatomic, readonly) nsstring* feet;//Abstract Construction Method-(void) makeheader;-(void) makebody ;-(void) makehand;-(void) makefeet;-(roboter*) getroboter; @end
Because it is an abstract class, its implementation part can be empty, but this example uses the implementation as the default, and its implementation is as follows:
#import the default method implemented by the "Roboter.h"//Abstract builder @implementation roboter-(void) makeheader{    _header = @ "header";} -(void) makebody{    _body = @ "Body";} -(void) makehand{    _hand = @ "Hand";} -(void) makefeet{    _feet = @ "feet";} -(roboter*) getroboter{    return self;} @end
and two concrete builders need to inherit the abstract construction class, and then overload the abstract manufacturing method, the implementation is:
Concrete Builder-Superman robot manufacturer//heavy-duty construction method @implementation supermanroboter-(void) makeheader{    _header = @ "Super man Header";} -(void) makebody{    _body = @ "Super man Body";} -(void) makehand{    _hand = @ "super man Hand";} -(void) makefeet{    _feet = @ "Super man Feet";} @end

Concrete Builders-Batman robot manufacturer//heavy-duty construction method @implementation batmenroboter-(void) makeheader{    _header = @ "bat man header";} -(void) makebody{    _body = @ "bat man Body";} -(void) makehand{    _hand = @ "bat man Hand";} -(void) makefeet{    _feet = @ "bat man Feet";} @end
Each of the two builders implements the manufacturing of parts of the robot in their own implementation, and then the need to create a mentor (director), which understands the abstract producer class, whose only way is to require the creator to start manufacturing, and then invoke the abstract manufacturing method, which is implemented as follows:
@implementation robotmaker-(void) Make: (roboter*) roboter{    //director-guide//    fixed process for production robots    [Roboter Makeheader];    [Roboter Makebody];    [Roboter Makehand];    [Roboter Makefeet];} @end
Thus, the client can use the Builder mode to create the desired robot by specifying the type of robot being built, and the client's calling code is roughly as follows:
@implementation child-(void) wantroboternamed: (nsstring*) name{    //client-client    robotmaker* maker = [[ Robotmaker alloc] init];    roboter* roboter = nil;    if ([Name isequaltostring:@ "BatMan"])    {        Roboter = [[Batmenroboter alloc] init];    }    else if ([Name isequaltostring:@ "Superman"])    {        Roboter = [[Supermanroboter alloc] init];    }    Manufacturing robot    [maker Make:roboter];        NSLog (@ "Robboter Info: \ n"          "header,%@\n"          "body-   %@\n" "Hand-   %@\n          " " Feet-   %@\n ",          Roboter.header, Roboter.body, Roboter.hand, roboter.feet);        Products to be constructed using the acquisition method    //[Roboter getroboter];} @end
The client creates the instructor and the concrete builder, and then starts by directing the command to create the object, and finally getresult the created object through an abstract method.
Summary through the above learning, we can find that, in fact, the product was created by the client at the beginning, but at this time the product can not be called the real product, we also have to give the product step-by-step assembly processing, and the processing part is separated to the builder concrete class inside the realization, This enables the internal representation of the product to be separated from the product's production process., which enables a construction process to produce product objects with different internal representations,greatly reduces the creation of complex objects. The benefits of using the builder model are:

    • Using the builder mode allows the client not to know the details of the internal composition of the product.
    • The concrete builder classes are independent of each other and are very advantageous to the expansion of the system.
    • Because the concrete builders are independent, the construction process can be progressively refined without any impact on the other modules.


reference 1, reference 2

Builder Mode (creator)

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.