Builder Mode Description
1. Separate the construction of a complex object from its representation so that the same creation process can have different representations. This is called the builder pattern.
2. Description in object-oriented language, main roles:
1>. Builder interface class defines the builder [worker], a unified and operational way of behavior, which represents a complex structural object;
2>. ConcreteBuilder is used to create instance objects of various forms of Builder to represent different representations of Builder;
3>. Director This commander is used to guide the execution process and form of the Builder instance, which is used to separate from the Builder instance performance, and is used to guide the Builder instance to create and generate product results in a certain rule order;
4>. The results created by ResultObject will generate a result object; this is the result created by the specific creator based on Director guidance;
3. The builder model is actually a commander, a builder, and a customer who uses the commander to call the specific builder to work and can draw results from the specific builder;
4. Builder mode, simulated scenario: [See a good example that illustrates the description of Builder mode]
It is said that a family member wants to build a house, but the owner of the house or other people in the family does not know how to build a house, so he has to hire a few workers. The team of the house building must have a foreman to build a house according to the owner's ideas. The foreman designs and requires workers to do what they do according to the owner's requirements;
The foreman said that the first step is to build the entire skeleton of the house, the second step is to build the bedroom, the third step is to decorate the kitchen, the fourth step is to build the living room, and the fifth step is to...
The foreman does not do anything, but the specific builder must do it according to the foreman's requirements. The first and second steps are built until the entire house is completed;
The creator must have all the skills to create this house, namely, build skeletons, decorate the bedroom, etc., that is, what the builder does, or has abilities, must be greater than or equal to what the commander requires to do, or has abilities;
That is, the commander is an organizer, and the builder provides skills;
5. In a weak language like JavaScript, if there is no such thing as an interface, ignore the interface definition layer, directly create a specific builder, and then build a guidance class to call back and forth the builder;
Instance source code
1. Worker Builder X:
The code copy is as follows:
function workerBuilder() {
this.workOne = function() {
//Build a skeleton
}
this.workTwo=function() {
//Build a bedroom
}
this.workThree=function() {
//Build a kitchen
}
this.workFour=function() {
//Build a living room
}
//...
this.getResult = function() {
//Build a house
var house = new House();
//house.HouseFrame...
return house;
}
}
workBuilder is a specific builder, workOne, Two is what to do, build skeletons, etc.;
Of course, workBuilder can be built several more to indicate that workers perform different methods for each job; but the work content is the same;
2. Commander category
The code copy 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 the work items can also be set.
}
}
The guidance method under the commander category includes callback references to the builder, including several or all of the builder's work content; the commander organizes and arranges what the builder workers need to do;
3. Product house
The code copy is as follows:
function House() {
this.HouseFrame = '';
this.Room = '';
this.Kitchen = '';
this.LivingRoom = '';
//...
}
4. How to use
The code copy is as follows:
var builder = new workBuilder();
var director = new Director();
director.construct(builder);
var house = builder.getResult();
The fourth step is that the entire use is equivalent to the customer: the owner of the house. The owner asks the Director foreman to build the house, but the foreman does not do anything, so he directs the builder worker to build the child, and finally the owner obtains the built house from the worker;
Other Instructions
The builder model is more suitable for which the content [abstract] is complex, and the actual scenes are different, such as the situation where the work content or order is inconsistent; for example, the daily life process of each person, and scenes similar to the examples above; through the instructor layer, it is possible to reduce the environment in which many similar workplaces but the order of work rules is inconsistent; it can greatly reduce the construction abstraction of actual objects;