Design Mode 5: builder Pattern)

Source: Internet
Author: User

From: http://www.cnblogs.com/zhenyulu/articles/37378.html

Reference: http://www.dofactory.com/Patterns/PatternBuilder.aspx

1. Builder Mode

Definition: Separate the construction of a complex object from its representation so that the same construction process can create different representations.

The builder model can separate the internal appearance of a product from the product generation process, so that a building process can generate product objects with different internal appearances.

Object Construction

In some cases, an object has some important properties and cannot be used as a complete product until they have no proper values. For example, an email contains the sender address, recipient address, subject, content, and appendix. However, this email cannot be sent before the minimum recipient address is assigned a value.

In some cases, some properties of an object must be assigned values in a certain order to make sense. Before a property is assigned a value, another property cannot be assigned a value. These situations make the construction of nature involve complex business logic.

At this time, this object is equivalent to a product to be built, and these properties of the object is equivalent to the parts of the product, the process of building the product is the process of combining Parts. Due to the complex process of assembling parts, the combination process of these parts is often "Externalized" into an object called builder, the builder returned the product object to the client after all parts have been built.

Naming considerations

The reason why "Builder" is used instead of "Builder" is that "Builder" is more appropriate to "CREATE" or "generate" because it is used to produce products with parts.

Ii. Builder mode structure:

 

Builder role: provides an abstract interface to regulate the construction of each component of a product object. Generally, this interface is independent from the business logic of the application. The concretebuilder role is used to directly create a product object. The method required by the specific builder class to implement this interface: one is the construction method, and the other is the result return method.

Concrete builder role: this role is played by classes closely related to applications that create product instances under application calls. The main tasks completed by this role include:

  • Implements the interfaces provided by the builder role and completes the process of creating product instances step by step.
  • After the construction process is complete, provide the product instance.

Ctor role: the class that acts as the role calls a specific builder role to create a product object. The director does not have the specific knowledge of the product class. What really possesses the specific knowledge of the product class is the specific builder object.

Product role: a product is a complex object during construction.

The mentor role is a role that deals with clients. The Director role divides the client product creation request into the construction request for each part, and then delegates these requests to the specific builder role. The specific builder role is used for specific construction, but is not known to the client.

3. program example:

This program demonstrates the builder Mode Step by step to complete the complex component product process. You can control the generation process and generate different objects.

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. collections; </P> <p> namespace building <br/> {<br/> // builder pattern -- Structural example </P> <p> // "director" <br/> class director <br/>{< br/> // methods <br/> Public void construct (Builder) <br/>{< br/> builder. buildparta (); <br/> builder. buildpartb (); <br/>}</P> <p>/"Builder" <br/> abstract class builder <br/>{< br/> // methods <br/> Abstract Public void buildparta (); <br/> Abstract Public void buildpartb (); <br/> Abstract Public Product getresult (); <br/>}</P> <p> // "concretebuilder1" <br/> class concretebuilder1: builder <br/> {<br/> // fields <br/> private product; </P> <p> // methods <br/> override public void buildparta () <br/>{< br/> product = new product (); <br/> product. add ("parta"); <br/>}</P> <p> override public void buildpartb () <br/>{< br/> product. add ("partb"); <br/>}</P> <p> override public product getresult () <br/>{< br/> return product; <br/>}</P> <p> // "concretebuilder2" <br/> class concretebuilder2: builder <br/> {<br/> // fields <br/> private product; </P> <p> // methods <br/> override public void buildparta () <br/>{< br/> product = new product (); <br/> product. add ("partx"); <br/>}</P> <p> override public void buildpartb () <br/>{< br/> product. add ("party"); <br/>}</P> <p> override public product getresult () <br/>{< br/> return product; <br/>}</P> <p>/"product" <br/> class product <br/>{< br/> // Fields <br/> arraylist parts = new arraylist (); </P> <p> // methods <br/> Public void add (string part) <br/> {<br/> parts. add (part); <br/>}</P> <p> Public void show () <br/>{< br/> console. writeline ("product parts -------"); <br/> foreach (string part in parts) <br/> console. writeline (part ); <br/>}</P> <p>/** // <summary> <br/> // client test <br/> /// </Summary> <br/> public class client <br/> {<br/> Public static void main (string [] ARGs) <br/>{< br/> // create director and builders <br/> Director ctor = new director (); </P> <p> builder b1 = new concretebuilder1 (); <br/> builder b2 = new concretebuilder2 (); </P> <p> // construct two products <br/> Director. construct (B1); <br/> product p1 = b1.getresult (); <br/> p1.show (); </P> <p> Director. construct (B2); <br/> product P2 = b2.getresult (); <br/> p2.show (); <br/> console. read (); <br/>}< br/>

4. Activity sequence of builder mode:

The client is responsible for creating the mentor and specific builder objects. Then, the customer handed over the specific builder object to the mentor. The customer ordered the mentor to manipulate the builder to start creating the product. After the product is created, the builder returns the product to the client.

V. Implementation of the builder mode:

The following program code demonstrates how the shop object uses vehiclebuilders to build different transportation tools. This example uses the builder mode to sequentially construct different parts of a vehicle.

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. collections; </P> <p> namespace building <br/> {<br/> // builder pattern -- real world example </P> <p> // "director" <br /> class shop <br/>{< br/> // methods <br/> Public void construct (vehiclebuilder) <br/>{< br/> vehiclebuilder. buildframe (); <br/> vehiclebuilder. buildengine (); <br/> vehiclebuilder. buildwheels (); <br/> vehiclebuilder. builddoors (); <br/>}</P> <p>/"Builder" <br/> abstract class vehiclebuilder <br/>{< br/> // fields <br/> protected vehicle; </P> <p> // properties <br/> public vehicle Vehicle <br/>{< br/> get {return vehicle ;} <br/>}</P> <p> // methods <br/> Abstract Public void buildframe (); <br/> Abstract Public void buildengine (); <br/> Abstract Public void buildwheels (); <br/> Abstract Public void builddoors (); <br/>}</P> <p> // "concretebuilder1" <br/> class motorcyclebuilder: vehiclebuilder <br/>{< br/> // methods <br/> override public void buildframe () <br/>{< br/> vehicle = new vehicle ("motorcycle"); <br/> vehicle ["frame"] = "Motorcycle Frame "; <br/>}</P> <p> override public void buildengine () <br/> {<br/> vehicle ["engine"] = "500 cc "; <br/>}</P> <p> override public void buildwheels () <br/> {<br/> vehicle ["Wheels"] = "2 "; <br/>}</P> <p> override public void builddoors () <br/> {<br/> vehicle ["Doors"] = "0 "; <br/>}</P> <p> // "concretebuilder2" <br/> class carbuilder: vehiclebuilder <br/>{< br/> // methods <br/> override public void buildframe () <br/>{< br/> vehicle = new vehicle ("car"); <br/> vehicle ["frame"] = "Car Frame "; <br/>}</P> <p> override public void buildengine () <br/> {<br/> vehicle ["engine"] = "2500 cc "; <br/>}</P> <p> override public void buildwheels () <br/> {<br/> vehicle ["Wheels"] = "4 "; <br/>}</P> <p> override public void builddoors () <br/> {<br/> vehicle ["Doors"] = "4 "; <br/>}</P> <p> // "concretebuilder3" <br/> class scooterbuilder: vehiclebuilder <br/>{< br/> // methods <br/> override public void buildframe () <br/>{< br/> vehicle = new vehicle ("Scooter"); <br/> vehicle ["frame"] = "scooter frame "; <br/>}</P> <p> override public void buildengine () <br/> {<br/> vehicle ["engine"] = "NONE "; <br/>}</P> <p> override public void buildwheels () <br/> {<br/> vehicle ["Wheels"] = "2 "; <br/>}</P> <p> override public void builddoors () <br/> {<br/> vehicle ["Doors"] = "0 "; <br/>}</P> <p>/"product" <br/> class vehicle <br/>{< br/> // Fields <br/> private string type; <br/> private hashtable parts = new hashtable (); </P> <p> // constructors <br/> public vehicle (string type) <br/>{< br/> This. type = type; <br/>}</P> <p> // indexers <br/> Public object this [String key] <br/>{< br/> get {return parts [key];} <br/> set {parts [Key] = value ;}< br/>}</P> <p> // methods <br/> Public void show () <br/> {<br/> console. writeline ("---------------------------"); <br/> console. writeline ("vehicle type:" + type); <br/> console. writeline ("frame:" + parts ["frame"]); <br/> console. writeline ("engine:" + parts ["engine"]); <br/> console. writeline ("# Wheels:" + parts ["Wheels"]); <br/> console. writeline ("# doors:" + parts ["Doors"]); <br/>}</P> <p>/** // <summary> <br/> // builderapp test <br/> /// </Summary> <br/> public class builderapp <br/> {<br/> Public static void main (string [] ARGs) <br/>{< br/> // create shop and vehicle builders <br/> shop = new shop (); <br/> vehiclebuilder b1 = new scooterbuilder (); <br/> vehiclebuilder b2 = new carbuilder (); <br/> vehiclebuilder B3 = new motorcyclebuilder (); </P> <p> // construct and display vehicles <br/> shop. construct (B1); <br/> b1.vehicle. show (); </P> <p> shop. construct (B2); <br/> b2.vehicle. show (); </P> <p> shop. construct (B3); <br/> b3.vehicle. show (); <br/> console. read (); <br/>}< br/>
6. Evolution of builder Models

The builder mode can evolve into multiple forms during use.

Omitting the abstract builder role

If you only need a specific builder in the system, you can omit the abstract builder. The code may be as follows:

// "Director" <br/> class director <br/>{< br/> private concretebuilder; </P> <p> // methods <br/> Public void construct () <br/> {<br/> builder. buildparta (); <br/> builder. buildpartb (); <br/>}< br/>}
The instructor role is omitted.

If there is only one specific builder, if the abstract builder role has been omitted, you can also omit the instructor role. Let the builder role assume the dual role of mentor and builder. The code may be as follows:

Public class builder <br/>{< br/> private product Product = new product (); </P> <p> Public void buildparta () <br/>{< br/> // some code here <br/>}</P> <p> Public void buildpartb () <br/>{< br/> // some code here <br/>}</P> <p> Public Product getresult () <br/>{< br/> return product; <br/>}</P> <p> Public void construct () <br/>{< br/> buildparta (); <br/> buildpartb (); <br/>}< br/>}

At the same time, the client also needs to make corresponding adjustments, as shown below:

Public class client <br/>{< br/> Private Static Builder; </P> <p> Public static void main () <br/>{< br/> builder = new Builder (); <br/> builder. construct (); <br/> product Product = builder. getresult (); <br/>}< br/>}

Stringbuilder in C # is an example.

7. Under what circumstances should the builder mode be used?

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 use of the construction model allows the internal appearance of the product to change independently. 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.

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.