Builder Model Description
1. Separating the construction of a complex object from its representation allows the same creation process to be represented differently, which is called the builder pattern.
2. Description in an object-oriented language, main role:
1>. Builder this interface class, defines the builder [worker], the unified operable behavior, which represents a complex structure object;
2>. ConcreteBuilder is used to create an instance object Builder various forms to represent Builder different representations;
3>. Director This conductor is used to instruct the execution process and form of the Builder instance, to be separated from the Builder instance performance, and to instruct the Builder instance to create product results in a regular order;
4>. The results created by resultobject generate a result object, which is the result created by the specific creator according to the Director guidance;
3. The builder model is, in fact, a conductor, a builder, a client who uses a conductor to invoke the work of a specific builder and obtain results from a specific builder;
4. Builder model, simulation scenario: [See a description of the builder pattern is good]
That a family would build a house, but the owner of the house or the rest of the family did not know how to build the house, so he had to go to a few workers, the building team had to have a foreman to build a house according to the idea of the homeowner, the foreman according to the requirements of the owners to design how to do the workers;
The foreman said the first step is to set up the whole skeleton of the house, the second step is to build the bedroom, the third step to decorate the kitchen, the fourth step to decorate the living room finished, fifth step ...
The foreman does not work, but the concrete builder must follow the foreman's request, the first step, the second step to build, until the whole house is completed;
The creator must have all the skills to create this House, that is, to build the skeleton, decorate the bedroom, etc. ..., that is, the builder's doing, or the ability to have, must be greater than or equal to the command required to do, or have the ability;
That the conductor is an organizer, and the builder provides the skill;
5. JavaScript in this weak language, there is no interface such things, ignore the interface definition of this layer, directly create concrete builders, and then build a guidance class back and forth the builder;
Instance source code
1. Worker Builder X:
Copy Code code as follows:
function Workerbuilder () {
This.workone = function () {
Build the skeleton of the house
}
This.worktwo=function () {
Building a sleeping room
}
This.workthree=function () {
Build a kitchen
}
This.workfour=function () {
Building a living room
}
//....
This.getresult = function () {
Build a House
var house = new House ();
House. Houseframe ...
return to house;
}
}
Workbuilder is the concrete builder class, Workone, two is to do things, build skeleton, etc.;
Of course, Workbuilder can be built more to indicate that workers perform differently for each job, but the job content is the same;
2. Conductors ' class
Copy Code code as follows:
function Director () {
this.construct = function (builder) {
Builder.workone ();
Builder.worktwo ();
Builder.workthree ();
Builder.workfour ();
//...
The contents of the above, the order can be set, and work items can also be set
}
}
The guidance method under the conductor category has a callback reference to the builder, which includes several or all of the work content of the builders; The conductor organizes and arranges what the builder's workers are to do;
3. Product House
Copy Code code as follows:
function House () {
This. Houseframe = ';
This. Room = ';
This. Kitchen = ';
This. Livingroom = ';
//...
}
4. How to use
Copy Code code as follows:
var builder = new Workbuilder ();
var director = new Director ();
Director.construct (builder);
var house = Builder.getresult ();
The fourth step, the entire use of the customer: homeowners, the owner of the house please Director the foreman to build houses, but the foreman is not doing things, so he commanded builder to build the son, the last homeowner from the workers to obtain the building of the house;
Other Notes
The builder model is better suited to that, content [abstract] complex, the actual scene performance is different, such as the work content or the order is inconsistent, such as each person's daily life process Ah, there are similar to the above example of the scene, through the instructor layer, can be reduced for many similar work situations, But the environment with inconsistent order of work rules can greatly reduce the construction abstraction of actual objects;