Builder Mode)

Source: Internet
Author: User

Builder Mode)
Definition

(After reading the Chinese and English versions of this book, I found that I have learned nine tricks about the builder model, So I went online to search for others' analysis, therefore, this chapter mostly collects data from the Internet. In addition, code examples indicate that the examples in this book are inappropriate and have changed, so this chapter has little to do with this book .)

The builder mode separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. The Builder model has two most important roles: the abstract Builder and the ctor. The Builder knows How to build the product, and the mentor knows What to create) the example of building a house in the product and builder mode is quite vivid. The building process varies widely, including the building shape, the number of floors, the number of internal rooms, and the decoration of the room. However, for the builder, the abstract building process is definite, building a building usually includes the following steps: (1) piling and building a foundation (2) building a framework. The builder model is essentially the same as the Building: the process remains unchanged, but the specific details of each process are constantly changing. The benefit of the builder model is that it ensures that the process will not change, that is, the process will not be increased, nor will it be omitted, or that the Process Order is incorrect. This is very important.

 

In addition to the preceding two roles, the builder mode also includes Product and ConcreteBuilder. The general relationship is as follows:

The roles are described as follows:

 

Builder: provides an abstract interface to standardize the construction of each component of a product object. This interface specifies which parts of a complex object should be created, and does not involve the creation of specific object parts. ConcreteBuilder: implements the Builder interface to create different parts of complex objects based on different business logic. After the construction process is complete, provide the product instance. Director: Call a specific builder to create various parts of a complex object. The supervisor does not involve the information of a specific product. He is only responsible for ensuring that all parts of the object are completely created or created in a certain order. Product: the complex object to be created.

 

 

The abstract builder abstracts a fixed method, and then the specific builder implements its own methods respectively. The instructor builds the product by running construct, and finally obtains the product by getResult. The Call Sequence diagram of this process can be summarized:

From the time sequence diagram, we can see that the products required for the creation can not be obtained immediately. The product needs to be obtained in the last step, which is different from the product obtained immediately in the factory mode. In fact, there are many similarities between the factory mode and the builder mode, both of which encapsulate the creation of objects to make the creation of products transparent, but there are also differences, as shown in the following table:

 

Builder mode and abstract factory Mode
Builder Mode Abstract Factory Model
Construct a complex object Construct simple or complex objects
Build an object in multiple steps Build objects directly step by step
Multiple methods to build an object Only one way to build objects
The created object is returned only in the last step of multiple steps. Returns the constructed object immediately.
Focus on creating an object Focusing on a series of objects

 

In practice, the earlier mode is mainly used:

 

When creating complex objects, the building sequence of the internal components of these objects is stable, but the internal components of the objects are subject to complex changes. The algorithm of the complex object to be created is independent of the components of the object and the Assembly Method of the components.

 


Code example: this example uses robot manufacturing to demonstrate the construction mode. Assume that the manufacturing process of a robot is: Head creation, Body Building, hand building, and foot building, in this way, we can abstract four manufacturing methods of robots. The manufacturing sequence is considered head, body, and hands and feet. Based on this scenario, we can use the builder mode to create different robots. In this example, the specific robots to be manufactured are Superman robots and Batman robots, so the builder class can be designed like this. The abstract builder class is defined as follows:
# Import
 
  
// 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; // The abstract construction method-(void) makeHeader;-(void) makeBody;-(void) makeHand;-(void) makeFeet;-(Roboter *) getRoboter; @ end
 
The implementation part of the abstract class can be empty. However, in this example, the Implementation part is used as the default. Its implementation is as follows:
# Import Roboter. h // the default method implemented by the 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
The two specific builders need to inherit the abstract construction class, and then reload the abstract manufacturing methods. Their implementations are as follows:
// Specific builder-Superman robot producer // method of heavy load construction @ 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

// Specific builder-Batman robot producer // method of heavy load construction @ 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
Both builders implement the manufacturing of each part of the robot in their own implementations, and then they need to create a ctor to understand the abstract maker class, the only method is to run the following command to ask the Creator to start manufacturing and then call the abstract manufacturing method. Its implementation is as follows:
@ Implementation RobotMaker-(void) make :( Roboter *) roboter {// director- // fixed process of the production robot [roboter makeHeader]; [roboter makeBody]; [roboter makeHand]; [roboter makeFeet];} @ end
In this way, the client can use the builder mode to create the desired robot by specifying the constructed robot type. The call code of the client 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: header-> % @ body-> % @ hand-> % @ feet-> % @, roboter. header, roboter. body, roboter. hand, roboter. feet); // you can obtain the product you want to build using the obtained method. // [roboter getRoboter];} @ end
The client creates a mentor and a specific builder, creates an object through commands under the mentor, and obtains the created object through the abstract method getResult.
After summing up the above learning, we can find that the product was created by the client at the beginning, but the product cannot be called a real product at this time. We have to assemble and process the product step by step, the processing part is separated into the builder's specific class for implementation, so that the internal appearance of the product is separated from the product generation process, this allows a building process to generate product objects with different internal representations, greatly reducing the ease of creating complex objects. Benefits of using the builder mode include:

 

By using the builder mode, the client does not have to know the details of the product composition. The specific builder classes are independent of each other, which is very beneficial to system expansion. Since the specific builder is independent, the construction process can be gradually refined without any impact on other modules.

 


 

 

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.