Common design patterns: [construction models]

Source: Internet
Author: User
1. Builder Mode

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.
Another example: Imagine a complex product like a computer. Each computer has many components, including CPU, optical drive, video card, memory ....... the final computer product needs to be installed with the CPU and memory in a certain order. The computer cannot be used before the assembly process ends. This assembly process may be complex and may change frequently. Then it is necessary to abstract this variable process into an external computer assembler responsible for the product assembly process. In addition, the customer will certainly have some preferences for computer products, such as some who like Dell Computers and some who like IBM computers. These computer producers have almost the same way of assembling products, that is to say, for different customer needs, the construction process of the computer assembler can be suitable for different producers.
The builder mode can be used to assemble a complex product composed of multiple parts.

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.

Import Java. util. arraylist; /*** [Build mode] * @ author Potter */public class buildertest {/*** @ Param ARGs */public static void main (string [] ARGs) {buildertest test = new buildertest (); buildertest. director dire= test. new Director (); buildertest. builder b1 = test. new councretebuilder1 (); // construct products: Build product ctor. counstruct (B1); product p1 = b1.getresult (); p1.show ();} // Director: Instructor role class director {// methodspublic void counstruct (Builder) {builder. buildparta (); builder. buildpartb () ;}}// builder: builder role abstract class builder {// methodsabstract public void buildparta (); Abstract Public void buildpartb (); Abstract Public Product getresult ();} // councretebuilder1: The specific builder role class councretebuilder1 extends builder {private product Product = new product (); @ overridepublic void buildparta () {// todo auto-generated method stubproduct. add ("parta") ;}@ overridepublic void buildpartb () {// todo auto-generated method stubproduct. add ("partb") ;}@ overridepublic product getresult () {// todo auto-generated method stubreturn product ;}} // product Product role class product {// fields arraylist <string> parts = new arraylist <string> (); // methodspublic void add (string part) {parts. add (part);} public void show () {system. out. println ("product parts ----"); For (string part: parts) {system. out. println ("" + part );}}}}

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. 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:

Import Java. util. arraylist; /*** [Build mode] * @ author Potter */public class buildertest {/*** @ Param ARGs */public static void main (string [] ARGs) {buildertest test = new buildertest (); buildertest. director dire= test. new Director (); // construct products: Build the product director. counstruct (); product p1 = Director. builder. getresult (); p1.show () ;}// ctor: Instructor role class director {private councretebuilder1 builder = new councretebuilder1 (); // methodspublic void counstruct () {builder. buildparta (); builder. buildpartb () ;}}// councretebuilder1: The specific builder role class councretebuilder1 {private product Product = new product (); Public void buildparta () {// todo auto-generated method stubproduct. add ("parta");} public void buildpartb () {// todo auto-generated method stubproduct. add ("partb");} public product getresult () {// todo auto-generated method stubreturn product ;}} // product Product role class product {// fields arraylist <string> parts = new arraylist <string> (); // methodspublic void add (string part) {parts. add (part);} public void show () {system. out. println ("product parts ----"); For (string part: parts) {system. out. println ("" + part );}}}}

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:

Import Java. util. arraylist; /*** [Build mode] * @ author Potter */public class buildertest {/*** @ Param ARGs */public static void main (string [] ARGs) {buildertest test = new buildertest (); councretebuilder1 builder = test. new councretebuilder1 (); builder. counstruct (); product p1 = builder. getresult (); p1.show () ;}// councretebuilder1: The specific builder role class councretebuilder1 {private product Product = new product (); Public void buildparta () {// todo auto-generated method stubproduct. add ("parta");} public void buildpartb () {// todo auto-generated method stubproduct. add ("partb");} public product getresult () {// todo auto-generated method stubreturn product;} public void counstruct () {buildparta (); buildpartb () ;}}// Product role class product {// fields arraylist <string> parts = new arraylist <string> (); // methodspublic void add (string part) {parts. add (part);} public void show () {system. out. println ("product parts ----"); For (string part: parts) {system. out. println ("" + part );}}}}

 

6. 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.

 

See the article:

Http://archive.cnblogs.com/a/1133133/

Related information:

Http://www.cnblogs.com/abcdwxc/archive/2007/08/30/876133.html

 

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.