The build mode is mainly used for complex product production, separating Component Construction Details to achieve good scalability.
Considering that the design model comes from architecture, let's look at an example of building a house. Now a customer wants to build a House,
Copy codeThe Code is as follows: public class House {
// House requested by the customer
}
Then he needs a Designer first, but the Designer can only design and instruct how to build the house, but he does not do it himself. Then he also needs a construction team BuildTeam, first, Designer should design how to build the house. First, the construction team should lay the foundation, and then the construction team should frame the skeleton, and then carry on the cement. (the specific details are unknown. Consult professionals ), from here, we can know that the designers have requirements for the construction team, that is, the construction team must lay the foundation, frame the skeleton, and meet the cement. Therefore, the following requirements are obtained for recruiting the construction team:Copy codeThe Code is as follows: public interface BuildTeam {
Public void lay the foundation ();
Public void frame skeleton ();
Cement () on public void ();
......
}
It can be seen from the above that, in order to do the construction team of this project, we must first sign the above conditions and do everything above. According to the designer's design, I learned that the designer will issue a command to the construction team, and then the construction team starts according to the designer's requirements:Copy codeThe Code is as follows: public class Designer {
Public void construct (BuildTeam ){
Team. lay the foundation ();
Team. Frame ();
Team. Cement ();
}
}
Since the designers started from the beginning to the end to instruct the design, and the construction team to carry out the actual construction, the customer will eventually find the construction team to accept the house, so the construction team must deliver the house to the customer, therefore, the construction team must add a provision to deliver the house. Otherwise, the house will be made, but the construction team will not deliver it to me, which is not a loss. Therefore:Copy codeThe Code is as follows: public interface BuildTeam {
Public void lay the foundation ();
Public void frame skeleton ();
Cement () on public void ();
......
Public House deliverHouse (); // Add a method for House delivery.
}
Well, the house has been designed, and how has it been designed? Now I want to do it for anyone. Now there is a construction team:Copy codeThe Code is as follows: public class BuildTeamA extends BuildTeam {
Public void laying ground (){}
Public void frame (){}
Cement () {} on public void (){}
......
Public House deliverHouse (){}
}
From the perspective of the construction team, the construction team fully complies with the requirements of the designer for the construction team, both interface BuildTeam and good, so they finally decided to do it. The whole process from start to end is as follows:
Designer designer = new Designer (); // find a Designer
BuildTeam teamA = new BuildTeamA (); // find a construction team BuildTeamA
Designer. construct (teamA); // The designer commands to let the construction team start building according to his design
House house = teamA. deliverHouse (); // deliver the House after the construction team completes
The first house was finally built. At this time, the same customer thought the designer was well designed, so he decided to use his design and instruct the construction team to recreate the same house, however, the construction team BuildTeamA suddenly opened his mouth and wanted more money. In order to save costs, the customer had to recruit a new construction team for construction again. Just as a construction team BuildTeamB met the requirements, the process was as follows:Copy codeThe Code is as follows: BuildTeam teamB = new BuildTeamB ();
Designer. construct (teamB); // because the designer has not changed and the same house has been built, the designer does not need to find a new one. You only need to replace the construction team he instructed with BuildTeamB.
House house = teamB. deliverHouse (); // deliver the House after the construction team is complete
Well, the second house has been completed, but the entire process has not changed much. Due to the use of the construction model, the entire process has a clear division of labor and customers do not need to participate in any design and construction, designers are only responsible for designing and executing commands, while the construction team is only responsible for specific implementation details, so that the construction details are independent and different construction teams can be changed at any time.