Introduction to builder mode of JavaScript design mode and javascript Design Mode

Source: Internet
Author: User

Introduction to builder mode of JavaScript design mode and javascript Design Mode

Builder mode description

1. The construction of a complex object is separated from its representation so that the same creation process can have different representations. This is called the builder mode.
2. Description in object-oriented language. Main roles:

1>. Builder: this interface class defines the Builder [Worker] and provides a unified and operable behavior, which represents a complex structure object;
2>. ConcreteBuilder is used to create instance objects in various forms of [Implementation] Builder to represent different representations of Builder;
3>. ctor this conductor is used to guide the execution process and form of the Builder instance. It is used to isolate the performance of the Builder instance and to guide the Builder instance to create and generate product results in a certain order of rules;
4>. The results created by ResultObject generate a result object. This is the result created by the specific creator according to the Director guide;

3. The Builder mode is actually a conductor, a builder, and a customer who uses the conductor to call the work of a specific builder and obtain the result from the specific builder;

4. Builder mode, simulated scenario: [see an example that describes the builder mode.]

If a family wants to build a house, but the house owner or other people in the House do not know how to build the house, he has to hire several workers, the house building team also had to have a foreman to build a house based on the homeowner's ideas. The foreman was designed to require workers to do what they wanted;

The foreman said that the first step is to build the entire skeleton of the room, the second step is to build the bedroom, the third step is to decorate the kitchen, the fourth step is to complete the construction and decoration of the living room, and the fifth step...

The foreman does not do things, but the specific builder must follow the foreman's requirements. The first step is the second step until the whole house is finished;

The creator must have all the skills to create the house, that is, to create a skeleton, to decorate the bedroom ..., that is, what the builder does or has the ability to do must be greater than or equal to what the conductor requires to do or have the ability;

That is, the conductor is an organizer, and the Builder provides skills;

5. In a weak language like JavaScript, if there is no interface, ignore the interface definition layer, directly create a specific builder, and then create a guide class to call the builder back and forth;

Instance source code

1. Worker builder X:
Copy codeThe Code is as follows:
Function workerBuilder (){
This. workOne = function (){
// Build the house skeleton
}
This. workTwo = function (){
// Create a bedroom
}
This. workThree = function (){
// Create a kitchen
}
This. workFour = function (){
// Create a living room
}
//....

This. getResult = function (){
// Build a house
Var house = new House ();
// House. HouseFrame...
Return house;
}
}


WorkBuilder is a specific builder class. workOne and Two are the tasks to be done and the skeleton should be built;

Of course, workBuilder can be created several times, which indicates that the workers perform different methods for each job, but the work content is the same;

2. Conductor

Copy codeThe Code is as follows:
Function Director (){
This. construct = function (builder ){
Builder. workOne ();
Builder. workTwo ();
Builder. workThree ();
Builder. workFour ();
//...
// The above content can be set in sequence and work items can also be set
}
}

The Guiding Methods in the Command owner class include callback references to the builder, including several or all items of the builder's work; the command owner organizes and arranges the tasks that the builder can do;

3. Product house

Copy codeThe Code is as follows:
Function House (){
This. HouseFrame = '';
This. Room = '';
This. Kitchen = '';
This. LivingRoom = '';
//...
}

4. Usage


Copy codeThe Code is as follows:
Var builder = new workBuilder ();
Var director = new Director ();
Director. construct (builder );

Var house = builder. getResult ();

Step 4: The entire use is equivalent to the customer: the homeowner. The homeowner asks Director to build the house, but the foreman does not do anything. Therefore, he directs the builder to build the house, finally, the homeowner obtained the house from the workers;

Other Instructions

The builder mode is suitable for the scenario where the content [Abstract] is complex and the actual scenario is different, for example, the work content or order is inconsistent. For example, the daily life process of everyone, there are also scenarios similar to the above examples. Through the instructor layer, you can reduce the environments for many similar work scenarios, but the work rules are not in the same order; you can greatly reduce the construction abstraction of actual objects;

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.