Builder Mode Description
1. Separate the construction of a complex object from its representation so that the same creation process can have different representations, which is called the builder pattern.
2. Description in the object-oriented language, main roles:
1>. Builder, the interface class, defines the builder [worker], a uniform, actionable way of behaving, which represents a complex structural object;
2>. ConcreteBuilder is used to create instance objects of the builder's various forms to represent different representations of the builder;
3>. Director This conductor directs the execution process and form of the builder instance, which is used to separate the instance representation of the builder, to instruct the builder instance to create the resulting 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's guidance;
3. The builder model is actually a conductor, a builder, a client who uses the command to invoke a concrete builder to work, and to derive results from concrete builders;
4. Builder mode, simulation scenario: [See an example of a description of the builder's pattern is good]
Said a family to build a house, but the house owner or Other people do not know how to build a house, so he has to ask a few workers, the building of the team has to have a foreman, to the owners of the idea of building a house, the foreman according to the requirements of the owner of the design requirements of workers how to do;
The foreman said that the first step to the whole skeleton of the house, the second step to build a good bedroom, the third step to decorate the kitchen , the fourth step to the living room construction decoration completed, Fifth step ...
The foreman does not do the work, but the concrete builders must follow the foreman's request, the first step, the second step to build, until the whole house is finished;
The creator must have all the skills to create the house, namely the skeleton, decorate the bedroom, etc.., that is, what the builder does, or the capacity it has, must be greater than or equal to what the conductor requires or has the ability to do;
That is, the conductor is an organizer, and the builder provides the skill;
5. JavaScript in this weak language, there is no interface such things, ignoring the interface definition of this layer, directly create concrete builders, and then build a guidance class back and forth to tune the builder;
Example Source
1. Worker Builder X:
functionWorkerbuilder () { This. Workone =function() { //Housing sub-skeleton } This. worktwo=function() { //building a sleeping room } This. workthree=function() { //built Kitchen } This. workfour=function() { //Built living room } //....
This. GetResult =function() {
//Built House
var house = New house ();
House. Houseframe ...
return house;
}}
Workbuilder is the concrete builder class, Workone, the other is to do things, build skeleton, etc.
Of course, Workbuilder can build several more to show that workers do not perform the same way for each job, but the job content is the same;
2. Conductor class
function Director () { thisfunction(builder) { builder.workone (); Builder.worktwo (); Builder.workthree (); Builder.workfour (); // ... // the above content, the order can be set, and the work item can also be set }}
The guidance method under the conductor class has a callback reference to the builder, which includes several or all of the contents of the work of the builders; The conductor organizes and arranges the things that the builder's workers must do;
3. Product House
function House () { this. Houseframe = "; this. The "*"; this. Kitchen = "; this. Livingroom = "; // ...}
4. How to use
var New Workbuilder (); var New Director ();d irector.construct (builder); var house = Builder.getresult ();
The fourth step, the entire use of the equivalent of the customer: The owner of the landlord, the owner of the Director to build a house, but the foreman is not doing things, so he commanded builders to build a son, finally the owner from the workers to obtain a built house;
Other Notes
The builder model is more suitable for that, the content [abstract] complex, the actual scene is different, such as the work content or the order is inconsistent, such as each person's daily life process Ah, and like the above examples of similar scenes, through the mentor layer can be reduced to many similar work situations, But the order of the working rules is inconsistent, and the construction abstraction of the actual object can be greatly reduced;
[design mode] JavaScript Builder mode