Design Mode (II): Builder mode-Creation Mode

Source: Internet
Author: User

1. Definition

Separates the construction of a complex object from its representation so that different representations can be created during the same construction process. Such a design pattern is called the builder pattern.

  

2. Applicable scenarios

1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods. 2. When the constructor must allow different representations of the constructed object.

  

3. Comment

1. It allows you to change the internal representation of a product. The builder object provides the Guide with an abstract interface for constructing products. This interface allows the generator to hide the representation and internal structure of this product. It also hides how the product is assembled. Because the product is constructed through abstract interfaces, all you need to do when changing the internal representation of the product is to define a new generator. 2. It separates the constructor code from the representation code. The builder mode encapsulates the creation and representation of a complex object to improve the modularity of the object. The customer does not need to know all information about the classes that define the internal structure of the product. These classes are not found in the builder interface. Each concrete builder contains all the code for creating and assembling a specific product. The Code only needs to be written once. Different ctor can reuse it to construct different products based on the same part set. 3. It allows you to control the construction process in more detail. The builder mode is different from the product creation mode that generates products at once. It constructs the product step by step under the control of the Guide. The Guide retrieves the product from the generator only when the product is completed. Therefore, the builder interface can better reflect the product construction process than other creation modes. This allows you to control the building process more precisely, so that you can control the internal structure of the resulting product more precisely.

 

4. Mode Structure

1 builder: specify an abstract interface for each part of a product object. 2 concretebuilder: implements the builder interface to construct and assemble each part of the product, define and define the representation it creates, and provide an interface for retrieving the product. 3 Director: Construct an object using the builder interface. 4. Product: a complex object to be constructed. Concretebuilder creates an internal representation of the product and defines its assembly process, including the class that defines the components, including the interfaces that assemble these components into the final product.

Generator mode sequence diagram:

 

 

5. Case Study

(1) This mode is used to make children's meals in fast food restaurants. A typical children's meal includes a staple food, a complementary food, a drink, and a toy (such as a hamburger, fried chicken, cola, and toy car ). These can be different in different children's meals, but the process of combining them into children's meals is the same. The process is the same no matter whether the customer orders a hamburger, sanmingzhi or chicken. The staff at the counter directly put the staple food, complementary food and toys together. These are put in a bag. The drink is poured into the cup and placed outside the bag. These processes are the same in competing restaurants.

Implementation Process Diagram
Here we take the KFC store as an example to buy a package, as shown below:

Client: customer. If you want to buy a set of packages (including hamburgers, cola, and Fries), you can choose between 1 and 2.
Instructor role: cashier. Know what kind of package the customer wants to buy and tell the restaurant staff to prepare the package.
Builder role: restaurant staff. Prepare the specific package according to the cashier's requirements, and put the package into the hamburger, cola, and fries respectively.

 

6.Difference between builder mode and factory Mode

We can see that the builder model is very similar to the factory model. In general, the builder model only has one "director" role more than the factory model. In the class diagram of the builder mode, if the director class is regarded as the client for the final call, the rest of the diagram can be regarded as a simple factory model.

The builder mode is generally used to createMore complex objectsBecause the object creation process is more complex, the object creation process is independent to form a new class-the director class. In other words, the factory mode encapsulates all the object creation processes in the factory class, and the factory class provides the final product to the client. In the builder mode, the builder class generally only provides the construction of each component in the product class, and delivers the specific construction process to the director class. The Director is responsible for forming various components into products according to specific rules, and then delivering the group-created products to the client.

7. Reference

[1] gof: design mode.

[2] Baidu Encyclopedia: http://baike.baidu.com/view/799520.htm.

[3] http://hzylyl.blog.163.com/blog/static/33097164200802195910992.

[4] http://blog.csdn.net/hguisu/article/details/7518060.

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.