Design Mode Summary-builder Mode

Source: Internet
Author: User

1. Understanding of the "process" and "parts" of builder mode decoupling:

Taking assembling a car as an example, an automobile can be composed of components such as the engine, steering wheel, and wheel. To assemble these components into a complete automobile, a complicated process is required. Similar components are assembled in different automobile factories and become different automobiles. This is the original requirement prototype of the builder mode.

We can see that if we can provide such a service: the customer only needs to provide us with the necessary parts and inform us of the type of car to be assembled, the automotive assembly company is responsible for the assembly of the entire backend-completely transparent to customers. Then we can decouple "components" from "processes. As long as we are willing, the same parts can be converted into different products in our hands.

Ii. Components of the builder mode:

We can understand the composition of builder models from two different perspectives-customers and factories.

First, from the perspective of the customer, what the customer can see and feel isProducts and Components,From the factory perspective, they are interested in how to produce these parts and how to assemble them into a qualified product.

Therefore, we can divide the builder mode into four components:

· Product: public interface Product

· Part: public interface part

· Part production process: Public interfact Builder

· Assembling parts: public class director

Among them, products and parts are an interface, which can have multiple implementation subclasses. We focus on the "process of producing parts" and "process of assembling parts"

For the same type of products, the "process of producing parts" is different. Therefore, we must define it as an interface to implement the specific process by the manufacturer of each part. As follows:

Public interface builder {
// Create part A, for example, creating a Car Wheel
Void buildparta ();
// Create part B, for example, create a car steering wheel
Void buildpartb ();
// Create part C, for example, create a car engine
Void buildpartc ();

// Return the final assembly result (return the final assembled car)
// The assembly process of finished products is not carried out here, but transferred to the director class below.
// Achieves the decoupling process and components
Product getresult ();
}

Public class concretebuilder implements builder {
Part parta, partb, partc;

Public void buildparta () {// here is the code for building parta };
Public void buildpartb () {// here is the specific code for building partb };
Public void buildpartc () {// here is how to build the partb code };

Public Product getresult (){
// Return the final assembly result
};
}

OK. Now that the manufacturer already knows how to make parts, the rest is: 1). produce these parts according to customer requirements. 2) Assemble parts into products.

The director class is responsible for assembling finished products. It interacts with the builder class and tells builder that I want to produce parts and assemble the parts produced by builder class into finished products, as shown below:

Public class director {

Private Builder;
 
Public Director (Builder ){
This. Builder = builder;
}

// Combine part parta partb partc to form a complex object
// The process of assembling the wheels, steering wheel, and engine into a car
Public void construct (){
Builder. buildparta ();
Builder. buildpartb ();
Builder. buildpartc ();
.....
}
}

Iii. Use of the builer mode:

· Create a builer object: builder builer = new concretebuiler ();
· Create a ctor object: Director dire= new director (builder );
· Call Director object construction product: Director. Construct ();
· Return the constructed product: Product = builder. getresult ();

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.